Search in sources :

Example 6 with CodeGenerationTemplate

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

the class SnippetCompletionProposal method getInterfaceSnippet.

private static CompletionItem getInterfaceSnippet(SnippetCompletionContext scc, IProgressMonitor monitor) {
    ICompilationUnit cu = scc.getCompilationUnit();
    if (!accept(cu, scc.completionContext, false)) {
        return null;
    }
    if (monitor.isCanceled()) {
        return null;
    }
    final CompletionItem interfaceSnippetItem = new CompletionItem();
    interfaceSnippetItem.setFilterText(INTERFACE_SNIPPET_LABEL);
    interfaceSnippetItem.setLabel(INTERFACE_SNIPPET_LABEL);
    interfaceSnippetItem.setSortText(SortTextHelper.convertRelevance(0));
    try {
        CodeGenerationTemplate template = ((scc.needsPublic(monitor))) ? CodeGenerationTemplate.INTERFACESNIPPET_PUBLIC : CodeGenerationTemplate.INTERFACESNIPPET_DEFAULT;
        interfaceSnippetItem.setInsertText(getSnippetContent(scc, template, true));
    } catch (CoreException e) {
        JavaLanguageServerPlugin.log(e.getStatus());
        return null;
    }
    return interfaceSnippetItem;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CoreException(org.eclipse.core.runtime.CoreException) CompletionItem(org.eclipse.lsp4j.CompletionItem) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)

Example 7 with CodeGenerationTemplate

use of org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate 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 CodeGenerationTemplate

use of org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate 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)

Example 9 with CodeGenerationTemplate

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

the class SnippetCompletionProposal method getSnippetContent.

private static String getSnippetContent(SnippetCompletionContext scc, CodeGenerationTemplate templateSetting, boolean snippetStringSupport) throws CoreException {
    ICompilationUnit cu = scc.getCompilationUnit();
    Template template = templateSetting.createTemplate();
    if (template == null) {
        return null;
    }
    CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), scc.getRecommendedLineSeprator());
    context.setVariable(PACKAGEHEADER, scc.getPackageHeader());
    String typeName = JavaCore.removeJavaLikeExtension(cu.getElementName());
    List<IType> types = Arrays.asList(cu.getAllTypes());
    int postfix = 0;
    while (!types.isEmpty() && types.stream().filter(isTypeExists(typeName)).findFirst().isPresent()) {
        typeName = "Inner" + JavaCore.removeJavaLikeExtension(cu.getElementName()) + (postfix == 0 ? "" : "_" + postfix);
        postfix++;
    }
    if (postfix > 0 && snippetStringSupport) {
        context.setVariable(CodeTemplateContextType.TYPENAME, "${1:" + typeName + "}");
    } else {
        context.setVariable(CodeTemplateContextType.TYPENAME, typeName);
    }
    context.setVariable(CURSOR, snippetStringSupport ? "${0}" : "");
    // TODO Consider making evaluateTemplate public in StubUtility
    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;
    }
    return str;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CodeTemplateContext(org.eclipse.jdt.internal.core.manipulation.CodeTemplateContext) CoreException(org.eclipse.core.runtime.CoreException) TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) BadLocationException(org.eclipse.jface.text.BadLocationException) Template(org.eclipse.jface.text.templates.Template) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate) IType(org.eclipse.jdt.core.IType)

Example 10 with CodeGenerationTemplate

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

the class SnippetCompletionProposal method getRecordSnippet.

private static CompletionItem getRecordSnippet(SnippetCompletionContext scc, IProgressMonitor monitor) {
    ICompilationUnit cu = scc.getCompilationUnit();
    IJavaProject javaProject = cu.getJavaProject();
    if (javaProject == null) {
        return null;
    }
    String version = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
    if (JavaModelUtil.isVersionLessThan(version, JavaCore.VERSION_14)) {
        // not checking if preview features are enabled, as Java 14+ might support records without preview flag
        return null;
    }
    if (!accept(cu, scc.getCompletionContext(), false)) {
        return null;
    }
    if (monitor.isCanceled()) {
        return null;
    }
    final CompletionItem recordSnippet = new CompletionItem();
    recordSnippet.setFilterText(RECORD_SNIPPET_LABEL);
    recordSnippet.setLabel(RECORD_SNIPPET_LABEL);
    recordSnippet.setSortText(SortTextHelper.convertRelevance(0));
    try {
        CodeGenerationTemplate template = (scc.needsPublic(monitor)) ? CodeGenerationTemplate.RECORDSNIPPET_PUBLIC : CodeGenerationTemplate.RECORDSNIPPET_DEFAULT;
        recordSnippet.setInsertText(getSnippetContent(scc, template, true));
    } catch (CoreException e) {
        JavaLanguageServerPlugin.log(e.getStatus());
        return null;
    }
    return recordSnippet;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) CompletionItem(org.eclipse.lsp4j.CompletionItem) CodeGenerationTemplate(org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)

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