Search in sources :

Example 16 with Template

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

the class StubUtility method getFieldComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 */
public static String getFieldComment(ICompilationUnit cu, String typeName, String fieldName, String lineDelimiter) throws CoreException {
    Template template = getCodeTemplate(CodeTemplateContextType.FIELDCOMMENT_ID, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.FIELD_TYPE, typeName);
    context.setVariable(CodeTemplateContextType.FIELD, fieldName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 17 with Template

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

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

the class StubUtility method getFileComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getFileComment(ICompilationUnit, String)
	 */
public static String getFileComment(ICompilationUnit cu, String lineDelimiter) throws CoreException {
    Template template = getCodeTemplate(CodeTemplateContextType.FILECOMMENT_ID, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    IJavaProject project = cu.getJavaProject();
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.TYPENAME, JavaCore.removeJavaLikeExtension(cu.getElementName()));
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) IJavaProject(org.eclipse.jdt.core.IJavaProject) Template(org.eclipse.jface.text.templates.Template)

Example 19 with Template

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

the class StubUtility method getMethodBodyContent.

/*
	 * Don't use this method directly, use CodeGeneration.
	 */
public static String getMethodBodyContent(boolean isConstructor, IJavaProject project, String destTypeName, String methodName, String bodyStatement, String lineDelimiter) throws CoreException {
    String templateName = isConstructor ? CodeTemplateContextType.CONSTRUCTORSTUB_ID : CodeTemplateContextType.METHODSTUB_ID;
    Template template = getCodeTemplate(templateName, project);
    if (template == null) {
        return bodyStatement;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
    context.setVariable(CodeTemplateContextType.BODY_STATEMENT, bodyStatement);
    String str = evaluateTemplate(context, template, new String[] { CodeTemplateContextType.BODY_STATEMENT });
    if (str == null && !Strings.containsOnlyWhitespaces(bodyStatement)) {
        return bodyStatement;
    }
    return str;
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 20 with Template

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

the class StubUtility method getTypeBody.

/**
	 * Don't use this method directly, use CodeGeneration.
	 * 
	 * @param templateID the template id of the type body to get. Valid id's are
	 *            {@link CodeTemplateContextType#CLASSBODY_ID},
	 *            {@link CodeTemplateContextType#INTERFACEBODY_ID},
	 *            {@link CodeTemplateContextType#ENUMBODY_ID},
	 *            {@link CodeTemplateContextType#ANNOTATIONBODY_ID},
	 * @param cu the compilation unit to which the template is added
	 * @param typeName the type name
	 * @param lineDelim the line delimiter to use
	 * @return return the type body template or <code>null</code>
	 * @throws CoreException thrown if the template could not be evaluated
	 */
public static String getTypeBody(String templateID, ICompilationUnit cu, String typeName, String lineDelim) throws CoreException {
    if (!VALID_TYPE_BODY_TEMPLATES.contains(templateID)) {
        //$NON-NLS-1$
        throw new IllegalArgumentException("Invalid code template ID: " + templateID);
    }
    Template template = getCodeTemplate(templateID, cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelim);
    context.setCompilationUnitVariables(cu);
    context.setVariable(CodeTemplateContextType.TYPENAME, typeName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Aggregations

Template (org.eclipse.jface.text.templates.Template)37 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)28 BadLocationException (org.eclipse.jface.text.BadLocationException)7 IDocument (org.eclipse.jface.text.IDocument)7 CoreException (org.eclipse.core.runtime.CoreException)6 Document (org.eclipse.jface.text.Document)6 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)6 TemplateException (org.eclipse.jface.text.templates.TemplateException)6 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)6 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 IOException (java.io.IOException)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 TemplatePersistenceData (org.eclipse.che.jface.text.templates.persistence.TemplatePersistenceData)2 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)2 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)2 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)2 Position (org.eclipse.jface.text.Position)2 Region (org.eclipse.jface.text.Region)2