mirror of
https://github.com/JetBrains/intellij-sdk-code-samples.git
synced 2025-07-28 01:07:49 +08:00
[md] cosmetics
This commit is contained in:
parent
f6708a26fb
commit
07c90669b3
@ -1,4 +1,4 @@
|
||||
Basics of working with the Editor.
|
||||
Basics of working with the Editor
|
||||
===========
|
||||
|
||||
### [Source code] (https://github.com/JetBrains/intellij-sdk/tree/master/code_samples/editor_basics)
|
||||
@ -8,12 +8,12 @@ Basics of working with the Editor.
|
||||
This tutorial will lead you through the series of steps showing how to work with the Editor in IntelliJ IDEA, how access and modify text it contains,
|
||||
and how to handle events sent to the Editor.
|
||||
|
||||
#Editor. Working with text.
|
||||
#Working with text
|
||||
The following set of steps will show how to access a text selection and change it.
|
||||
|
||||
##Prerequirements.
|
||||
##Prerequirements
|
||||
|
||||
###Creating a new action.
|
||||
###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
|
||||
[AnAction.java] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/actionSystem/AnAction.java)
|
||||
@ -22,7 +22,7 @@ class.
|
||||
public class EditorIllustration extends AnAction {
|
||||
}
|
||||
|
||||
###Registering an action.
|
||||
###Registering an action
|
||||
To register the action we should add a corresponding attribute to the *<actions>* section of the plugin configuration file
|
||||
[plugin.xml] (https://github.com/JetBrains/intellij-sdk/blob/master/code_samples/editor_basics/resources/META-INF/plugin.xml)
|
||||
|
||||
@ -35,7 +35,7 @@ To register the action we should add a corresponding attribute to the *<actions>
|
||||
If an action is registered in the group EditorPopupMenu, like the sample above shows,
|
||||
it will be available from the context menu when the focus is located in the editor.
|
||||
|
||||
###Defining action's visibility.
|
||||
###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.
|
||||
|
||||
@ -53,7 +53,7 @@ If we want to work with a selected part of the text, it's reasonable to make the
|
||||
|
||||
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.
|
||||
|
||||
##Getting an instance of the Active Editor.
|
||||
##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```.
|
||||
|
||||
@ -81,7 +81,7 @@ object is available ```final Editor editor = actionEvent.getData(CommonDataKeys.
|
||||
|
||||
------------------
|
||||
|
||||
##Obtaining a caret model and selection.
|
||||
##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] () 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:
|
||||
@ -115,7 +115,7 @@ package and include:
|
||||
|
||||
------------
|
||||
|
||||
##Obtainitg a Document.
|
||||
##Obtainitg a Document
|
||||
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.
|
||||
|
||||
@ -147,7 +147,7 @@ We also need to figure out where the selected part of the text is located.
|
||||
final int end = selectionModel.getSelectionEnd();
|
||||
}
|
||||
|
||||
##Modifying text.
|
||||
##Modifying text
|
||||
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 mean the Document must be locked and any changes should be performed under the
|
||||
@ -191,15 +191,15 @@ To see how text replacement works, check out
|
||||
[Editor Basics] (https://github.com/JetBrains/intellij-sdk/blob/master/code_samples/editor_basics/src/org/jetbrains/plugins/editor/basics/)
|
||||
plugin, make the project, and run it, then invoke the *EditorIllustration* action which is available in the context menu of the editor.
|
||||
|
||||
#Editor coordinates system. Positions and offsets.
|
||||
#Editor coordinates system. Positions and offsets
|
||||
Every caret in the editor has a set of properties describing it's coordinates. These properties can be accessed by obtaining a
|
||||
[caret model instance] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/editor/CaretModel.java).
|
||||
Working with caret positions and it's logical and visual properties will be explained in the sample below.
|
||||
|
||||
##Prerequirements.
|
||||
##Prerequirements
|
||||
Access to the Editor is performed through an action.
|
||||
|
||||
##Accessing caret positions.
|
||||
##Accessing caret positions
|
||||
To get an access to caret positions an instance of CaretModel should be obtained.
|
||||
|
||||
public class EditorAreaIllustration extends AnAction {
|
||||
@ -215,7 +215,7 @@ To get an access to caret positions an instance of CaretModel should be obtained
|
||||
}
|
||||
}
|
||||
|
||||
##Logical position.
|
||||
##Logical position
|
||||
[LogicalPosition.java] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/editor/LogicalPosition.java)
|
||||
represents a line and a column of the current logical position of the caret. Logical positions ignore folding -
|
||||
for example, if the top 10 lines of the document are folded, the 10th line in the document will have the line number 10 in its logical position.
|
||||
@ -240,7 +240,7 @@ Rationale is that single logical pair matches soft wrap-introduced virtual space
|
||||
correspond to the same logical position. It's convenient to store exact visual location details within the logical
|
||||
position in order to relief further 'logical position' -> 'visual position' mapping.
|
||||
|
||||
##Visual position.
|
||||
##Visual position
|
||||
[VisualPosition.java] (https://github.com/JetBrains/intellij-community/blob/master/platform/editor-ui-api/src/com/intellij/openapi/editor/VisualPosition.java)
|
||||
represent a visual position and may very from the corresponding logical position.
|
||||
Visual positions take folding into account - for example,
|
||||
@ -261,7 +261,7 @@ if the top 10 lines of the document are folded, the 10th line in the document wi
|
||||
}
|
||||
}
|
||||
|
||||
##Offset.
|
||||
##Offset
|
||||
An absolute offset for a given caret position is accessible through CaretModel as well
|
||||
|
||||
public class EditorAreaIllustration extends AnAction {
|
||||
@ -280,7 +280,7 @@ An absolute offset for a given caret position is accessible through CaretModel a
|
||||
}
|
||||
}
|
||||
|
||||
##Displaying position values.
|
||||
##Displaying position values
|
||||
To display the actual values of logical anf visual positions we add an
|
||||
```Messages.showInfoMessage()``` call that will show them in form of notification after the action is performed.
|
||||
|
||||
@ -315,16 +315,16 @@ Find the action in the context menu:
|
||||
Perform the action to see caret positions:
|
||||

|
||||
|
||||
#Actions activated by editor events.
|
||||
#Actions activated by editor events
|
||||
IntelliJ IDEA SDK provides a set of embedded mechanisms for handling events related to the Editor.
|
||||
|
||||
##Handling keystrokes in the Editor.
|
||||
##Handling keystrokes in the Editor
|
||||
To handle keystrokes and provide custom reactions interface
|
||||
[TypedActionHandler]()
|
||||
may be used.
|
||||
Series of steps below shows how to change standard behaviour of the editor and make it react on typing differently instead of simply displaying a typed character in the editor area.
|
||||
|
||||
###Implementing *TypedActionHandler*.
|
||||
###Implementing *TypedActionHandler*
|
||||
First we need to implement an instance of
|
||||
[TypedActionHandler]():
|
||||
|
||||
@ -334,7 +334,7 @@ First we need to implement an instance of
|
||||
}
|
||||
}
|
||||
|
||||
###Implementing logic for handling keystrokes.
|
||||
###Implementing logic for handling keystrokes
|
||||
```public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext);```
|
||||
method should contain the main logical part for handling keystrokes. It will be called every time a key is pressed.
|
||||
In the following example our typed handler is meant insert a string at the zero offset in the editor after a keystroke occurs:
|
||||
@ -354,7 +354,7 @@ In the following example our typed handler is meant insert a string at the zero
|
||||
}
|
||||
}
|
||||
|
||||
###Setting up *TypedActionHandler*.
|
||||
###Setting up *TypedActionHandler*
|
||||
|
||||
To enable a custom implementation of *TypedActionHandler* in the plugin we need to create a new instance of it and pass to
|
||||
```public TypedActionHandler setupHandler(TypedActionHandler handler);``` method of the
|
||||
@ -371,14 +371,14 @@ class. By doing it we replace the typing handler with the specified handler.
|
||||
|
||||
After compiling and running the code snippet above typing in the editor will be handled with inserting an extra string at the 0 position.
|
||||
|
||||
##Working with EditorActionHandler.
|
||||
##Working with EditorActionHandler
|
||||
Class
|
||||
[EditorActionHandler.java] (https://github.com/JetBrains/intellij-community/blob/master/platform/platform-api/src/com/intellij/openapi/editor/actionSystem/EditorActionHandler.java)
|
||||
stays for actions activated by keystrokes in the editor.
|
||||
Series of steps below show how access *EditorActionManager* and pass it actions to be executed.
|
||||
In this example we will use *EditorActionHandler* to insert one extra caret above the current caret if available.
|
||||
|
||||
###Prerequirements.
|
||||
###Prerequirements
|
||||
Create an action:
|
||||
|
||||
public class EditorHandlerIllustration extends AnAction {
|
||||
@ -400,7 +400,7 @@ Register action in
|
||||
</action>
|
||||
</action>
|
||||
|
||||
###Setting visibility.
|
||||
###Setting visibility
|
||||
Our action should be visible only in case if the following conditions are met:
|
||||
there's a project open, there's an editor available, and there's at least one caret active in the editor:
|
||||
|
||||
@ -416,7 +416,7 @@ there's a project open, there's an editor available, and there's at least one ca
|
||||
}
|
||||
}
|
||||
|
||||
###Obtaining *EditorActionHandler*.
|
||||
###Obtaining *EditorActionHandler*
|
||||
|
||||
To manipulate with standard Editor's actions first we need to obtain
|
||||
an instance of
|
||||
@ -437,7 +437,7 @@ an instance of
|
||||
}
|
||||
}
|
||||
|
||||
###Making *EditorActionHandler* execute actions.
|
||||
###Making *EditorActionHandler* execute actions
|
||||
To execute an action we need to call the ```public final void execute(@NotNull Editor editor, @Nullable final Caret contextCaret, final DataContext dataContext);```
|
||||
method of a corresponding *EditorActionHandler*
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user