bel*_*ace 8 java eclipse eclipse-plugin
我喜欢当Eclipse允许我使用Tab键在方法调用中跳转参数时.我希望我的插件提供类似的功能.确切地说,我正在向编辑器中注入一些文本,我想强调特定的语法,让程序员使用Tab键跳转到下一个匹配.
这是一个例子.让我们假设我动态创建了以下代码段:
String a = "bogus string";
int i = a.[?]
Run Code Online (Sandbox Code Playgroud)
我会将其注入编辑器中,我希望它[?]
能够突出显示并准备好进行修改(用户可能会输入length()
).此外,如果有更多[?]
碎片,我希望用户使用Tab移动到下一个.
经过一番研究后,我发现可以使用模板完成.但是,我在网上找不到任何相关的例子.有没有人有这方面的经验?
更新:
我找到了两个可能有用的链接,虽然我仍然无法提出解决方案.
样本处理程序代码:
AbstractTextEditor activeEditor =
(AbstractTextEditor) HandlerUtil.getActiveEditor(event);
ISourceViewer sourceViewer =
(ISourceViewer) activeEditor.getAdapter(ITextOperationTarget.class);
Point range = sourceViewer.getSelectedRange();
// You can generate template dynamically here!
Template template = new Template("sample",
"sample description",
"no-context",
"private void ${name}(){\r\n" +
"\tSystem.out.println(\"${name}\")\r\n"
+ "}\r\n", true);
IRegion region = new Region(range.x, range.y);
TemplateContextType contextType = new TemplateContextType("test");
TemplateContext ctx =
new DocumentTemplateContext(contextType,
sourceViewer.getDocument(),
range.x,
range.y);
TemplateProposal proposal
= new TemplateProposal(template, ctx, region, null);
proposal.apply(sourceViewer, (char) 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)
结果:
我建议你使用org.eclipse.jdt.ui.javaCompletionProposalComputer
扩展名.它允许您以更合法的方式贡献模板.
在我的代码中,有黑客,因为没有办法ISourceViewer
合法.我知道ISourceViewer
它ITextTargetOperation
本身,但它不是API(非法铸造).而模板旨在被设计用于TemplateCompletionProcessor
或TemplateCompletionProposalComputer
.