Plugin to reformat an external Java file (or string)

I am trying to write a plugin that would format an external Java file passed to it (the file may or may not be part of the current project). However, the notion of Document, VirtualFile and PsiFile causes enough confusion for me to not being able to decide when the file is finally ready.

The code that seems to work as of now is this:

            application.invokeLater(new Runnable() {
                public void run() {
                    try {
                        WriteCommandAction.runWriteCommandAction(project, new Runnable() {
                            public void run() {
                                application.runWriteAction(new Runnable() {
                                    public void run() {
                                        final VirtualFile vFile = localFileSystem.findFileByIoFile(file);
                                        final PsiFile psiFile = psiManager.findFile(vFile);
                                        codeStyleMgr.reformatText(psiFile, Arrays.asList(psiFile.getTextRange()));
                                        Document document = fileDocMgr.getDocument(vFile);
                                        psiDocumentMgr.doPostponedOperationsAndUnblockDocument(document);
                                        psiDocumentMgr.commitDocument(document);
                                        fileDocMgr.saveDocument(document);
                                        editorManager.closeFile(psiFile.getVirtualFile());
                                        synchronized (formattingSync){
                                            formattingSync.notify();
                                        }
                                    }
                                });
                            }
                        });
                    } catch (Exception e) {
                        logger.error(e);
                    }
                }
            }, ModalityState.NON_MODAL);


            synchronized (formattingSync){
                try {
                    formattingSync.wait(15000);
                } catch (InterruptedException e) {
                    logger.error(e);
                }
            }

However, most of this is written on a trial and error basis. Also, due to my lack of understanding of how files are refreshed and persisted by IntelliJ's virtual file system, I am running into synchronization issues. I wonder if there is a simpler alternative than the above.

In fact, all I want is the ability to format an external Java string. Since reformatText is the only one that seems to work for me, I am forced to pass a file instead of string leading to more I/O and synchronization issues. Since the # of java files that I want formatted is in the order of 100k, I want to speed up things as much as possible.

I am currently looking for a function that takes a Java string and returns a formatted Java string. Any help is appreciated.

Please sign in to leave a comment.