Search in sources :

Example 6 with CodeTemplateContext

use of org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext 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)

Example 7 with CodeTemplateContext

use of org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext in project eclipse.jdt.ls by eclipse.

the class StubUtility method getGetterComment.

/*
	 * Don't use this method directly, use CodeGeneration.
	 * @see org.eclipse.jdt.ui.CodeGeneration#getGetterComment(ICompilationUnit, String, String, String, String, String, String)
	 */
public static String getGetterComment(ICompilationUnit cu, String typeName, String methodName, String fieldName, String fieldType, String bareFieldName, String lineDelimiter) throws CoreException {
    CodeGenerationTemplate templateSetting = CodeGenerationTemplate.GETTERCOMMENT;
    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);
    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 8 with CodeTemplateContext

use of org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext in project eclipse.jdt.ls by eclipse.

the class StubUtility method getTypeComment.

// 
// /*
// * Don't use this method directly, use CodeGeneration.
// * @see org.eclipse.jdt.ui.CodeGeneration#getCompilationUnitContent(ICompilationUnit, String, String, String, String)
// */
// public static String getCompilationUnitContent(ICompilationUnit cu, String fileComment, String typeComment, String typeContent, String lineDelimiter) throws CoreException {
// IPackageFragment pack= (IPackageFragment)cu.getParent();
// String packDecl= pack.isDefaultPackage() ? "" : "package " + pack.getElementName() + ';'; //$NON-NLS-1$ //$NON-NLS-2$
// return getCompilationUnitContent(cu, packDecl, fileComment, typeComment, typeContent, lineDelimiter);
// }
// 
// public static String getCompilationUnitContent(ICompilationUnit cu, String packDecl, String fileComment, String typeComment, String typeContent, String lineDelimiter) throws CoreException {
// Template template= getCodeTemplate(CodeTemplateContextType.NEWTYPE_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.PACKAGE_DECLARATION, packDecl);
// context.setVariable(CodeTemplateContextType.TYPE_COMMENT, typeComment != null ? typeComment : ""); //$NON-NLS-1$
// context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment : ""); //$NON-NLS-1$
// context.setVariable(CodeTemplateContextType.TYPE_DECLARATION, typeContent);
// context.setVariable(CodeTemplateContextType.TYPENAME, JavaCore.removeJavaLikeExtension(cu.getElementName()));
// 
// String[] fullLine= { CodeTemplateContextType.PACKAGE_DECLARATION, CodeTemplateContextType.FILE_COMMENT, CodeTemplateContextType.TYPE_COMMENT };
// return evaluateTemplate(context, template, fullLine);
// }
// 
// 
// /*
// * 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);
// }
// 
/*
	 * 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 = CodeGenerationTemplate.TYPECOMMENT.createTemplate(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
    TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
    // added
    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(StatusFactory.newErrorStatus("Problem insert tag", e));
        }
    }
    return document.get();
}
Also used : CodeTemplateContext(org.eclipse.jdt.ls.core.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) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)

Example 9 with CodeTemplateContext

use of org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext in project eclipse.jdt.ls by eclipse.

the class StubUtility method getMethodComment.

// 
// private static String evaluateTemplate(CodeTemplateContext context, Template template, String[] fullLineVariables) throws CoreException {
// TemplateBuffer buffer;
// try {
// buffer= context.evaluate(template);
// if (buffer == null) {
// return null;
// }
// String str= fixEmptyVariables(buffer, fullLineVariables);
// if (Strings.containsOnlyWhitespaces(str)) {
// return null;
// }
// return str;
// } catch (BadLocationException e) {
// throw new CoreException(Status.CANCEL_STATUS);
// } catch (TemplateException e) {
// throw new CoreException(Status.CANCEL_STATUS);
// }
// }
// 
/*
	 * 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;
    CodeGenerationTemplate templateSetting = CodeGenerationTemplate.METHODCOMMENT;
    if (decl.isConstructor()) {
        templateSetting = CodeGenerationTemplate.CONSTRUCTORCOMMENT;
    } else if (needsTarget) {
        if (delegate) {
            templateSetting = CodeGenerationTemplate.DELEGATECOMMENT;
        } else {
            templateSetting = CodeGenerationTemplate.OVERRIDECOMMENT;
        }
    }
    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, 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(StatusFactory.newErrorStatus("Invalid edit", 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) 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

CodeTemplateContext (org.eclipse.jdt.ls.core.internal.corext.template.java.CodeTemplateContext)9 CodeGenerationTemplate (org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)9 Template (org.eclipse.jface.text.templates.Template)9 CoreException (org.eclipse.core.runtime.CoreException)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 Document (org.eclipse.jface.text.Document)3 IDocument (org.eclipse.jface.text.IDocument)3 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)3 TemplateException (org.eclipse.jface.text.templates.TemplateException)3 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)3 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)1 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)1 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)1