Search in sources :

Example 1 with TemplateTranslator

use of org.eclipse.jface.text.templates.TemplateTranslator 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 TemplateTranslator

use of org.eclipse.jface.text.templates.TemplateTranslator in project KaiZen-OpenAPI-Editor by RepreZen.

the class SwaggerTemplateContext method createTemplateTranslator.

protected TemplateTranslator createTemplateTranslator() {
    try {
        int offset = getStart();
        IRegion lineRegion = getDocument().getLineInformationOfOffset(offset);
        String line = getDocument().get(lineRegion.getOffset(), lineRegion.getLength());
        int i = 0;
        // support for array items
        StringBuilder indentation = new StringBuilder();
        while (i < line.length()) {
            char indentSymbol = line.charAt(i);
            if (Character.isWhitespace(indentSymbol)) {
                indentation.append(indentSymbol);
                i++;
            } else if ('-' == indentSymbol) {
                // array item
                indentation.append(' ');
                i++;
            } else {
                break;
            }
        }
        if (i != 0)
            return new IndentationAwareTemplateTranslator(indentation.toString());
        return new TemplateTranslator();
    } catch (BadLocationException ex) {
        return new TemplateTranslator();
    }
}
Also used : IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator)

Example 3 with TemplateTranslator

use of org.eclipse.jface.text.templates.TemplateTranslator in project KaiZen-OpenAPI-Editor by RepreZen.

the class SwaggerTemplateContext method evaluate.

// Unmodified code from
// org.eclipse.xtext.ui.editor.templates.XtextTemplateContext below
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
    if (!canEvaluate(template))
        return null;
    TemplateTranslator translator = createTemplateTranslator();
    TemplateBuffer buffer = translator.translate(template);
    getContextType().resolve(buffer, this);
    return buffer;
}
Also used : TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator)

Example 4 with TemplateTranslator

use of org.eclipse.jface.text.templates.TemplateTranslator in project che by eclipse.

the class CodeTemplateContext method evaluate.

/*
	 * @see org.eclipse.jdt.internal.corext.template.TemplateContext#evaluate(org.eclipse.jdt.internal.corext.template.Template)
	 */
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
    // test that all variables are defined
    Iterator<TemplateVariableResolver> iterator = getContextType().resolvers();
    while (iterator.hasNext()) {
        TemplateVariableResolver var = iterator.next();
        if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) {
            //$NON-NLS-1$ //$NON-NLS-2$
            Assert.isNotNull(getVariable(var.getType()), "Variable " + var.getType() + "not defined");
        }
    }
    if (!canEvaluate(template))
        return null;
    String pattern = changeLineDelimiter(template.getPattern(), fLineDelimiter);
    TemplateTranslator translator = new TemplateTranslator();
    TemplateBuffer buffer = translator.translate(pattern);
    getContextType().resolve(buffer, this);
    return buffer;
}
Also used : TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator)

Example 5 with TemplateTranslator

use of org.eclipse.jface.text.templates.TemplateTranslator in project che by eclipse.

the class JavaDocContext method evaluate.

/*
	 * @see TemplateContext#evaluate(Template)
	 */
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
    TemplateTranslator translator = new TemplateTranslator();
    TemplateBuffer buffer = translator.translate(template);
    getContextType().resolve(buffer, this);
    //		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);
    return buffer;
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator)

Aggregations

TemplateTranslator (org.eclipse.jface.text.templates.TemplateTranslator)7 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)6 IJavaProject (org.eclipse.jdt.core.IJavaProject)2 TemplateVariableResolver (org.eclipse.jface.text.templates.TemplateVariableResolver)2 MultiVariable (org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IRegion (org.eclipse.jface.text.IRegion)1 TemplateException (org.eclipse.jface.text.templates.TemplateException)1 TemplateVariableType (org.eclipse.jface.text.templates.TemplateVariableType)1