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