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

2 comments
Comment actions Permalink

Assuming the file is in your project, you probably want:
com.intellij.psi.search.FilenameIndex to get the virtual file by name, and
com.intellij.openapi.fileEditor.OpenFileDescriptor to open the virtual file.

0
Comment actions Permalink

You might want to look at
http://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview.html
http://www.jetbrains.org/intellij/sdk/docs/basics/virtual_file_system.html

Using native java IO to access files is (generally) a bad idea.

  @Override
  public void actionPerformed(final AnActionEvent e) {
    Project project = e.getRequiredData(CommonDataKeys.PROJECT);

    VirtualFile file = VfsUtil.findFileByIoFile(new File("/path/to/file"), true);
    if (file == null) return; // file not found
    if (file.getFileType().isBinary()) return; // file is binary

    final Document document = FileDocumentManager.getInstance().getDocument(file);
    if (document == null) return; // can't read the file. Ex: it is too big

    CommandProcessor.getInstance().executeCommand(project, new Runnable() {
      @Override
      public void run() {
        ApplicationManager.getApplication().runWriteAction(new Runnable() {
          @Override
          public void run() {
            int index = StringUtil.indexOf(document.getCharsSequence(), "package");
            if (index == -1) return;
            int lineNumber = document.getLineNumber(index);
            int offset = document.getLineEndOffset(lineNumber);
            document.insertString(offset, "\nSOME_TEXT_HERE");
          }
        });
      }
    }, "Update android manifest", null);
  }

0

Please sign in to leave a comment.