Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 10694 - [templates] need more flexible type mechanism
Summary: [templates] need more flexible type mechanism
Status: CLOSED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 2.0   Edit
Hardware: PC All
: P4 normal with 2 votes (vote)
Target Milestone: 3.3 M1   Edit
Assignee: JDT-Text-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 25286 78273 (view as bug list)
Depends on:
Blocks:
 
Reported: 2002-03-04 12:53 EST by Peter Burka CLA
Modified: 2006-10-20 02:35 EDT (History)
8 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Peter Burka CLA 2002-03-04 12:53:51 EST
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)
Comment 1 Claude Knaus CLA 2002-03-05 04:10:30 EST
I absolutely agree. The tricky part is to figure out a flexible grammar to 
specify additional arguments to variables.
Comment 2 Claude Knaus CLA 2002-10-28 06:37:11 EST
*** Bug 25286 has been marked as a duplicate of this bug. ***
Comment 3 Kai-Uwe Maetzel CLA 2003-02-24 05:15:17 EST
Unassign due to staff changes.
Comment 4 Tom Hofmann CLA 2004-11-10 08:22:49 EST
*** Bug 78273 has been marked as a duplicate of this bug. ***
Comment 5 Tom Hofmann CLA 2004-11-10 08:25:10 EST
Also see bug 78273 for interesting comments about adding generics awareness to
templates.
Comment 6 Tom Hofmann CLA 2006-06-23 06:25:47 EDT
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}
}
Comment 7 Wojtek Bok CLA 2006-10-19 18:29:49 EDT
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?
Comment 8 Wojtek Bok CLA 2006-10-19 18:34:30 EDT
And for bonus points :-)

Maybe hook into refactoring??
Comment 9 Dani Megert CLA 2006-10-20 02:35:30 EDT
>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.