如何将参数传递到eclipse-plugin中的代码模板

我想创建一个定义新代码模板的插件(喜欢这篇博文).如何将参数传递给模板?像 ${name:param}?

I want to create a plugin which defines a new code template (like this blog post). How can I pass a parameter into the template? like ${name:param}?

推荐答案

您可以传递给代码模板的东西并不多.例如 ${word_selection} 包含当前选择.

There aren't many things that you can pass into a code template. For example ${word_selection} contains the current selection.

但是很多人缺少的是您可以定义自己的变量:

But what many people are missing is that you can define your own variables:

private static final ${type} ${name} = new ${type} (${cursor});

${type}${name} 都不在您单击插入变量..."按钮时获得的列表中.Eclipse 注意到并允许您使用 Tab 循环浏览它们,它将使这些自定义模板字段"的内容保持同步(因此您在 new 之后的部分被填写如果您在第一个字段中输入).

Neither ${type} nor ${name} are in the list which you get when you click the "Insert variable..." button. Eclipse notices and allows you to cycle through them with Tab and it will keep the content of these custom "template fields" in sync (so you the part after new gets filled in if you type in the first field).

查看此答案以了解其他有用的 Eclipse 模板.

根据您提到的博客文章中的答案,目前只能使用编辑器模板,而不是代码模板.我建议提交一个针对 JDT Text 的错误以为此打开 API.

According to the answers in the blog post which you mention, this is only possible at the moment with editor templates, not code templates. I suggest to file a bug against JDT Text to open the API for this.

相关文章