如何以编程方式在 IntelliJ IDEA 中打开特定文件

How to open particular file in IntelliJ idea programatically

我正在 IntelliJ idea 中开发 Android studio 插件,我需要在其中打开特定的 java 文件。我可以打开 selectedTextEditor,使用编辑器、文档和 VirtualFile API 以及以下代码进行编辑和保存。

 @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);
                                }
                            }

                );
    }

但是我想打开一个特定的文件(比如 abc.java),没有找到任何 API 来实现这个。只是想知道,如果不是open.Thanks

,我们怎么能open/save/edit特别file/Editor

要在编辑器中打开文件,请使用 new OpenFileDescriptor(project, virtualFile).navigate()

您可能想要修复代码中的其他一些问题:

  • 不要在 IntelliJ IDEA 类 上使用 toString() 方法进行调试以外的任何事情。您通过 toString() 所做的所有事情都有适当的 API。
  • 您不需要两种不同的方式来获取编辑器。第一个(e.getRequiredData())是正确的。
  • 不要使用 BufferedReader 从磁盘读取文件的文本。请改用 Document.getText()
  • 您不需要 invokeLater() 电话。