mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-30 18:27:49 +08:00
Clean up editor tutorial
This commit is contained in:
parent
e95b058179
commit
519b4c946e
@ -66,11 +66,11 @@
|
||||
* [Virtual Files](basics/architectural_overview/virtual_file.md)
|
||||
* [Documents](basics/architectural_overview/documents.md)
|
||||
* [Editors](reference_guide/editors.md)
|
||||
* [Multiple Carets](reference_guide/multiple_carets.md)
|
||||
* [Editor Basics](tutorials/editor_basics.md)
|
||||
* [1. Working With Text](tutorials/editor_basics/working_with_text.md)
|
||||
* [2. Editor Coordinates System. Positions And Offsets](tutorials/editor_basics/coordinates_system.md)
|
||||
* [3. Handling Editor Events](tutorials/editor_basics/editor_events.md)
|
||||
* [Multiple Carets](reference_guide/multiple_carets.md)
|
||||
* [Run Configurations](basics/run_configurations.md)
|
||||
* [Run Configuration Management](basics/run_configurations/run_configuration_management.md)
|
||||
* [Execution](basics/run_configurations/run_configuration_execution.md)
|
||||
|
@ -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()));
|
||||
|
@ -2,4 +2,5 @@
|
||||
title: Editors
|
||||
---
|
||||
|
||||
* [Editor basics tutorial](/tutorials/editor_basics.md)
|
||||
* [Multiple carets](multiple_carets.md)
|
||||
|
@ -7,8 +7,8 @@ title: Basics of working with the Editor
|
||||
|
||||
----------
|
||||
|
||||
This tutorial will lead you through the series of steps showing how to work with the IntelliJ Platform Editor, how access and modify text it contains,
|
||||
and how to handle events sent to the Editor.
|
||||
This tutorial will lead you through the series of steps showing how to work with the IntelliJ Platform Editor, how to access and modify text it contains,
|
||||
and how to handle events sent to the editor.
|
||||
|
||||
* [1. Working With Text](editor_basics/working_with_text.md)
|
||||
* [2. Editor coordinates system. Positions and offsets](editor_basics/coordinates_system.md)
|
||||
|
@ -9,8 +9,8 @@ The following set of steps will show how to access a text selection and change i
|
||||
|
||||
### 1.1.1 Creating a new action
|
||||
|
||||
In this example access to the Editor is made through an action as a plug-in point.
|
||||
To create an action we need derive
|
||||
In this example we access the editor from an action.
|
||||
To create an action we need to extend the
|
||||
[AnAction.java](upsource:///platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java)
|
||||
class.
|
||||
|
||||
@ -23,7 +23,7 @@ public class EditorIllustration extends AnAction {
|
||||
|
||||
### 1.1.2. Registering an action
|
||||
|
||||
To register the action we should add a corresponding attribute to the `<actions>` section of the plugin configuration file
|
||||
To register the action we should add the corresponding tag to the `<actions>` section of the plugin configuration file
|
||||
[plugin.xml](https://github.com/JetBrains/intellij-sdk-docs/blob/master/code_samples/editor_basics/resources/META-INF/plugin.xml)
|
||||
|
||||
|
||||
@ -43,12 +43,12 @@ it will be available from the context menu when the focus is located in the edit
|
||||
### 1.1.3. Defining action's visibility
|
||||
|
||||
To determine conditions by which the action will be visible and available for being executed we need to override it's
|
||||
*public void update(final AnActionEvent e)* method.
|
||||
*public void update(AnActionEvent e)* method.
|
||||
|
||||
```java
|
||||
public class EditorIllustration extends AnAction {
|
||||
@Override
|
||||
public void update(final AnActionEvent e) {
|
||||
public void update(AnActionEvent e) {
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -59,57 +59,55 @@ If we want to work with a selected part of the text, it's reasonable to make the
|
||||
* There is an instance of the Editor available
|
||||
* There is a text selection in the Editor
|
||||
|
||||
Further steps will show how to check these conditions through obtaining instances of Project and Editor and how to set up a desired level of action's visibility.
|
||||
Further steps will show how to check these conditions through obtaining instances of Project and Editor and how to show or hide the action based on them.
|
||||
|
||||
## 1.2. Getting an instance of the Active Editor
|
||||
## 1.2. Getting an instance of the active Editor
|
||||
|
||||
A reference to an instance of the Editor can be obtained by calling `CommonDataKeys.EDITOR`,
|
||||
obtaining a project reference is performed the same way `CommonDataKeys.PROJECT`.
|
||||
A reference to an instance of the Editor can be obtained by calling `getData(CommonDataKeys.EDITOR)`.
|
||||
To obtain a project reference, we use the `getProject()` method.
|
||||
|
||||
|
||||
```java
|
||||
public class EditorIllustration extends AnAction {
|
||||
@Override
|
||||
public void update(final AnActionEvent e) {
|
||||
public void update(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
|
||||
e.getPresentation().setVisible((project != null && editor != null));
|
||||
e.getPresentation().setVisible(project != null && editor != null);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Note:**
|
||||
|
||||
To access an Editor instance also can be used other ways:
|
||||
To access an Editor instance other ways can also be used:
|
||||
|
||||
* If [DataContext](upsource:///platform/editor-ui-api/src/com/intellij/openapi/actionSystem/DataContext.java)
|
||||
object is available `final Editor editor = CommonDataKeys.EDITOR.getData(context);`
|
||||
|
||||
* If [ActionEvent](upsource:///platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnActionEvent.java)
|
||||
object is available `final Editor editor = actionEvent.getData(CommonDataKeys.EDITOR);`
|
||||
object is available: `final Editor editor = CommonDataKeys.EDITOR.getData(context);`
|
||||
|
||||
* If only a `Project` is available, you can use `FileEditorManager.getInstance(project).getSelectedTextEditor()`
|
||||
|
||||
|
||||
## 1.3. Obtaining a caret model and selection
|
||||
|
||||
After making sure we have a project open and an instance of the Editor we need to check if any selection is available and set action's visibility accordingly to these conditions.
|
||||
[SelectionModel](upsource:///platform/editor-ui-api/src/com/intellij/openapi/editor/SelectionModel.java)
|
||||
got from the Editor allows to do it by calling it's `hasSelection()` method.
|
||||
Here's how our `update(final AnActionEvent e)` method should look like at the end:
|
||||
accessed from the Editor allows to do it by calling its `hasSelection()` method.
|
||||
Here's how our `update(AnActionEvent e)` method should look like at the end:
|
||||
|
||||
|
||||
```java
|
||||
public class EditorIllustration extends AnAction {
|
||||
@Override
|
||||
public void update(final AnActionEvent e) {
|
||||
public void update(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()));
|
||||
e.getPresentation().setVisible(project != null && editor != null &&
|
||||
editor.getSelectionModel().hasSelection());
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -134,34 +132,34 @@ package and include:
|
||||
|
||||
The action is visible and available now.
|
||||
In order to make it do something we need to override it's
|
||||
`public void actionPerformed(final AnActionEvent anActionEvent)` method.
|
||||
`public void actionPerformed(AnActionEvent anActionEvent)` method.
|
||||
|
||||
|
||||
```java
|
||||
public class EditorIllustration extends AnAction {
|
||||
@Override
|
||||
public void update(final AnActionEvent e) {
|
||||
public void update(AnActionEvent e) {
|
||||
//code here
|
||||
}
|
||||
@Override
|
||||
public void actionPerformed(final AnActionEvent anActionEvent) {
|
||||
public void actionPerformed(AnActionEvent anActionEvent) {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
To modify the text an instance of the
|
||||
[Document](upsource:///platform/core-api/src/com/intellij/openapi/editor/Document.java)
|
||||
needs to be accessed. Document represents the contents of a text file loaded into memory, and possibly opened in an IDEA text editor.
|
||||
The instance of a Document will be use later when a text replacement is performed.
|
||||
needs to be accessed. [Document](/basics/architectural_overview/documents.md) represents the contents of a text file loaded into memory, and possibly opened in an IDEA text editor.
|
||||
The instance of a Document will be used later when a text replacement is performed.
|
||||
We also need to figure out where the selected part of the text is located.
|
||||
|
||||
|
||||
```java
|
||||
@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.getProject();
|
||||
//Access document, caret, and selection
|
||||
final Document document = editor.getDocument();
|
||||
final SelectionModel selectionModel = editor.getSelectionModel();
|
||||
@ -174,29 +172,26 @@ public void actionPerformed(final AnActionEvent anActionEvent) {
|
||||
|
||||
Generally replacement can be done by calling
|
||||
`void replaceString(int startOffset, int endOffset, @NotNull CharSequence s);` of the Document, however,
|
||||
the operation of replacement must be executed safely, this means the Document must be locked and any changes should be performed under the [write action](upsource:///platform/core-api/src/com/intellij/openapi/command/WriteCommandAction.java)<!--#L172-->. See the [Threading Issues](/basics/architectural_overview/general_threading_rules.md) section to learn more about synchronization issues and changes safety on the IntelliJ Platform.
|
||||
the operation of replacement must be executed safely, this means the Document must be locked and
|
||||
any changes should be performed under the [write action](upsource:///platform/core-api/src/com/intellij/openapi/command/WriteCommandAction.java)<!--#L172-->.
|
||||
See the [Threading Issues](/basics/architectural_overview/general_threading_rules.md) section to learn more about synchronization issues and changes safety on the IntelliJ Platform.
|
||||
|
||||
```java
|
||||
@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.getProject();
|
||||
//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();
|
||||
}
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user