简体   繁体   中英

How to open particular file in IntelliJ idea programatically

I'm developing Android studio plugin in IntelliJ idea, where I need to open particular java files. I'm able to open selectedTextEditor, edit and save using Editor, Document and VirtualFile API and following code.

 @Override
    public void actionPerformed(AnActionEvent e) {
        final Project project = e.getProject();
        if (project == null) {
            return;
        }
        final Editor edit = e.getRequiredData(CommonDataKeys.EDITOR);
        System.out.println("Edit=======>>>>>>>>>>>>>>>>>>>>" + edit.getDocument());

        MANIFEST_PATH = project.toString() + "AndroidManifest.xml";

        System.out.println("project Path=>>>>>>" + project.toString());

        Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();

        FileEditor editors[] = FileEditorManager.getInstance(project).getAllEditors();
        if (editor == null) {
            return;
        }
        System.out.println("editor Path=>>>>>>" + editor.toString());
        for (int index = 0; index < editors.length; index++) {
            System.out.println("editors Path=>>>>>>" + editors[index].toString());
        }

        final Document document = editor.getDocument();
        System.out.println("document Path=>>>>>>" + document.toString());
        VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
        if (virtualFile == null) {
            return;
        }
        System.out.println("Virtual Path=>>>>>>" + virtualFile.toString());
        final String contents;
        try {
            System.out.println("Virtual getpath=>" + virtualFile.getPath());
            BufferedReader br = new BufferedReader(new FileReader(virtualFile.getPath()));
            String currentLine;
            StringBuilder stringBuilder = new StringBuilder();
            StringBuilder sb = new StringBuilder();
            while ((currentLine = br.readLine()) != null) {
                stringBuilder.append(currentLine);
                stringBuilder.append("\n");
                if (currentLine.contains("package")) {
                    stringBuilder.append(ManifestGenerator.generateGCMPermission());
                } else if (currentLine.contains("android:icon")) {
                    stringBuilder.append(ManifestGenerator.generateMetaData());
                    stringBuilder.append(ManifestGenerator.generateService());
                    stringBuilder.append(ManifestGenerator.generateReceiver());
                }
            }
            contents = stringBuilder.toString();
        } catch (IOException e1) {
            return;
        }

        final Runnable readRunner = new Runnable() {
            @Override
            public void run() {
                document.setText(contents);
            }
        };
        ApplicationManager.getApplication().

                invokeLater(new Runnable() {
                                @Override
                                public void run() {
                                    CommandProcessor.getInstance().executeCommand(project, new Runnable() {
                                        @Override
                                        public void run() {
                                            ApplicationManager.getApplication().runWriteAction(readRunner);
                                        }
                                    }, "DiskRead", null);
                                }
                            }

                );
    }

But I want to open a particular file(say abc.java), didn't find any API to achieve this. Just want to know, how can we open/save/edit particular file/Editor if not open.Thanks

To open a file in the editor, use new OpenFileDescriptor(project, virtualFile).navigate() .

You may want to fix a few other things in your code:

  • Don't use the toString() method on IntelliJ IDEA classes for anything other than debugging. There are proper APIs for all the things that you're doing through toString().
  • You don't need two different ways to get the editor. The first one ( e.getRequiredData() ) is correct.
  • Don't use BufferedReader to read the text of the file from disk. Use Document.getText() instead.
  • You don't need the invokeLater() call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM