name How do I update an Eclipse template variable on the fly?

how to change javadoc author in eclipse (3)

I've added the following new Eclipse template via extension point. It simply adds a template for a sample testTag tag.

<!-- Add code template -->
<extension point="org.eclipse.ui.editors.templates">
   <template autoinsert="true"
             description="[Description] Template populated by Snippet values ***"
                <testTag style="background: ${color}"></testTag> 

What I'd cannot figure out is how to change the value of the $(color) variable at runtime. More specifically, when the user presses Ctrl + Space (or the equivalent for content-assist) and types in "testTag" and presses Enter -- instead of the "color" placeholder text, I'd like it replaced by some other text value I have in another class. How do I do this?

Add Java code template programmatically

Sample Handler Codes:

AbstractTextEditor activeEditor = 
        (AbstractTextEditor) HandlerUtil.getActiveEditor(event);

ISourceViewer sourceViewer = 
        (ISourceViewer) activeEditor.getAdapter(ITextOperationTarget.class);

Point range = sourceViewer.getSelectedRange();

// You can generate template dynamically here!
Template template = new Template("sample", 
        "sample description", 
        "private void ${name}(){\r\n" + 
        + "}\r\n", true);

IRegion region = new Region(range.x, range.y);
TemplateContextType contextType = new TemplateContextType("test");
TemplateContext ctx =
    new DocumentTemplateContext(contextType, 

TemplateProposal proposal 
    = new TemplateProposal(template, ctx, region, null);

proposal.apply(sourceViewer, (char) 0, 0, 0);


I suggest you use org.eclipse.jdt.ui.javaCompletionProposalComputer extension. It allows you can contribute Template more legal way.

In my codes, there are hacks since there is no way to get ISourceViewer legally. I know ISourceViewer is ITextTargetOperation itself, but it is not API(Illegal Casting). And Template is intended to designed to be used by TemplateCompletionProcessor or TemplateCompletionProposalComputer.

I'm not entirely sure what you want, but you may be able to do what you want with templates.

For example, open a java editor, place the cursor inside a method, type arraya then ctlr-space, and select arrayadd from the pop up menu. You will get a template with String highlighted, pressing tab jumps to the next variable. The template source can be seen in,

Preferences -> java -> editor ->templates

${array_type}[] ${result:newName(array)} = new ${array_type}[${array}.length + 1];
System.arraycopy(${array}, 0, ${result}, 0, ${array}.length);
${result}[${array}.length]= ${var};

Everything starting the a $ is a variable that you can fill in, and you can tab between variables while filling in the template.

This email chain from 2004 says it might not be possible:

the Java editor chooses not to respect resolvers contributed to its two context types ('java' and 'javadoc'), but only recognizes the built-in resolvers.

The html editor you are working with may have a similar restriction.