(十四)IntelliJ 插件开发—— Editor Coordinate Systems(编辑器坐标系统)

官方文档

https://www.jetbrains.org/intellij/sdk/docs/tutorials/editor_basics/coordinates_system.html

Github

https://github.com/kungyutucheng/my_gradle_plugin

运行环境

macOS 10.14.5
IntelliJ idea 2019.2.4

效果

光标位置
结果

定义

以编辑器左上角为坐标原点(0,0),向右和向下延伸的坐标体系


坐标体系示例

逻辑坐标(Logical Position)

0为基点,不受Code FoldingSoft Line Wrap影响

视图坐标(Visual Position)

0为基点,受Code FoldingSoft Line Wrap影响

Lean

The Column Position of a caret is the boundary between two characters. A caret can be associated with either a preceding or succeeding character. The association is important in bidirectional text, where mapping from Logical Column Position to Visual Column Position is not continuous.

As defined in the [`LogicalPosition`] class, if a caret position is associated with a succeeding character it *Leans Forward*. Otherwise, it is associated with the preceding character.

As defined in the [`VisualPosition`] class, if a caret position is associated with a succeeding character it *Leans Right*. Otherwise, it is associated with the preceding character.

偏移量(Offset)

0为基点,文档第一个字符开始叠加,无视换行

Demo

EditorAreaIllustration

// Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package com.kungyu.editor.component;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * If conditions support it, makes a menu visible to display information
 * about the caret.
 *
 * @see AnAction
 */
public class EditorAreaIllustration extends AnAction {
  
  /**
   * Displays a message with information about the current caret.
   * @param e  Event related to this action
   */
  @Override
  public void actionPerformed(@NotNull final AnActionEvent e) {
    // Get access to the editor and caret model. update() validated editor's existence.
    final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
    final CaretModel caretModel = editor.getCaretModel();
    // Getting the primary caret ensures we get the correct one of a possible many.
    final Caret primaryCaret = caretModel.getPrimaryCaret();
    // Get the caret information
    LogicalPosition logicalPos = primaryCaret.getLogicalPosition();
    VisualPosition visualPos = primaryCaret.getVisualPosition();
    int caretOffset = primaryCaret.getOffset();
    // Build and display the caret report.
    StringBuilder report = new StringBuilder(logicalPos.toString() + "\n");
    report.append(visualPos.toString() + "\n");
    report.append("Offset: " + caretOffset);
    Messages.showInfoMessage(report.toString(), "Caret Parameters Inside The Editor");
  }
  
  /**
   * Sets visibility and enables this action menu item if:
   *   A project is open,
   *   An editor is active,
   * @param e  Event related to this action
   */
  @Override
  public void update(@NotNull final AnActionEvent e) {
    // Get required data keys
    final Project project = e.getProject();
    final Editor editor = e.getData(CommonDataKeys.EDITOR);
    //Set visibility only in case of existing project and editor
    e.getPresentation().setEnabledAndVisible(project != null && editor != null);
  }
}

注册Action

<action id="com.kungyu.editor.component.EditorAreaIllustration" class="com.kungyu.editor.component.EditorAreaIllustration"
                text="Caret Position" description="Caret Position">
    <add-to-group group-id="EditorPopupMenu" anchor="after" relative-to-action="com.kungyu.editor.component.EditorIllustrationAction"/>
</action>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。