在Eclipse(Helios)代码模板中,有没有办法将变量值的第一个字母大写
我有一个带有变量的代码模板,我只想在某些情况下将该变量的值大写(仅第一个字母)。有办法做到这一点吗 模板代码如下-我想在我的函数名中大写属性名在Eclipse(Helios)代码模板中,有没有办法将变量值的第一个字母大写,eclipse,Eclipse,我有一个带有变量的代码模板,我只想在某些情况下将该变量的值大写(仅第一个字母)。有办法做到这一点吗 模板代码如下-我想在我的函数名中大写属性名 private $$${PropertyName}; ${cursor} public function get${PropertyName}() { return $$this->${PropertyName}; } public function set${PropertyName}($$value) { $$this-&
private $$${PropertyName};
${cursor}
public function get${PropertyName}()
{
return $$this->${PropertyName};
}
public function set${PropertyName}($$value)
{
$$this->${PropertyName} = $$value;
}
请注意:这是一个用于IDE中代码模板的模板(不是PHP)。有关详细信息,请参见:我也希望这样做,并尝试构建一个自定义的
TemplateVariableResolver
。(我已经有了一个定制的解析器,可以生成新的UUID a la。)
我制作了一个绑定到大写的自定义解析器
:
public class CapitalizingVariableResolver extends TemplateVariableResolver {
@Override
public void resolve(TemplateVariable variable, TemplateContext context) {
@SuppressWarnings("unchecked")
final List<String> params = variable.getVariableType().getParams();
if (params.isEmpty())
return;
final String currentValue = context.getVariable(params.get(0));
if (currentValue == null || currentValue.length() == 0)
return;
variable.setValue(currentValue.substring(0, 1).toUpperCase() + currentValue.substring(1));
}
}
公共类大写VariableResolver扩展TemplateVariableResolver{
@凌驾
公共无效解析(TemplateVariable变量、TemplateContext上下文){
@抑制警告(“未选中”)
最终列表参数=variable.getVariableType().getParams();
if(params.isEmpty())
返回;
最终字符串currentValue=context.getVariable(params.get(0));
如果(currentValue==null | | currentValue.length()==0)
返回;
变量.setValue(currentValue.substring(0,1).toUpperCase()+currentValue.substring(1));
}
}
(plugin.xml:)
我将这样使用:(我在Java中工作;我看到您似乎不是)
publicpropertyaccessor${property:field}(){
返回${property};
}
公共${propertyType}获取${CapitaldProperty:capitalize(property)}(){
返回${property}.get();
}
公共无效集${capitalizedProperty}(${propertyType}${property}){
这个${property}.set(${property});
}
从Eclipse3.5开始,我遇到的问题是,一旦我为属性
变量指定了一个值,我的自定义解析器就没有机会重新解析。Java开发工具(EclipseJDT)似乎通过JavaContext
中名为MultiVariableGuess
的机制来重新解析依赖模板变量(请参见addDependency()
)。不幸的是,这种机制似乎没有公开,因此我/我们不能使用它来做同样的事情(没有大量的复制和粘贴或其他冗余工作)
在这一点上,我将再次放弃一段时间,并将继续在两个独立的模板变量中分别键入前导小写和前导大写名称。这是一个很好的答案。谢谢
<extension point="org.eclipse.ui.editors.templates">
<resolver
class="com.foo.CapitalizingVariableResolver"
contextTypeId="java"
description="Resolves to the value of the variable named by the first argument, but with its first letter capitalized."
name="capitalized"
type="capitalize">
</resolver>
</extension>
public PropertyAccessor<${propertyType}> ${property:field}() {
return ${property};
}
public ${propertyType} get${capitalizedProperty:capitalize(property)}() {
return ${property}.get();
}
public void set${capitalizedProperty}(${propertyType} ${property}) {
this.${property}.set(${property});
}