js editor templates - custom variable doesn't resolve [message #870969] |
Thu, 10 May 2012 11:58 |
macbeth Missing name Messages: 2 Registered: May 2012 |
Junior Member |
|
|
Hi,
I am trying to create a custom variable which could be used in javascript editor templates. I have a class extending TemplateVariableResolver and overriding
public void resolve(TemplateVariable variable, TemplateContext context) method.
My template looks like this:
foo${variableOne:abcd}bar${variableTwo:capitalize(variableOne)}
the custom variable "capitalize" should uppercase the first letter of anything that is contained in variableOne.
the output is:
fooabcdbarAbcd
this is fine, but if I edit the text abcd, the variableTwo isn't re-resolved
I tried to add dependency between the two variables in the TemplateContext but it didn't work. How can this be done?
thanks
My code (experimental, with reflection and discouraged access):
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateVariable;
import org.eclipse.jface.text.templates.TemplateVariableResolver;
import org.eclipse.wst.jsdt.internal.corext.template.java.JavaContext;
import org.eclipse.wst.jsdt.internal.ui.text.template.contentassist.MultiVariable;
public class CapitalizingVariableResolver extends TemplateVariableResolver {
@Override
public void resolve(TemplateVariable variable, TemplateContext context) {
@SuppressWarnings("unchecked")
final List<String> params = variable.getVariableType().getParams();
if (params.isEmpty())
return;
try {
MultiVariable v = dirtyReflectionHack(context, params.get(0));
final String currentValue = (String) v.getCurrentChoice();
if (currentValue == null || currentValue.length() == 0)
return;
JavaContext jc = (JavaContext) context;
jc.addDependency(v, (MultiVariable) variable);
variable.setValue(currentValue.substring(0, 1).toUpperCase() + currentValue.substring(1));
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("rawtypes")
private MultiVariable dirtyReflectionHack(TemplateContext context, String paramVar) throws Exception {
Field f = context.getClass().getDeclaredField("fVariables");
f.setAccessible(true);
return (MultiVariable)((Map) f.get(context)).get(paramVar);
}
}
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02923 seconds