Search in sources :

Example 1 with CodeGenerationTemplate

use of org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate in project eclipse.jdt.ls by eclipse.

the class StubUtility method getCatchBodyContent.

public static String getCatchBodyContent(ICompilationUnit cu, String exceptionType, String variableName, String enclosingType, String enclosingMethod, String lineDelimiter) throws CoreException {
    // Template template = getCodeTemplate(CodeTemplateContextType.CATCHBLOCK_ID, cu.getJavaProject());
    CodeGenerationTemplate templateSetting = CodeGenerationTemplate.CATCHBODY;
    Template template = templateSetting.createTemplate(cu.getJavaProject());
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, enclosingType);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, enclosingMethod);
    context.setVariable(CodeTemplateContextType.EXCEPTION_TYPE, exceptionType);
    context.setVariable(CodeTemplateContextType.EXCEPTION_VAR, variableName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) Template(org.eclipse.jface.text.templates.Template) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)

Example 2 with CodeGenerationTemplate

use of org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate in project eclipse.jdt.ls by eclipse.

the class StubUtility method getSetterMethodBodyContent.

/*
	 * Don't use this method directly, use CodeGeneration.
	 */
public static String getSetterMethodBodyContent(IJavaProject project, String destTypeName, String methodName, String fieldName, String paramName, String lineDelimiter) throws CoreException {
    CodeGenerationTemplate templateSetting = CodeGenerationTemplate.SETTERBOY;
    Template template = templateSetting.createTemplate(project);
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
    context.setVariable(CodeTemplateContextType.FIELD, fieldName);
    context.setVariable(CodeTemplateContextType.FIELD_TYPE, fieldName);
    context.setVariable(CodeTemplateContextType.PARAM, paramName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) Template(org.eclipse.jface.text.templates.Template) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)

Example 3 with CodeGenerationTemplate

use of org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate in project eclipse.jdt.ls 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 {
    CodeGenerationTemplate templateSetting = CodeGenerationTemplate.SETTERCOMMENT;
    Template template = templateSetting.createTemplate(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.ls.core.internal.corext.template.java.CodeTemplateContext) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) Template(org.eclipse.jface.text.templates.Template) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)

Example 4 with CodeGenerationTemplate

use of org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate in project eclipse.jdt.ls by eclipse.

the class StubUtility method getGetterMethodBodyContent.

// 
// private static final Set<String> VALID_TYPE_BODY_TEMPLATES;
// static {
// VALID_TYPE_BODY_TEMPLATES= new HashSet<>();
// VALID_TYPE_BODY_TEMPLATES.add(CodeTemplateContextType.CLASSBODY_ID);
// VALID_TYPE_BODY_TEMPLATES.add(CodeTemplateContextType.INTERFACEBODY_ID);
// VALID_TYPE_BODY_TEMPLATES.add(CodeTemplateContextType.ENUMBODY_ID);
// VALID_TYPE_BODY_TEMPLATES.add(CodeTemplateContextType.ANNOTATIONBODY_ID);
// }
// 
// /*
// * 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;
// }
/*
	 * Don't use this method directly, use CodeGeneration.
	 */
public static String getGetterMethodBodyContent(IJavaProject project, String destTypeName, String methodName, String fieldName, String lineDelimiter) throws CoreException {
    CodeGenerationTemplate templateSetting = CodeGenerationTemplate.GETTERBODY;
    Template template = templateSetting.createTemplate(project);
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), project, lineDelimiter);
    context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
    context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, destTypeName);
    context.setVariable(CodeTemplateContextType.FIELD, fieldName);
    return evaluateTemplate(context, template);
}
Also used : CodeTemplateContext(org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) Template(org.eclipse.jface.text.templates.Template) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)

Example 5 with CodeGenerationTemplate

use of org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate in project eclipse.jdt.ls by eclipse.

the class StubUtility method getMethodComment.

// 
// /**
// * 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)) {
// throw new IllegalArgumentException("Invalid code template ID: " + templateID); //$NON-NLS-1$
// }
// 
// 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);
// }
// 
/*
	 * 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 {
    CodeGenerationTemplate codeTemplate = CodeGenerationTemplate.METHODCOMMENT;
    if (retTypeSig == null) {
        codeTemplate = CodeGenerationTemplate.CONSTRUCTORCOMMENT;
    } else if (target != null) {
        if (delegate) {
            codeTemplate = CodeGenerationTemplate.DELEGATECOMMENT;
        } else {
            codeTemplate = CodeGenerationTemplate.OVERRIDECOMMENT;
        }
    }
    Template template = codeTemplate.createTemplate(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;
    }
    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(StatusFactory.newErrorStatus("Problem insert tag", 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) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) CodeTemplateContext(org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext) CoreException(org.eclipse.core.runtime.CoreException) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) BadLocationException(org.eclipse.jface.text.BadLocationException) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

CodeGenerationTemplate (org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)11 Template (org.eclipse.jface.text.templates.Template)8 CodeTemplateContext (org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext)7 CoreException (org.eclipse.core.runtime.CoreException)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 BadLocationException (org.eclipse.jface.text.BadLocationException)3 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)3 TemplateException (org.eclipse.jface.text.templates.TemplateException)3 CompletionItem (org.eclipse.lsp4j.CompletionItem)3 Document (org.eclipse.jface.text.Document)2 IDocument (org.eclipse.jface.text.IDocument)2 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)2 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IType (org.eclipse.jdt.core.IType)1 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)1 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)1 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)1 CodeTemplateContext (org.eclipse.jdt.internal.core.manipulation.CodeTemplateContext)1