Bug 440102 - [templates] ${...:field(...)} template variables are not resolved in class body
Summary: [templates] ${...:field(...)} template variables are not resolved in class body
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 4.4   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Text-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-07-22 08:47 EDT by Andreas Sewe CLA
Modified: 2014-07-22 11:22 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Sewe CLA 2014-07-22 08:47:28 EDT
When inserted directly in the body of a class, the following template

  public void m() {
      ${f:field(java.lang.Object)} = null;
  }

should have "f" expanded to whatever field (of type Object) happens to be in scope; instead, it expands to just

  public void m() {
      f = null;
  }

However, I would expect it expand to "a field in the current scope that is a subtype of any of the given types" as per the documentation [1].

If this is indeed the desired behavior, I can start working on a patch but would like to get confirmation beforehand that this is indeed how ${...:field(...)} should work.

[1] <http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Fconcepts%2Fconcept-template-variables.htm>
Comment 1 Jay Arthanareeswaran CLA 2014-07-22 09:53:24 EDT
Looks like your question is valid. In fact, I don't find this working even for the examples mentioned in the documentation. I am not sure if this ever worked and whether it belongs in UI or core. Copying Markus who might have more info.
Comment 2 Markus Keller CLA 2014-07-22 11:22:50 EDT
Currently, the ${field} template variable only works at insertion positions where the field name is valid content assist completion (e.g. inside a method body). Code pointer: org.eclipse.jdt.internal.corext.template.java.FieldResolver.getVisibleVariables(..).

I agree it would be nice to have it work in the "Java type members" context as well. I'm not sure about the best way to implement this. We'd probably need a second invocation of code assist (JavaContext.getCompletion()) in a CU that already contains the method header "public void m() {", but this may be hard to get right, given that we don't know much about the structure of the actual template.