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?
Please sign in to leave a comment.
In what context is your code operating?
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.
You can obtain editor(s) for file via com.intellij.openapi.fileEditor.FileEditorManager
Thank you, I have instance of FileEditor, is it possible to change caret position from here? That is my goal.
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
Thank you this works.