Bug 12756 - [templates] Allow to use functions in template
Summary: [templates] Allow to use functions in template
Status: ASSIGNED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 2.0   Edit
Hardware: All All
: P5 enhancement with 18 votes (vote)
Target Milestone: ---   Edit
Assignee: Platform-Text-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 14601 35569 36203 220953 365685 (view as bug list)
Depends on:
Blocks:
 
Reported: 2002-04-04 02:50 EST by Ilja Preuss CLA
Modified: 2021-07-08 07:00 EDT (History)
21 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ilja Preuss CLA 2002-04-04 02:50:04 EST
It would be nice to be able to declare a variable to have the capitalized 
content of an other or vice versa.

For example the template

public ${type} get${Name}() {
  return ${name};
}

could automatically use the lowercased value of ${Name} for ${name}.
Comment 1 Erich Gamma CLA 2002-04-09 11:38:03 EDT
nice idea but the notation needs to be more explicit - lower prio - Claude FYI
Comment 2 Claude Knaus CLA 2002-04-09 11:51:11 EDT
The more general idea is to support functions of variables in templates.
This requires to introduce functional constructs both in templates as well as 
in 'linked positions'.

The notation could be something like

  public ${type} get&{capitalize${type}} {
      return ${type};
  }

The increased completity in grammar and the shift towards a turing complete
programming language is holding me back from implementing it right away.

An alternative could be to introduce templettes - small java classes which
could be loaded at run-time. Any complex logic could then be implemented in the
most familiar language - Java.
Comment 3 Claude Knaus CLA 2002-04-26 10:55:35 EDT
*** Bug 14601 has been marked as a duplicate of this bug. ***
Comment 4 Kai-Uwe Maetzel CLA 2003-02-24 05:16:41 EST
Unassign due to staff changes.
Comment 5 Tom Hofmann CLA 2004-02-17 04:30:26 EST
*** Bug 35569 has been marked as a duplicate of this bug. ***
Comment 6 Tom Hofmann CLA 2004-02-17 04:32:21 EST
*** Bug 36203 has been marked as a duplicate of this bug. ***
Comment 7 Jim Mayer CLA 2011-05-04 16:46:59 EDT
I see that the last comment here was in 2004.  Having a feature like this would be extremely useful for the project that I'm working on.  I think it could build it on top of the current syntax.  For example:

// FooProperty: ${name}
${:import(com.somewhere.Foo)}
private final Foo<${type>}> ${name}Property = new Foo("${name}");

public ${type} ${theGetter:getter(name)} {
  return ${name}Property.get();
}

public void ${theSetter:setter(name)}(${type} value) {
  ${name}Property.set(value);
}

My short list of modifiers would include:

${id:getter(variable)} - Returns the getter name for the referenced template variable.  For example, "getName" or "isName".  This could follow the configuration settings for getters.

${id:setter(variable)} - Returns the setter name for the referenced template variable.

${id:capitalize(variable)} - Return the capitalized form of the referenced template variable (e.g., "foo" -> "Foo", "Foo" -> "Foo").

${id:uncapitalize(variable)} - Return the non-capitalized form of the referenced template variable (e.g., "Foo" -> "foo", "foo" -> "foo").

Note: I'm not sure what "FOO" should map to... "fOO" or "foo", and I'm not sure it really matters.  One can always refactor if it guesses wrong.

Lots of others (uppercase, lowercase, etc.) are possible, but I know we could use these.
Comment 8 Matthew Hall CLA 2011-08-29 19:02:15 EDT
I want to add my voice to this issue.

Taking my first stab at JDT code templates, attempting to make a "with" method template:

public ${enclosing_type} with${Property}(${Type} ${property}) {
  set${Property}(${property});
  return this;
}

This would be trivial to implement with the capitalize/setter variables as outlined by Jim Mayer.
Comment 9 Dani Megert CLA 2011-12-06 02:00:09 EST
*** Bug 365685 has been marked as a duplicate of this bug. ***
Comment 10 Holger Klene CLA 2012-07-08 18:45:43 EDT
I think a "turing complete" function syntax is over-engineered, as most of the duplicates of this bug request fairly simple string operations on other variables values, most often changing the case.

Actually I consider the available variables insufficient, as I myself bounced against the wrong case within the first half hour of writing my very first template, only to find out, that it won't work the way I'd like it to.
Comment 11 Dani Megert CLA 2012-07-16 08:55:26 EDT
*** Bug 220953 has been marked as a duplicate of this bug. ***
Comment 12 Martin Hansen CLA 2014-03-11 05:42:34 EDT
(In reply to Holger Klene from comment #10)
> I think a "turing complete" function syntax is over-engineered, as most of
> the duplicates of this bug request fairly simple string operations on other
> variables values, most often changing the case.


Agree
Comment 13 Paul Richards CLA 2015-06-02 10:56:33 EDT
The templates are so powerful yet still very limited by not being able to convert between common naming rules for converting between filename, types, variables, etc in order to generate even larger blocks of common code.  I'd like to add my vote here.

I suspect even the addition of simple conversion functions between upper and lower camel case, underscored capitalisation, prefix / suffix stripping, etc could go a long way to supporting the needs of most applications.

These can of course already be added with extensions for limited context types, but it would be useful if they could be added with only template extensions (i.e. without the need for custom resolvers).  And for all context types.

What would be even more useful is if these conversions could be continuously re-evaluated as the user updates a user defined variable in an editor template.
Comment 14 Sergey Kirdyankin CLA 2021-07-07 13:40:10 EDT
(In reply to Martin Otzen from comment #12)
> (In reply to Holger Klene from comment #10)
> > I think a "turing complete" function syntax is over-engineered, as most of
> > the duplicates of this bug request fairly simple string operations on other
> > variables values, most often changing the case.
> 
> 
> Agree

Me too
Comment 15 Alexander Rathai CLA 2021-07-08 05:01:00 EDT
Full Agreement on my part.
Comment 16 Martin Hansen CLA 2021-07-08 07:00:59 EDT
Agree on the basic concept here.
But let's not re-invent the wheel once more. 
I recommend to use something already there, like the string operations in gnu make. It has all the requested stuff like changing case combining strings a.s.o.

I agree the GNU string functions is maybe not the prettiest of syntaxes, and it was ment as a good candidate. But in any case better a ugly syntax that i already know than a beautiful thing I have to learn all over, and can only use here.