【问题标题】:Binding keys using org.eclipse.ui.binding使用 org.eclipse.ui.binding 绑定键
【发布时间】:2018-10-14 16:48:07
【问题描述】:

我正在尝试在我们的自定义编辑器中引入一个快捷键(Ctrl+Shift+f)来格式化内容。

我已经实现了以下更改。

  1. 通过添加带有定义 Id/schema/context 的键扩展来添加对插件 xml 的更改。

  2. 通过扩展TextEditorAction类实现Action如下。

    @Override
    public void run() {
        this.doOperation(ISourceViewer.FORMAT);
    }
    
  3. 通过实现IContentFormatter 实现了一个格式化程序类。

  4. 通过覆盖 getContentFormatter 将上述 Formatter 类传递给我们切割的 sourceVIewConfiguration(扩展 SourceViewerConfiguration)类。

  5. 在我们的自定义编辑器类中重写了createActions() API,该类扩展了TextEditor

由于某种原因,我的快捷键不起作用。我在我的动作类中放置了一个调试点,并注意到当我按下快捷键时控制器没有去那里。

我还注意到新创建的密钥没有显示在首选项 -> 密钥列表下。

有人可以提供解决问题的指针或示例吗?

plugin.xml 条目:

 <key
            commandId="com.language.javascripteditor.XJSFormatAction"
            schemeId="myScheme"
            sequence="M1+M2+z"/>
      <scheme
            id="myScheme"
            name="myScheme">
      </scheme>

格式化程序类:

public class JavaScriptEditorFormatter implements IContentFormatter {

    @Override
    public void format(IDocument document, IRegion region) {
        try {
            String content =document.get(region.getOffset(), region.getLength());
            String formatted = new JSBeautifier().js_beautify(content,null);
            document.replace(region.getOffset(), region.getLength(), formatted);
        } catch (BadLocationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

    @Override
    public IFormattingStrategy getFormattingStrategy(String contentType) {
        throw new UnsupportedOperationException();
    }

}

为自定义模式添加了一个新的属性文件,名称为 plugin_customization.ini,内容如下 org.eclipse.ui/KEY_CONFIGURATION_ID=myScheme

plugin.xml 中的命令部分

<command
            defaultHandler="com.cisco.nm.workflowbuilder.language.javascripteditor.XJSFormatAction"
    id="com.language.javascripteditor.XJSFormatAction"
    name="%action.label.format.xjs">
</command>

我写了一个Action 类,而不是一个处理程序。如果这种方法不起作用,请告诉我

【问题讨论】:

  • 向我们展示 plugin.xml 中的 key 绑定。它使用什么上下文?什么命令ID?你是如何定义命令 id 和处理程序的?
  • edit您在添加额外信息时提出问题。命令com.cisco.nm.workflowbuilder.language.javascripteditor.XJSFormatAction 是如何定义的 - 向我们展示org.eclipse.ui.commands 定义,向我们展示处理程序的org.eclipse.ui.handlers 定义。使用不同的方案意味着您必须激活它。您更有可能需要在编辑器中激活的上下文 ID,
  • 我正在使用 org.eclipse.ui.binding 和 Action 类来实现,我没有使用处理程序方法
  • 键绑定仅适用于无法直接执行操作的命令和处理程序。我建议你看看org.eclipse.jdt.ui 插件,看看它是如何做到的。

标签: eclipse-plugin eclipse-rcp


【解决方案1】:

1.为现有编辑器的扩展添加贡献者类。 2.创建带有格式ID的命令。例如:com.javascript.text.format 3.用格式化方法编写Action类

@Override
    public void run() {
        this.doOperation(ISourceViewer.FORMAT);
    }

4。插件 xml 条目

  <key
            commandId="com.javascript.text.format"
            schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
            sequence="M1+M2+F"/>

5。重写 createActions() 。在此方法中实例化了 Action 类和 setActionDefinitionId。

【讨论】:

  • 但我遇到了新问题。已经有一个具有相同键盘命令(ctrl+shift+F)的键控。现在我需要在打开此编辑器时覆盖该命令。基本上需要指针来解决冲突。我尝试了不同的 schemaId,但这对我没有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
相关资源
最近更新 更多