Reformat Scratch File in Custom Editor

Hi,

I'm using an editor instance in a JFrame. Which took me a while to implement, but now it's working.

 
ScratchpadManager scratchpadManager = ScratchpadManager.getInstance(project);
VirtualFile scratchFile = scratchpadManager.createScratchFile(psiFile.getLanguage());
EditorFactory editorFactory = EditorFactory.getInstance();
Document document = editorFactory.createDocument(selectedText);
Editor editor = editorFactory.createEditor(document, project, scratchFile, false);

The variable psiFile comes from the application editor. My questions is, how can I get the PSI File of scratchFile?
Or is there any method than CodeStyleManager.reformat() to reformat the code without getting the PSI File?

5 comments
Comment actions Permalink

ScratchpadManager is not intended, and also completely unnecessary for this kind of use. Instead, use PsiFileFactory.createFileFromText() to create a PSI file from text and then PsiDocumentManager.getInstance(project).getDocument(file) to get the corresponding document. You can then call CodeStyleManager.reformat() on your PSI file.

0
Comment actions Permalink

Thanks for your answer.
Now I got the following code:

 
final PsiFile psiFileFromText = PsiFileFactory.getInstance(project).createFileFromText("newSnippet." + fileExtension, psiFile.getVirtualFile().getFileType(), selectedText);
Document newDocument = PsiDocumentManager.getInstance(project).getDocument(psiFileFromText);

But newDocument is always null. What did I do wrong?

0
Comment actions Permalink

Try to use another factory method

 
public abstract PsiFile createFileFromText(@NotNull String name, @NotNull Language language, @NotNull CharSequence text,
                                           boolean
eventSystemEnabled, boolean markAsCopy);


and set eventSystemEnabled to true

0
Comment actions Permalink

Now I have

 
final PsiFile psiFileFromText = PsiFileFactory.getInstance(project).createFileFromText("newSnippet." + fileExtension, psiFile.getLanguage(), selectedText, true, false);

This code works, but syntax highlighting is missing and I cannot seletct text properly (see attached screenshot).



Attachment(s):
Screenshot 2015-03-04 20.55.42.png
0
Comment actions Permalink

Now I have found a solution that fits my need. I am using an EditorTextField now.

final PsiFile psiFileFromText = PsiFileFactory.getInstance(project).createFileFromText("newSnippet." + fileExtension, psiFile.getLanguage(), selectedText, true, false);
Document newDocument = PsiDocumentManager.getInstance(project).getDocument(psiFileFromText);
assert newDocument != null;
editorTextField = new EditorTextField(newDocument, project, psiFile.getFileType(), false, false);
editorTextField.setBackground(EditorColors.GUTTER_BACKGROUND.getDefaultColor());
editorTextField.setPlaceholder("Please insert your code snippet here");

JComponent component = editorTextField.getComponent();
component.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
component.setVisible(true);
scrollPaneCode.setViewportView(component);

ApplicationManager.getApplication().invokeLater(
      new Runnable() {
         @Override
         public void run() {
            new WriteCommandAction(project) {
               @Override
               protected void run(@NotNull Result result) throws Throwable {
                  CodeStyleManager.getInstance(project).reformat(psiFileFromText, false);
               }
            }.execute();
         }
      }
);
0

Please sign in to leave a comment.