【发布时间】:2018-10-14 16:48:07
【问题描述】:
我正在尝试在我们的自定义编辑器中引入一个快捷键(Ctrl+Shift+f)来格式化内容。
我已经实现了以下更改。
通过添加带有定义 Id/schema/context 的键扩展来添加对插件 xml 的更改。
-
通过扩展TextEditorAction类实现Action如下。
@Override public void run() { this.doOperation(ISourceViewer.FORMAT); } 通过实现
IContentFormatter实现了一个格式化程序类。通过覆盖
getContentFormatter将上述 Formatter 类传递给我们切割的sourceVIewConfiguration(扩展SourceViewerConfiguration)类。在我们的自定义编辑器类中重写了
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