官方文档
https://www.jetbrains.org/intellij/sdk/docs/tutorials/editor_basics/working_with_text.html
Github
https://github.com/kungyutucheng/my_gradle_plugin
运行环境
macOS 10.14.5
IntelliJ idea 2019.2.4
效果
Demo
EditorIllustrationAction
package com.kungyu.editor.component;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Caret;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.kungyu.icons.EditorBasicsIcons;
import org.jetbrains.annotations.NotNull;
/**
* @author wengyongcheng
* @since 2020/3/9 9:29 下午
*/
public class EditorIllustrationAction extends AnAction {
public EditorIllustrationAction() {
super(EditorBasicsIcons.Sdk_default_icon);
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
final Project project = e.getRequiredData(CommonDataKeys.PROJECT);
final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
final Document document = editor.getDocument();
// 获取选中文本起始索引和结束索引
Caret primaryCaret = editor.getCaretModel().getPrimaryCaret();
int start = primaryCaret.getSelectionStart();
int end = primaryCaret.getSelectionEnd();
// 执行文本替换
WriteCommandAction.runWriteCommandAction(project,() ->
document.replaceString(start,end,"editor_basics")
);
// 对替换后的文本移除选中效果
primaryCaret.removeSelection();
}
@Override
public void update(@NotNull AnActionEvent e) {
Project project = e.getProject();
Editor editor = e.getData(CommonDataKeys.EDITOR);
e.getPresentation().setEnabledAndVisible(project != null && editor != null
&& editor.getSelectionModel().hasSelection());
}
}
editor支持以下几种数据模型:
-
SelectionModel
:支持Document
文本的范围选择,并且检索被选择内容的信息
/**
* Returns the selection model for the editor, which can be used to select ranges of text in
* the document and retrieve information about the selection.
* <p>
* To query or change selections for specific carets, {@link CaretModel} interface should be used.
*
* @return the selection model instance.
* @see #getCaretModel()
*/
@NotNull
SelectionModel getSelectionModel();
-
CaretModel
:支持对editor
添加或者移除carets
(引入符号),并且可以查询或者更新carets
的选择部分的索引
/**
* Returns the caret model for the document, which can be used to add and remove carets to the editor, as well as to query and update
* carets' and corresponding selections' positions.
*
* @return the caret model instance.
*/
@NotNull
CaretModel getCaretModel();
-
FoldingModel
:折叠模型,支持新增、移除、展开和收缩已折叠区域
/**
* Returns the folding model for the document, which can be used to add, remove, expand
* or collapse folded regions in the document.
*
* @return the folding model instance.
*/
@NotNull
FoldingModel getFoldingModel();
-
IndentsModel
:缩进模型,2019.2.4版本源码不存在该Model -
ScrollingModel
:滚动模型,支持滚动Doucment
和检索滚动条的当前位置的信息
/**
* Returns the scrolling model for the document, which can be used to scroll the document
* and retrieve information about the current position of the scrollbars.
*
* @return the scrolling model instance.
*/
@NotNull
ScrollingModel getScrollingModel();
-
SoftWrapModel
:支持获取编辑器已注册的soft wrap
的信息,并提供基础的管理方法
/**
* Returns the soft wrap model for the document, which can be used to get information about soft wraps registered
* for the editor document at the moment and provides basic management functions for them.
*
* @return the soft wrap model instance.
*/
@NotNull
SoftWrapModel getSoftWrapModel();
soft wrap:适应窗口宽度的自动换行模式,否则出现滚动条
EditorBasicsIcons
package com.kungyu.icons;
import com.intellij.openapi.util.IconLoader;
import javax.swing.*;
public class EditorBasicsIcons {
public static final Icon Sdk_default_icon = IconLoader.getIcon("/icons/sdk_16.svg");
}
注册Action
<action id="com.kungyu.editor.component.EditorIllustrationAction" class="com.kungyu.editor.component.EditorIllustrationAction"
text="Editor Replace Text" description="Replaces selected text with 'Replacement'.">
<add-to-group group-id="EditorPopupMenu" anchor="first"/>
</action>