How to get current Editor instance?

Answered

Hi, I have instance of Project, Document, FileEditor and a Virtual file open in the editor, but I can't seem to get an Editor instance from these, I dont have AnActionEvent to use. I got my Project instance with the code: 

Project project = (Project) DataManager.getInstance().getDataContext().getData(DataConstants.PROJECT);

Using the same code for an editor instance, like so:

Editor editor = (Editor) DataManager.getInstance().getDataContext().getData(DataConstants.EDITOR);

gives null. How can I get an instance of the active Editor?

1
6 comments

You can obtain editor(s) for file via com.intellij.openapi.fileEditor.FileEditorManager

2

In what context is your code operating?

0

There is a graph being displayed in a tool window, with the text equivalent of that graph in the text editor. After the user clicks a part of that graph, i want to jump to its position in the text editor.

0

Thank you, I have instance of FileEditor, is it possible to change caret position from here? That is my goal.

0

try cast to com.intellij.openapi.fileEditor.TextEditor, com.intellij.openapi.fileEditor.TextEditor#getEditor -> com.intellij.openapi.editor.Editor#getCaretModel

see also http://www.jetbrains.org/intellij/sdk/docs/tutorials/editor_basics/coordinates_system.html

0

Thank you this works.

0

Please sign in to leave a comment.