Open modal editor and refactor code

Hi...

this is a newbie question:

I am writing a simple Plugin. I managed to open an editor with a (xml) String in a new Tab.

My first question:
How can I open the editor modal?

Second question:
How can I trigger reformat code (like it happens when pressing ctrl + alt + L).

My code looks like this:

@Override
public void actionPerformed(AnActionEvent anActionEvent) {



        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>...";


        Project currentProject = anActionEvent.getData(PlatformDataKeys.PROJECT);


        LightVirtualFile file = new LightVirtualFile("response.xml", xml);


        FileEditorManager manager = FileEditorManager.getInstance(currentProject);
        
        FileEditor[] editors = manager.openFile(file, true, true);


}


regards
Chris

4 comments
Comment actions Permalink

To open an editor in a modal dialog, use the EditorTextField component. To reformat a file, use CodeStyleManager.reformat().

0
Comment actions Permalink

Thanks for the awnser, but I still can't make my stuff work.

1. How can I make the EditorTextField appear. I tried both: .setVisible(true) and .show(). Do I have to wrap the EditorTextField in some way?
2. I am trying to reformat a LightVirtualFile but does not implement the 'VirtualFileWithId' interface so I get an exception. Is there any way to format file but not to save it?

regards
Chris

0
Comment actions Permalink

1. EditorTextField is a Swing component that needs to be added to a dialog. Creating an EditorTextField in the air will not make it appear anywhere.
2. Reformat works on a PSI file, not on a virtual file. Rather than creating a LightVirtualFile, create a PsiFile from text using PsiFileFactory.createFileFromText().

0
Comment actions Permalink

Thank you! Now it works.

0

Please sign in to leave a comment.