Search in sources :

Example 91 with Template

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

the class UnimplementedCodeCleanUp method getOverridingMethodComment.

private String getOverridingMethodComment() {
    String templateName = CodeTemplateContextType.OVERRIDECOMMENT_ID;
    Template template = getCodeTemplate(templateName);
    if (template == null)
        //$NON-NLS-1$
        return "";
    //$NON-NLS-1$
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), null, "\n");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.FILENAME, "Face.java");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.PACKAGENAME, "test");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.PROJECTNAME, "TestProject");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, "Face");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, "method");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.RETURN_TYPE, "void");
    //$NON-NLS-1$
    context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, "test.IFace#foo()");
    return evaluateTemplate(template, context);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 92 with Template

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

the class StubUtility method setCodeTemplate.

/**
	 * Only to be used by tests
	 *
	 * @param templateId the template id
	 * @param pattern the new pattern
	 * @param project not used
	 */
public static void setCodeTemplate(String templateId, String pattern, IJavaProject project) {
    TemplateStore codeTemplateStore = JavaPlugin.getDefault().getCodeTemplateStore();
    TemplatePersistenceData data = codeTemplateStore.getTemplateData(templateId);
    Template orig = data.getTemplate();
    Template copy = new Template(orig.getName(), orig.getDescription(), orig.getContextTypeId(), pattern, true);
    data.setTemplate(copy);
}
Also used : TemplatePersistenceData(org.eclipse.che.jface.text.templates.persistence.TemplatePersistenceData) TemplateStore(org.eclipse.che.jface.text.templates.persistence.TemplateStore) Template(org.eclipse.jface.text.templates.Template)

Example 93 with Template

use of org.eclipse.jface.text.templates.Template in project che 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
	 * @see org.eclipse.jdt.ui.CodeGeneration#getTypeBody(String, ICompilationUnit, String, String)
	 */
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)

Example 94 with Template

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

the class StubUtility method getSetterComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getSetterComment(ICompilationUnit, String, String, String, String, String, String, String)
	 */
public static String getSetterComment(ICompilationUnit cu, String typeName, String methodName, String fieldName, String fieldType, String paramName, String bareFieldName, String lineDelimiter) throws CoreException {
    String templateName = CodeTemplateContextType.SETTERCOMMENT_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);
    context.setVariable(CodeTemplateContextType.FIELD, fieldName);
    context.setVariable(CodeTemplateContextType.FIELD_TYPE, fieldType);
    context.setVariable(CodeTemplateContextType.BARE_FIELD_NAME, bareFieldName);
    context.setVariable(CodeTemplateContextType.PARAM, paramName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext) Template(org.eclipse.jface.text.templates.Template)

Example 95 with Template

use of org.eclipse.jface.text.templates.Template in project che 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)

Aggregations

Template (org.eclipse.jface.text.templates.Template)158 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)27 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)22 TemplateException (org.eclipse.jface.text.templates.TemplateException)21 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)19 IDocument (org.eclipse.jface.text.IDocument)18 Document (org.eclipse.jface.text.Document)17 ArrayList (java.util.ArrayList)15 BadLocationException (org.eclipse.jface.text.BadLocationException)15 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)15 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)14 TemplatePersistenceData (org.eclipse.jface.text.templates.persistence.TemplatePersistenceData)13 TemplateStore (org.eclipse.jface.text.templates.persistence.TemplateStore)12 CoreException (org.eclipse.core.runtime.CoreException)11 Region (org.eclipse.jface.text.Region)11 DocumentTemplateContext (org.eclipse.jface.text.templates.DocumentTemplateContext)11 ISubReference (org.eclipse.titan.designer.AST.ISubReference)11 IRegion (org.eclipse.jface.text.IRegion)10 ITextSelection (org.eclipse.jface.text.ITextSelection)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)10