Search in sources :

Example 61 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project flux 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 62 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project flux by eclipse.

the class StubUtility method getTypeComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getTypeComment(ICompilationUnit, String, String[], String)
	 */
public static String getTypeComment(ICompilationUnit cu, String typeQualifiedName, String[] typeParameterNames, String lineDelim) throws CoreException {
    Template template = getCodeTemplate(CodeTemplateContextType.TYPECOMMENT_ID, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelim);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, Signature.getQualifier(typeQualifiedName));
    context.setVariable(CodeTemplateContextType.TYPENAME, Signature.getSimpleName(typeQualifiedName));
    TemplateBuffer buffer;
    try {
        buffer = context.evaluate(template);
    } catch (BadLocationException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    } catch (TemplateException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    }
    String str = buffer.getString();
    if (Strings.containsOnlyWhitespaces(str)) {
        return null;
    }
    // look if Javadoc tags have to be added
    TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
    if (position == null) {
        return str;
    }
    IDocument document = new Document(str);
    int[] tagOffsets = position.getOffsets();
    for (int i = tagOffsets.length - 1; i >= 0; i--) {
        // from last to first
        try {
            insertTag(document, tagOffsets[i], position.getLength(), EMPTY, EMPTY, null, typeParameterNames, false, lineDelim);
        } catch (BadLocationException e) {
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
        }
    }
    return document.get();
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) CoreException(org.eclipse.core.runtime.CoreException) TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) IDocument(org.eclipse.jface.text.IDocument) Template(org.eclipse.jface.text.templates.Template)

Example 63 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project flux by eclipse.

the class StubUtility method evaluateTemplate.

private static String evaluateTemplate(CodeTemplateContext context, Template template) throws CoreException {
    TemplateBuffer buffer;
    try {
        buffer = context.evaluate(template);
    } catch (BadLocationException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    } catch (TemplateException e) {
        throw new CoreException(Status.CANCEL_STATUS);
    }
    if (buffer == null)
        return null;
    String str = buffer.getString();
    if (Strings.containsOnlyWhitespaces(str)) {
        return null;
    }
    return str;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 64 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project xtext-eclipse by eclipse.

the class XtextTemplateProposal method getAdditionalProposalInfo.

@Override
public String getAdditionalProposalInfo() {
    try {
        TemplateContext context = getContext();
        context.setReadOnly(true);
        TemplateBuffer templateBuffer;
        try {
            Template template = getTemplate();
            if (context instanceof XtextTemplateContext) {
                templateBuffer = ((XtextTemplateContext) context).evaluateForDisplay(template);
            } else {
                templateBuffer = context.evaluate(template);
            }
        } catch (TemplateException e) {
            return null;
        }
        return templateBuffer.getString();
    } catch (BadLocationException e) {
        return null;
    }
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) TemplateContext(org.eclipse.jface.text.templates.TemplateContext) BadLocationException(org.eclipse.jface.text.BadLocationException) Template(org.eclipse.jface.text.templates.Template)

Example 65 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project xtext-eclipse by eclipse.

the class XtextTemplateContext method evaluateForDisplay.

/**
 * @since 2.3
 */
public TemplateBuffer evaluateForDisplay(Template template) throws BadLocationException, TemplateException {
    if (!canEvaluate(template))
        return null;
    TemplateTranslator translator = new TemplateTranslator();
    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)

Aggregations

TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)71 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)30 BadLocationException (org.eclipse.jface.text.BadLocationException)26 TemplateException (org.eclipse.jface.text.templates.TemplateException)26 Document (org.eclipse.jface.text.Document)23 Template (org.eclipse.jface.text.templates.Template)23 Test (org.junit.Test)22 IDocument (org.eclipse.jface.text.IDocument)21 CoreException (org.eclipse.core.runtime.CoreException)17 DocumentTemplateContext (org.eclipse.jface.text.templates.DocumentTemplateContext)11 TemplateTranslator (org.eclipse.jface.text.templates.TemplateTranslator)11 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)9 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)8 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)6 SimpleDateFormat (com.ibm.icu.text.SimpleDateFormat)4 CodeGenerationTemplate (org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)4 ArrayList (java.util.ArrayList)3 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)3 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)3 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)3