Community
Participate
Working Groups
Templates provide some rudimentary support for indicating the type of a variable. If you use ${array}, for instance, the template tries to pick an array for that variable. But the built-in types are already insufficient, and I cannot imagine that we could possibly satisfy 80% of the users with only built in types. (For instance, I've written templates which cry for ${string} and ${iterator} types.) I think that the template support should provide some sort of generic mechanism for specifying the type of a variable. Something like ${java.lang.String string} or ${int index}. (20020215 build)
I absolutely agree. The tricky part is to figure out a flexible grammar to specify additional arguments to variables.
*** Bug 25286 has been marked as a duplicate of this bug. ***
Unassign due to staff changes.
*** Bug 78273 has been marked as a duplicate of this bug. ***
Also see bug 78273 for interesting comments about adding generics awareness to templates.
Fixed > 20060623. Template variables may now optionally be typed. A typed variable has the form ${name:type [( params )]} where the parameter list is an optional comma-separated list of strings. Variable resolvers are selected based on the variable type and may query the parameters via the new TemplateVariableType class. Variables that do not explicitely specify a type (as up to now) work as they always did: the variable name is also the variable's type. That is, the following two variable declarations are equivalent: ${array} ${array:array} But while it was only possible to have a single variable for each type, one can now have several variables. -- Interested parties are encouraged to provide feedback on the solution. One problem may be that templates tend to get more verbose as types and parameters are added. This could be solved by allowing declarations and other template constructs that do not directly translate into a resolved template, for example imports. -- In addition, resolvers for the following variable types were contributed to the Java template context: var(type) - resolves to a local variable in the current scope the type of which matches the parameter. name(varOrType) - resolves to a name for a new local variable of a type determined by the parameter. The parameter may be a Java type name or the name of another template variable. The resolved name adheres to the configured code conventions and does not conflict with an existing variable. elementType(var) - resolves to the element type of the template variable referenced as parameter. The element type is either the element type of a java.util.Iterable or of an array. typevar(var, index) - resolves to the lower bound of the type argument of another template variable at the specified index. The first type variable is taken if index is omitted. A for loop template could be written as follows: for (${t:typevar(i)} ${:name(t)} : ${i:var(java.lang.Iterable)}) { ${cursor} }
As an addition to this, could support for imports be added? If I have a code template such as: ---------------------------------------- catch( SQLException e ) { ${cursor}// TODO handle bad SQL execution } finally { try { conn.close(); } catch( SQLException e ) { // oh well .... } } ---------------------------------------- When this code is inserted, then the SQLException is flagged as not being found. I need to place the cursor after SQLException then CTRL-SPACE which inserts the import statement. Maybe something like: ${import:java.sql.SQLException} so the above template would look like: ---------------------------------------- catch( ${import:java.sql.SQLException}SQLException e ) { ${cursor}// TODO handle bad SQL execution } finally { try { conn.close(); } catch( SQLException e ) { // oh well .... } } ---------------------------------------- with maybe hooks into the code assist to generate the import statement?
And for bonus points :-) Maybe hook into refactoring??
>As an addition to this, could support for imports be added? If I have a code >template such as: Please file a new enhancement request - this one is closed. >And for bonus points :-) Well, provide a patch and you get the bonus points.