Search in sources :

Example 1 with CodeTemplateContext

use of org.eclipse.jdt.internal.core.manipulation.CodeTemplateContext 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)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IType (org.eclipse.jdt.core.IType)1 CodeTemplateContext (org.eclipse.jdt.internal.core.manipulation.CodeTemplateContext)1 CodeGenerationTemplate (org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 Template (org.eclipse.jface.text.templates.Template)1 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)1 TemplateException (org.eclipse.jface.text.templates.TemplateException)1