开发者

Conditional statements in Eclipse templates

Eclipse templates can automatically inser开发者_如何学Ct text and variables as you are coding. When variables are used with the ${variable} form, the value is inserted automatically.

My question is whether you can add sections to these templates conditionally. Can you have a method definition template that will fill in multiple variables and add local definitions of them, but only if they are filled in?

def method(${var1}, ${var2}, ${var3}):  
   self.${var1} = ${var1}  
   self.${var2} = ${var2}  
   self.${var3} = ${var3}  

So, the ${var2} and ${var3} local variables would only exist if their definitions existed in the method definition?

This question is specifically for Python development with PyDev but answers for other languages would be welcome too.


It can be done through Jython scripting in Pydev in the latest nightly build (which will be 1.5.4).

See http://pydev.org/download.html for details on getting it.

Shortly, you can define a variable in scripting and program it in Jython to be how you want it to be (and you can update the templates cache on the fly after creating your variable).

There are some variables defined by default like that at: http://github.com/aptana/Pydev/blob/master/plugins/org.python.pydev.jython/jysrc/pytemplate_defaults.py

That module has comments on how to proceed (and you can use that module as an example).


Not currently possible in eclipse editor templates.

You can define custom VariableResolvers that generate whatever you would like. However, I don't think that would do what you're looking for.

A variable resolver can return "", you'd have to have basically the entire template in the resolver; not terribly reusable in other templates.

Sorry - I don't see any way to do that (though I'd love to have that capability).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜