在Eclipse中的自定义插件中触发格式化程序

时间:2012-11-22 19:21:41

标签: eclipse eclipse-plugin formatter

我为eclipse编写自己的文本编辑器插件。我现在正在研究自己的格式化程序。实际上,遵循该链接http://wiki.eclipse.org/FAQ_How_do_I_support_formatting_in_my_editor%3F。 我编写了我的策略,我在SourceViewerConfiguration中覆盖了getContentFormatter ..

当我运行我的插件并按Ctrl + Shift + F时 - 没有任何反应。

我认为我在这里错过了一步。我应该创建一个处理程序还是什么?

由于

2 个答案:

答案 0 :(得分:1)

你可能跳过了链接页面的最后一部分吗?

  

最后,您需要创建一个调用格式化程序的操作。文本基础结构没有定义通用格式化操作,但创建自己的一个很容易。 action的run方法可以简单地在源查看器上调用以下内容来调用formatter:

sourceViewer.doOperation(ISourceViewer.FORMAT);

答案 1 :(得分:1)

是什么帮助了我。我用以下执行器体创建了一个处理程序:

//get the editorPart    
if (editorPart != null) {
                ITextOperationTarget target = (ITextOperationTarget) editorPart
                        .getAdapter(ITextOperationTarget.class);
                if (target instanceof ISourceViewer) {
                    ISourceViewer textViewer = (ISourceViewer) target;
                    ((ITextOperationTarget) textViewer)
                            .doOperation(ISourceViewer.FORMAT);
                }
            }

然后只创建菜单项并将它们绑定到处理程序。