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;
}
Aggregations