Clean up editor tutorial

This commit is contained in:
Dmitry Jemerov 2018-03-16 16:19:00 +01:00
parent 9527b9a41e
commit 8837d36e1a

View File

@ -18,32 +18,27 @@ public class EditorIllustration extends AnAction {
}
@Override
public void actionPerformed(final AnActionEvent anActionEvent) {
public void actionPerformed(final AnActionEvent e) {
//Get all the required data from data keys
final Editor editor = anActionEvent.getRequiredData(CommonDataKeys.EDITOR);
final Project project = anActionEvent.getRequiredData(CommonDataKeys.PROJECT);
final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
final Project project = e.getRequiredData(CommonDataKeys.PROJECT);
//Access document, caret, and selection
final Document document = editor.getDocument();
final SelectionModel selectionModel = editor.getSelectionModel();
final int start = selectionModel.getSelectionStart();
final int end = selectionModel.getSelectionEnd();
//New instance of Runnable to make a replacement
Runnable runnable = new Runnable() {
@Override
public void run() {
document.replaceString(start, end, "Replacement");
}
};
//Making the replacement
WriteCommandAction.runWriteCommandAction(project, runnable);
WriteCommandAction.runWriteCommandAction(project, () ->
document.replaceString(start, end, "Replacement")
);
selectionModel.removeSelection();
}
@Override
public void update(final AnActionEvent e) {
//Get required data keys
final Project project = e.getData(CommonDataKeys.PROJECT);
final Project project = e.getProject();
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()));