From 9a6a2f1c70f31f51c49128d2445192b48eb5de27 Mon Sep 17 00:00:00 2001 From: Anna Bulenkova Date: Tue, 9 Dec 2014 17:26:30 +0100 Subject: [PATCH] [code + md] getting the active editor --- .../editor/basics/EditorIllustration.java | 2 ++ tutorials/working_with_editor.md | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/code_samples/editor_basics/src/org/jetbrains/plugins/editor/basics/EditorIllustration.java b/code_samples/editor_basics/src/org/jetbrains/plugins/editor/basics/EditorIllustration.java index 1ebbbcb3a..f46768a11 100644 --- a/code_samples/editor_basics/src/org/jetbrains/plugins/editor/basics/EditorIllustration.java +++ b/code_samples/editor_basics/src/org/jetbrains/plugins/editor/basics/EditorIllustration.java @@ -16,8 +16,10 @@ public class EditorIllustration extends AnAction { } @Override public void update(AnActionEvent e) { + //Get required data keys final Project project = e.getData(CommonDataKeys.PROJECT); final Editor editor = e.getData(CommonDataKeys.EDITOR); + //Set visibility only in case of existing project and editor and if some text in the editor is selected e.getPresentation().setVisible((project != null && editor != null && editor.getSelectionModel().hasSelection())); } } diff --git a/tutorials/working_with_editor.md b/tutorials/working_with_editor.md index c7f72da69..96ceb3cf5 100644 --- a/tutorials/working_with_editor.md +++ b/tutorials/working_with_editor.md @@ -16,3 +16,35 @@ An instance on IntelliJ IDEA editor is represented by an interface and it's implementation can be found in a class [EditorImpl.java] (https://github.com/JetBrains/intellij-community/blob/master/platform/platform-impl/src/com/intellij/openapi/editor/impl/EditorImpl.java). +##Get an instance of the active editor +A reference to an instance of the editor can be obtained by calling + + CommonDataKeys.EDITOR + +To access the editor instance directly the following ways can be used: + +* If +[DataContext] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/DataContext.java) +object is available +```final Editor editor = [CommonDataKeys].EDITOR.getData(context); +``` +* If +[ActionEvent] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnActionEvent.java) +object is available +```final Editor editor = actionEvent.getData(CommonDataKeys.EDITOR); +``` + +Please see +[EditorIllustration.java] (https://github.com/JetBrains/intellij-sdk/blob/master/code_samples/editor_basics/src/org/jetbrains/plugins/editor/basics/EditorIllustration.java) +for more details. + +---------------- + +See also +[CommonDataKeys.java] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/CommonDataKeys.java), +[DataKey.java] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/DataKey.java) + + + + +