Retrieve the default editor of the current view

Answered

I am developing a plugin that creates a second ModelEditor for a text file provided by a ModelEditorProvider. So I got these nice litte tabs on the bottom. Now I want to synchronize these two views so the curse would move to the object that is selected in the other Editor, but obviously only in the same view not in a split view. How do retrieve the default Editor in the current view to get the CaretModel?

I found out how to get all Editors of the virtual file… But how do I get the specific Editor in the current view?

0
5 comments

Hi Sven,

Please clarify your use case. I understand that:

  1. There is some already existing text editor for the file you edit.
  2. You created a custom visual editor.
  3. You want to open your visual editor and if you select some object, the cursor in the text editor should be positioned on the selected object.

What do you mean by "only in the same view not in a split view". Can you provide a screenshot or drawing?

What do you mean by the default editor in the current view?

0

I can split the view of a file so I have two views of the same file as in the screenshot. As you can see each view has a Text and a Design tab. The Design tab is from my plugin. Now when I create my Design tab, I want to retrieve the Editor object from the Text tab of the same view. So when I select an object the cursor is only moved in this view but not in the other. But when I ask for Editors of this file. I get all of them. So I don't know which one is in the same view.

In the ModelEditorProvider the Text tab is referred to as the default editor.

0

I see now, thanks for the explanation.

Please take a look at EditorComposite which represents a group of editors for a file in a single tab. To get an instance of EditorComposite you have to access EditorWindow, which can be achieved with one of the FileEditorManagerEx's methods.

Next, you can use EditorWindow.getComposite(VirtualFile) to get the composite for your file, and in the composite, you can access editors.

0

Thanks for the answer. I didn't had time before to check it.

I implemented FileEditorProvider. In the createEditor method I create my FileEditor implementation. Here I would like to link to the the default text editor which seams to be an PsiAwareTextEditorImpl object.

But I couldn't find a way to get the EditorComposite I need. And when I looked into the IntelliJ source code it was no surprise because the object is not created at that point. At least in the case of a new window.

So right now I use ApplicationManager.getApplication().invokeLater() to get my code executed when all objects are created.

I guess I can use FileEditorManagerEx.getCurrentWindow() at that time to get the correct EditorWindow no matter if it was just created or not. Otherwise I would have to scan all EditorWindows for the FileEditor I just created. Now I can scan EditorComposite for an instance of the TextEditor interface which should be the default editor associated to my new FileEditor, right?

 This seams unnecessary complicated for something that should be a common case in plugin development... Isn't there a better way to do this?

 

0

Hi Sven,

Now I can scan EditorComposite for an instance of the TextEditor interface which should be the default editor associated to my new FileEditor, right?

Correct.

IMHO this is not a typical use case for plugin developers as editors are usually independent and there is rarely a need for accessing editor windows. If you are worried about performance, then you can update the caret position on specific events like editor selection change:
https://github.com/JetBrains/intellij-community/blob/master/platform/analysis-api/src/com/intellij/openapi/fileEditor/FileEditorManagerListener.java#L63

Also, scanning all EditorWindows shouldn't be a big deal as there are only at most 10 (by default) editor tabs/windows open at a given moment, but I would avoid doing it without the need (on every selection in your custom editor).

0

Please sign in to leave a comment.