Search in sources :

Example 1 with MultiVariable

use of org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable in project che by eclipse.

the class JavaContext method evaluate.

/*
	 * @see TemplateContext#evaluate(Template template)
	 */
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
    clear();
    if (!canEvaluate(template))
        throw new TemplateException(JavaTemplateMessages.Context_error_cannot_evaluate);
    TemplateTranslator translator = new TemplateTranslator() {

        @Override
        protected TemplateVariable createVariable(TemplateVariableType type, String name, int[] offsets) {
            //				TemplateVariableResolver resolver= getContextType().getResolver(type.getName());
            //				return resolver.createVariable();
            MultiVariable variable = new JavaVariable(type, name, offsets);
            fVariables.put(name, variable);
            return variable;
        }
    };
    TemplateBuffer buffer = translator.translate(template);
    getContextType().resolve(buffer, this);
    rewriteImports();
    //		IPreferenceStore prefs = JavaPlugin.getDefault().getPreferenceStore();
    //prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER);
    boolean useCodeFormatter = true;
    IJavaProject project = getJavaProject();
    JavaFormatter formatter = new JavaFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentation(), useCodeFormatter, project);
    formatter.format(buffer, this);
    clear();
    return buffer;
}
Also used : TemplateVariableType(org.eclipse.jface.text.templates.TemplateVariableType) IJavaProject(org.eclipse.jdt.core.IJavaProject) TemplateException(org.eclipse.jface.text.templates.TemplateException) MultiVariable(org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator)

Example 2 with MultiVariable

use of org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable in project che by eclipse.

the class NameResolver method resolve.

/*
	 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
	 */
@Override
public void resolve(TemplateVariable variable, TemplateContext context) {
    List<String> params = variable.getVariableType().getParams();
    String param;
    if (params.size() == 0)
        param = fDefaultType;
    else
        param = params.get(0);
    JavaContext jc = (JavaContext) context;
    TemplateVariable ref = jc.getTemplateVariable(param);
    MultiVariable mv = (MultiVariable) variable;
    if (ref instanceof MultiVariable) {
        // reference is another variable
        MultiVariable refVar = (MultiVariable) ref;
        jc.addDependency(refVar, mv);
        refVar.getAllChoices();
        Object[] types = flatten(refVar.getAllChoices());
        for (int i = 0; i < types.length; i++) {
            String[] names = jc.suggestVariableNames(mv.toString(types[i]));
            mv.setChoices(types[i], names);
        }
        mv.setKey(refVar.getCurrentChoice());
        jc.markAsUsed(mv.getDefaultValue());
    } else {
        // reference is a Java type name
        jc.addImport(param);
        String[] names = jc.suggestVariableNames(param);
        mv.setChoices(names);
        jc.markAsUsed(names[0]);
    }
}
Also used : TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) MultiVariable(org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable)

Example 3 with MultiVariable

use of org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable in project che by eclipse.

the class TypeVariableResolver method resolve.

/*
	 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
	 * @since 3.3
	 */
@Override
public void resolve(TemplateVariable variable, TemplateContext context) {
    if (!(variable instanceof MultiVariable)) {
        super.resolve(variable, context);
        return;
    }
    MultiVariable mv = (MultiVariable) variable;
    List<String> params = variable.getVariableType().getParams();
    if (params.isEmpty()) {
        super.resolve(variable, context);
        return;
    }
    JavaContext jc = (JavaContext) context;
    String reference = params.get(0);
    int index = 0;
    if (params.size() > 1) {
        String indexParam = params.get(1);
        try {
            index = Integer.parseInt(indexParam);
        } catch (NumberFormatException x) {
        }
    }
    TemplateVariable refVar = jc.getTemplateVariable(reference);
    if (refVar instanceof JavaVariable) {
        JavaVariable jvar = (JavaVariable) refVar;
        resolve(mv, jvar, index, jc);
        return;
    }
    super.resolve(variable, context);
}
Also used : MultiVariable(org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable)

Example 4 with MultiVariable

use of org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable in project che by eclipse.

the class ElementTypeResolver method resolve.

/*
	 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
	 * @since 3.3
	 */
@Override
public void resolve(TemplateVariable variable, TemplateContext context) {
    if (!(variable instanceof MultiVariable)) {
        super.resolve(variable, context);
        return;
    }
    MultiVariable mv = (MultiVariable) variable;
    List<String> params = variable.getVariableType().getParams();
    if (params.isEmpty()) {
        super.resolve(variable, context);
        return;
    }
    JavaContext jc = (JavaContext) context;
    String reference = params.get(0);
    TemplateVariable refVar = jc.getTemplateVariable(reference);
    if (refVar instanceof JavaVariable) {
        JavaVariable jvar = (JavaVariable) refVar;
        resolve(mv, jvar, jc);
        return;
    }
    super.resolve(variable, context);
}
Also used : MultiVariable(org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable)

Example 5 with MultiVariable

use of org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable in project che by eclipse.

the class TypeResolver method resolve.

/* (non-Javadoc)
	 * @see org.eclipse.jface.text.templates.TemplateVariableResolver#resolve(org.eclipse.jface.text.templates.TemplateVariable, org.eclipse.jface.text.templates.TemplateContext)
	 */
@Override
public void resolve(TemplateVariable variable, TemplateContext context) {
    List<String> params = variable.getVariableType().getParams();
    String param;
    if (params.size() == 0)
        param = fDefaultType;
    else
        param = params.get(0);
    JavaContext jc = (JavaContext) context;
    MultiVariable mv = (MultiVariable) variable;
    String reference = jc.addImport(param);
    mv.setValue(reference);
    mv.setUnambiguous(true);
}
Also used : MultiVariable(org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable)

Aggregations

MultiVariable (org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable)5 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)3 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)1 TemplateException (org.eclipse.jface.text.templates.TemplateException)1 TemplateTranslator (org.eclipse.jface.text.templates.TemplateTranslator)1 TemplateVariableType (org.eclipse.jface.text.templates.TemplateVariableType)1