Search in sources :

Example 26 with TemplateBuffer

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

the class StubUtility method getMethodComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * This method should work with all AST levels.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, boolean, String, String[], String)
	 */
public static String getMethodComment(ICompilationUnit cu, String typeName, MethodDeclaration decl, boolean isDeprecated, String targetName, String targetMethodDeclaringTypeName, String[] targetMethodParameterTypeNames, boolean delegate, String lineDelimiter) throws CoreException {
    boolean needsTarget = targetMethodDeclaringTypeName != null && targetMethodParameterTypeNames != null;
    String templateName = CodeTemplateContextType.METHODCOMMENT_ID;
    if (decl.isConstructor()) {
        templateName = CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
    } else if (needsTarget) {
        if (delegate)
            templateName = CodeTemplateContextType.DELEGATECOMMENT_ID;
        else
            templateName = CodeTemplateContextType.OVERRIDECOMMENT_ID;
    }
    Template template = getCodeTemplate(templateName, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, decl.getName().getIdentifier());
    if (!decl.isConstructor()) {
        context.setVariable(CodeTemplateContextType.RETURN_TYPE, ASTNodes.asString(getReturnType(decl)));
    }
    if (needsTarget) {
        if (delegate)
            context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
        else
            context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetMethodDeclaringTypeName, targetName, targetMethodParameterTypeNames));
    }
    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;
    }
    // look if Javadoc tags have to be added
    TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
    if (position == null) {
        return str;
    }
    IDocument textBuffer = new Document(str);
    List<TypeParameter> typeParams = shouldGenerateMethodTypeParameterTags(cu.getJavaProject()) ? decl.typeParameters() : Collections.emptyList();
    String[] typeParamNames = new String[typeParams.size()];
    for (int i = 0; i < typeParamNames.length; i++) {
        TypeParameter elem = typeParams.get(i);
        typeParamNames[i] = elem.getName().getIdentifier();
    }
    List<SingleVariableDeclaration> params = decl.parameters();
    String[] paramNames = new String[params.size()];
    for (int i = 0; i < paramNames.length; i++) {
        SingleVariableDeclaration elem = params.get(i);
        paramNames[i] = elem.getName().getIdentifier();
    }
    String[] exceptionNames = getExceptionNames(decl);
    String returnType = null;
    if (!decl.isConstructor()) {
        returnType = ASTNodes.asString(getReturnType(decl));
    }
    int[] tagOffsets = position.getOffsets();
    for (int i = tagOffsets.length - 1; i >= 0; i--) {
        // from last to first
        try {
            insertTag(textBuffer, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParamNames, isDeprecated, lineDelimiter);
        } catch (BadLocationException e) {
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
        }
    }
    return textBuffer.get();
}
Also used : TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) TemplateException(org.eclipse.jface.text.templates.TemplateException) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) Template(org.eclipse.jface.text.templates.Template) CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) CoreException(org.eclipse.core.runtime.CoreException) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) BadLocationException(org.eclipse.jface.text.BadLocationException) IDocument(org.eclipse.jface.text.IDocument)

Example 27 with TemplateBuffer

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

the class StubUtility method getMethodComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, String, String[], String[], String, String[], IMethod, String)
	 */
public static String getMethodComment(ICompilationUnit cu, String typeName, String methodName, String[] paramNames, String[] excTypeSig, String retTypeSig, String[] typeParameterNames, IMethod target, boolean delegate, String lineDelimiter) throws CoreException {
    String templateName = CodeTemplateContextType.METHODCOMMENT_ID;
    if (retTypeSig == null) {
        templateName = CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
    } else if (target != null) {
        if (delegate)
            templateName = CodeTemplateContextType.DELEGATECOMMENT_ID;
        else
            templateName = CodeTemplateContextType.OVERRIDECOMMENT_ID;
    }
    Template template = getCodeTemplate(templateName, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
    if (retTypeSig != null) {
        context.setVariable(CodeTemplateContextType.RETURN_TYPE, Signature.toString(retTypeSig));
    }
    if (target != null) {
        String targetTypeName = target.getDeclaringType().getFullyQualifiedName('.');
        String[] targetParamTypeNames = getParameterTypeNamesForSeeTag(target);
        if (delegate)
            context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetTypeName, methodName, targetParamTypeNames));
        else
            context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetTypeName, methodName, targetParamTypeNames));
    }
    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;
    }
    // look if Javadoc tags have to be added
    TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
    if (position == null) {
        return str;
    }
    IDocument document = new Document(str);
    String[] exceptionNames = new String[excTypeSig.length];
    for (int i = 0; i < excTypeSig.length; i++) {
        exceptionNames[i] = Signature.toString(excTypeSig[i]);
    }
    String returnType = retTypeSig != null ? Signature.toString(retTypeSig) : null;
    int[] tagOffsets = position.getOffsets();
    for (int i = tagOffsets.length - 1; i >= 0; i--) {
        // from last to first
        try {
            insertTag(document, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParameterNames, false, lineDelimiter);
        } catch (BadLocationException e) {
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
        }
    }
    return document.get();
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) Template(org.eclipse.jface.text.templates.Template) CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) CoreException(org.eclipse.core.runtime.CoreException) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) BadLocationException(org.eclipse.jface.text.BadLocationException) IDocument(org.eclipse.jface.text.IDocument)

Example 28 with TemplateBuffer

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

the class ErlangFileWizardPage method parse.

private String parse(final Template template, final TemplateContextType contextType) {
    String s = getFileName();
    if (SourceKind.hasModuleExtension(s)) {
        s = SystemConfiguration.withoutExtension(s);
    }
    ModuleVariableResolver.getDefault().setModule(moduleName(s));
    TemplateBuffer tb = null;
    try {
        final DocumentTemplateContext context = new DocumentTemplateContext(contextType, new Document(template.getPattern()), 0, template.getPattern().length());
        tb = context.evaluate(template);
    } catch (final BadLocationException e) {
        ErlLogger.warn(e);
    } catch (final TemplateException e) {
        ErlLogger.warn(e);
    }
    if (tb == null) {
        return null;
    }
    return tb.getString();
}
Also used : DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 29 with TemplateBuffer

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

the class FunctionVariableResolver method resolve.

@Override
public void resolve(final TemplateVariable variable, final TemplateContext context) {
    @SuppressWarnings("unchecked") final Iterator<TemplateVariableResolver> it = ErlangSourceContextTypeLayout.getDefault().resolvers();
    FunctionNameVariableResolver name_var = null;
    // !TODO: Use BodyVariableResolver
    // BodyVariableResolver body_var = null;
    ArgumentsVariableResolver arg_var = null;
    while (it.hasNext()) {
        final TemplateVariableResolver element = it.next();
        if (element instanceof FunctionNameVariableResolver) {
            name_var = (FunctionNameVariableResolver) element;
        } else if (element instanceof BodyVariableResolver) {
        // body_var = (BodyVariableResolver) element;
        } else if (element instanceof ArgumentsVariableResolver) {
            arg_var = (ArgumentsVariableResolver) element;
        }
    }
    if (arg_var == null || name_var == null) // || body_var == null
    {
        variable.setValue("");
        return;
    }
    final StringBuilder buff = new StringBuilder();
    for (final Object[] element : functions) {
        arg_var.setArity((Integer) element[1]);
        name_var.setFunctionName((String) element[0]);
        final Template commentTemplate = ErlangSourceContextTypeComment.getDefault().getTemplateStore().getTemplateData("org.erlide.ui.erlangsource.functioncomment").getTemplate();
        DocumentTemplateContext commentContext = new DocumentTemplateContext(ErlangSourceContextTypeLayout.getDefault(), new Document(commentTemplate.getPattern()), 0, commentTemplate.getPattern().length());
        TemplateBuffer tb = null;
        try {
            tb = commentContext.evaluate(commentTemplate);
        } catch (final BadLocationException e) {
            ErlLogger.warn(e);
            buff.append("Error: " + e.getMessage());
        } catch (final TemplateException e) {
            ErlLogger.warn(e);
            buff.append("Error: " + commentTemplate.getName() + " could not be validated!");
        }
        if (tb != null) {
            buff.append(tb.getString() + "\n");
        }
        final Template template = ErlangSourceContextTypeComment.getDefault().getTemplateStore().getTemplateData("org.erlide.ui.erlangsource.functionlayout").getTemplate();
        commentContext = new DocumentTemplateContext(ErlangSourceContextTypeLayout.getDefault(), new Document(template.getPattern()), 0, template.getPattern().length());
        try {
            tb = commentContext.evaluate(template);
        } catch (final BadLocationException e) {
            ErlLogger.warn(e);
            buff.append("Error: " + e.getMessage());
        } catch (final TemplateException e) {
            ErlLogger.warn(e);
            buff.append("Error: " + template.getName() + " could not be validated!");
        }
        if (tb != null) {
            buff.append(tb.getString() + "\n");
        }
    }
    variable.setValue(buff.toString());
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) Template(org.eclipse.jface.text.templates.Template) DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) BadLocationException(org.eclipse.jface.text.BadLocationException) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver)

Example 30 with TemplateBuffer

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

the class ErlangTemplateContext method evaluate.

public TemplateBuffer evaluate(final Template template0, final boolean indentFrom0) throws BadLocationException, TemplateException {
    Template template = template0;
    if (!canEvaluate(template)) {
        return null;
    }
    if (ErlTemplateCompletionPreferences.getIndentCode()) {
        template = indentTemplatePattern(template, indentFrom0);
    }
    final TemplateTranslator translator = new TemplateTranslator();
    final TemplateBuffer buffer = translator.translate(template);
    getContextType().resolve(buffer, this);
    return buffer;
}
Also used : TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Template(org.eclipse.jface.text.templates.Template) 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