(十三)IntelliJ 插件开发—— Working With Text(文本处理)

官方文档

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>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 不知不觉,我们进入了微时代。这是一个分享的时代,是一个万象包罗的时代。 每天我们在微信上和朋友聊天,...
    飞飞的小幸福阅读 543评论 3 2
  • 幸福是需要修出来的~每天进步1%~幸福实修14班~ 金包玉组D3(3/60) 【幸福实修目标】本周目标一:健康 1...
    蒋莺华阅读 181评论 0 1
  • 面对任何事情 都是有原因结果的 不论是天意还是人为 自虐哭泣只能给自己添加更多不堪与凄惨 既然已然如此 那就...
    世界小盆友阅读 330评论 0 0