Extensive cleanup

This commit is contained in:
JohnHake 2019-08-02 12:26:58 -07:00
parent 1d96680729
commit ad74c65a3c
4 changed files with 104 additions and 41 deletions

View File

@ -10,31 +10,42 @@ import com.intellij.openapi.ui.Messages;
import java.util.List; import java.util.List;
/** /**
* If conditions support it, makes a menu visible to display information
* about the caret.
* @author Anna Bulenkova * @author Anna Bulenkova
* @see com.intellij.openapi.actionSystem.AnAction
*/ */
public class EditorAreaIllustration extends AnAction { public class EditorAreaIllustration extends AnAction {
/**
* Displays a message with information about the current caret.
* @param e Event related to this action
*/
@Override @Override
public void actionPerformed(AnActionEvent anActionEvent) { public void actionPerformed(AnActionEvent e) {
final Editor editor = anActionEvent.getRequiredData(CommonDataKeys.EDITOR); // Get access to the editor and caret model. update() validated editor's existence.
final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
CaretModel caretModel = editor.getCaretModel(); CaretModel caretModel = editor.getCaretModel();
boolean caretModelUpToDate = caretModel.isUpToDate(); // Getting the primary caret ensures we get the right one of a possible many.
Caret primaryCaret = caretModel.getPrimaryCaret(); Caret primaryCaret = caretModel.getPrimaryCaret();
boolean primaryCaretIsValid = primaryCaret.isValid(); // Build and display the caret report.
StringBuilder report = new StringBuilder(); StringBuilder report = new StringBuilder();
if ( caretModelUpToDate && primaryCaretIsValid ) { report.append(primaryCaret.getLogicalPosition().toString() + "\n");
report.append(primaryCaret.getLogicalPosition().toString() + "\n" ); report.append(primaryCaret.getVisualPosition().toString() + "\n");
report.append(primaryCaret.getVisualPosition().toString() + "\n" ); report.append("Offset: " + primaryCaret.getOffset());
report.append("Offset: " + primaryCaret.getOffset() ); Messages.showInfoMessage(report.toString(), "Caret Parameters Inside The Editor");
} else {
report.append("Caret model up to date: " + caretModelUpToDate + "\n" + "Primary Caret is valid: " + primaryCaretIsValid);
}
Messages.showInfoMessage(report.toString(),"Caret Parameters Inside The Editor");
} }
/**
* Sets visibility of this action menu item if:
* A project is open,
* An editor is active,
* @param e Event related to this action
*/
@Override @Override
public void update(AnActionEvent e) { public void update(AnActionEvent e) {
//Get required data keys //Get required data keys
final Project project = e.getData(CommonDataKeys.PROJECT); final Project project = e.getProject();
final Editor editor = e.getData(CommonDataKeys.EDITOR); final Editor editor = e.getData(CommonDataKeys.EDITOR);
//Set visibility only in case of existing project and editor //Set visibility only in case of existing project and editor
e.getPresentation().setVisible(project != null && editor != null); e.getPresentation().setVisible(project != null && editor != null);

View File

@ -9,24 +9,59 @@ import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* Menu action to clone a new caret based on an existing one.
* @author Anna Bulenkova * @author Anna Bulenkova
* @see com.intellij.openapi.actionSystem.AnAction
*/ */
public class EditorHandlerIllustration extends AnAction { public class EditorHandlerIllustration extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent anActionEvent) { /**
final Editor editor = anActionEvent.getRequiredData(CommonDataKeys.EDITOR); * This block of static code does not pertain to this class.
EditorActionManager actionManager = EditorActionManager.getInstance(); * It registers the custom MyTypedHandler, an TypedActionHandler
//Insert one more caret below the active caret * that handles actions activated by typing in the editor.
EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_CLONE_CARET_BELOW); * This registration code just needs to appear in a class (like AnAction class)
actionHandler.execute(editor, editor.getCaretModel().getCurrentCaret(), anActionEvent.getDataContext()); * that gets instantiated as part of IntelliJ startup.
*/
static {
final EditorActionManager actionManager = EditorActionManager.getInstance();
final TypedAction typedAction = actionManager.getTypedAction();
typedAction.setupHandler(new MyTypedHandler());
} }
/**
* Clones a new caret at a higher Logical Position line number.
* @param e Event related to this action
*/
@Override @Override
public void update(@NotNull final AnActionEvent anActionEvent) { public void actionPerformed(@NotNull AnActionEvent e) {
//Set visible if at least one caret is available // Editor is known to exist from update, so it's not null
final Project project = anActionEvent.getData(CommonDataKeys.PROJECT); final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
final Editor editor = anActionEvent.getData(CommonDataKeys.EDITOR); // Get the action manager in order to get the necessary action handler...
anActionEvent.getPresentation() EditorActionManager actionManager = EditorActionManager.getInstance();
.setVisible((project != null && editor != null && !editor.getCaretModel().getAllCarets().isEmpty())); // Get the action handler registered to clone carets
EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_CLONE_CARET_BELOW);
// Clone one caret below the active caret
actionHandler.execute(editor, editor.getCaretModel().getPrimaryCaret(), e.getDataContext());
} }
/**
* Sets visibility of this action menu item if:
* A project is open,
* An editor is active,
* At least one caret exists
* @param e Event related to this action
*/
@Override
public void update(@NotNull final AnActionEvent e) {
boolean visibility = false;
//Set visible if at least one caret is available
final Project project = e.getProject();
final Editor editor = e.getData(CommonDataKeys.EDITOR);
if (editor != null && project != null) {
// Ensure the list of carets in the editor is not empty
visibility = !editor.getCaretModel().getAllCarets().isEmpty();
}
e.getPresentation().setVisible(visibility);
}
} }

View File

@ -9,41 +9,49 @@ import com.intellij.openapi.editor.actionSystem.*;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
/** /**
* Menu action to replace a selection of characters with a fixed string.
* @author Anna Bulenkova * @author Anna Bulenkova
* @see com.intellij.openapi.actionSystem.AnAction
*/ */
public class EditorIllustration extends AnAction { public class EditorIllustration extends AnAction {
static { /**
final EditorActionManager actionManager = EditorActionManager.getInstance(); * Replaces the run of text selected by the primary caret with a fixed string.
final TypedAction typedAction = actionManager.getTypedAction(); * @param e Event related to this action
typedAction.setupHandler(new MyTypedHandler()); */
}
@Override @Override
public void actionPerformed(final AnActionEvent e) { public void actionPerformed(final AnActionEvent e) {
//Get all the required data from data keys //Get all the required data from data keys
// Editor and Project were verified in update(), so they are not null.
final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR); final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
final Project project = e.getRequiredData(CommonDataKeys.PROJECT); final Project project = e.getRequiredData(CommonDataKeys.PROJECT);
//Access document, caret, and selection
final Document document = editor.getDocument(); final Document document = editor.getDocument();
// Get information about the selection // Work off of the primary caret to get the selection info
final SelectionModel selectionModel = editor.getSelectionModel(); Caret primaryCaret = editor.getCaretModel().getPrimaryCaret();
final int start = selectionModel.getSelectionStart(); int start = primaryCaret.getSelectionStart();
final int end = selectionModel.getSelectionEnd(); int end = primaryCaret.getSelectionEnd();
//Make the replacement with the name of this plugin // Replace the selection with a fixed string.
// Must do this document change in a write action context.
WriteCommandAction.runWriteCommandAction(project, () -> WriteCommandAction.runWriteCommandAction(project, () ->
document.replaceString(start, end, "Replaced by editor_basics") document.replaceString(start, end, "editor_basics")
); );
// De-select the text range that was just replaced // De-select the text range that was just replaced
selectionModel.removeSelection(); primaryCaret.removeSelection();
} }
/**
* Sets visibility of this action menu item if:
* A project is open,
* An editor is active,
* Some characters are selected
* @param e Event related to this action
*/
@Override @Override
public void update(final AnActionEvent e) { public void update(final AnActionEvent e) {
//Get required data keys //Get required data keys
final Project project = e.getProject(); final Project project = e.getProject();
final Editor editor = e.getData(CommonDataKeys.EDITOR); 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 //Set visibility only in case of existing project and editor and if a selection exists
e.getPresentation().setVisible((project != null && editor != null && editor.getSelectionModel().hasSelection())); e.getPresentation().setVisible((project != null && editor != null && editor.getSelectionModel().hasSelection()));
} }
} }

View File

@ -10,14 +10,23 @@ import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
/** /**
* This is a custom TypedActionHandler that handles actions activated
* keystrokes in the editor.
* The execute method inserts a fixed string at Offset 0 of the document.
* Document changes are made in the context of a write action.
* MyTypedHandler is registered by static code in the EditorHandlerIllustration class.
* @author Anna Bulenkova * @author Anna Bulenkova
* @see com.intellij.openapi.editor.actionSystem.TypedActionHandler
*/ */
class MyTypedHandler implements TypedActionHandler { class MyTypedHandler implements TypedActionHandler {
@Override @Override
public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext) { public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext) {
// Get the document and project
final Document document = editor.getDocument(); final Document document = editor.getDocument();
Project project = editor.getProject(); Project project = editor.getProject();
Runnable runnable = () -> document.insertString(0, "Inserted by editor_basics\n"); // Construct the runnable to substitute the string at offset 0 in the document
Runnable runnable = () -> document.insertString(0, "editor_basics\n");
// Make the document change in the context of a write action.
WriteCommandAction.runWriteCommandAction(project, runnable); WriteCommandAction.runWriteCommandAction(project, runnable);
} }
} }