diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/contentassist/SnippetCompletionProposal.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/contentassist/SnippetCompletionProposal.java index 187e8d675c..7f14773385 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/contentassist/SnippetCompletionProposal.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/contentassist/SnippetCompletionProposal.java @@ -639,10 +639,6 @@ private static String getSnippetContent(SnippetCompletionContext scc, CodeGenera } CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), scc.getRecommendedLineSeprator()); - String lineDelimiter = StubUtility.getLineDelimiterUsed(cu.getJavaProject()); - String fileComment = cu.getTypes().length == 0 ? CodeGeneration.getFileComment(cu, lineDelimiter) : null; - context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment + lineDelimiter : ""); - context.setVariable(PACKAGEHEADER, scc.getPackageHeader(lineDelimiter)); String typeName = JavaCore.removeJavaLikeExtension(cu.getElementName()); List types = Arrays.asList(cu.getAllTypes()); int postfix = 0; @@ -651,10 +647,15 @@ private static String getSnippetContent(SnippetCompletionContext scc, CodeGenera postfix++; } if (postfix > 0 && snippetStringSupport) { - context.setVariable(CodeTemplateContextType.TYPENAME, "${1:" + typeName + "}"); - } else { - context.setVariable(CodeTemplateContextType.TYPENAME, typeName); + typeName = "${1:" + typeName + "}"; } + context.setVariable(CodeTemplateContextType.TYPENAME, typeName); + String lineDelimiter = StubUtility.getLineDelimiterUsed(cu.getJavaProject()); + String fileComment = cu.getTypes().length == 0 ? CodeGeneration.getFileComment(cu, lineDelimiter) : null; + String typeComment = CodeGeneration.getTypeComment(cu, typeName, lineDelimiter); + context.setVariable(CodeTemplateContextType.FILE_COMMENT, fileComment != null ? fileComment + lineDelimiter : ""); + context.setVariable(CodeTemplateContextType.TYPE_COMMENT, typeComment != null ? typeComment + lineDelimiter : ""); + context.setVariable(PACKAGEHEADER, scc.getPackageHeader(lineDelimiter)); context.setVariable(CURSOR, snippetStringSupport ? "${0}" : ""); // TODO Consider making evaluateTemplate public in StubUtility diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/CodeTemplatePreferences.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/CodeTemplatePreferences.java index 23e274716b..20dd05745f 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/CodeTemplatePreferences.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/CodeTemplatePreferences.java @@ -148,7 +148,7 @@ public class CodeTemplatePreferences { /** * Default value for type comments */ - public static final String CODETEMPLATE_TYPECOMMENT_DEFAULT = "/**\n" + " * ${tags}\n" + " */\n"; + public static final String CODETEMPLATE_TYPECOMMENT_DEFAULT = "/**\n" + " * ${type_name}\n" + " * ${tags}\n" + " */"; /** * Default value for getter comments @@ -198,7 +198,7 @@ public class CodeTemplatePreferences { /** * Default value for public class snippet body content */ - public static final String CODETEMPLATE_CLASSSNIPPET_PUBLIC = "${filecomment}${package_header}/**\n * ${type_name}\n */\npublic class ${type_name} {\n\n\t${cursor}\n}"; + public static final String CODETEMPLATE_CLASSSNIPPET_PUBLIC = "${filecomment}${package_header}${typecomment}public class ${type_name} {\n\n\t${cursor}\n}"; /** * Default value for interface snippet body content */ @@ -206,7 +206,7 @@ public class CodeTemplatePreferences { /** * Default value for public interface snippet body content */ - public static final String CODETEMPLATE_INTERFACESNIPPET_PUBLIC = "${filecomment}${package_header}/**\n * ${type_name}\n */\npublic interface ${type_name} {\n\n\t${cursor}\n}"; + public static final String CODETEMPLATE_INTERFACESNIPPET_PUBLIC = "${filecomment}${package_header}${typecomment}public interface ${type_name} {\n\n\t${cursor}\n}"; /** * Default value for record snippet body content */ @@ -214,7 +214,7 @@ public class CodeTemplatePreferences { /** * Default value for public record snippet body content */ - public static final String CODETEMPLATE_RECORDSNIPPET_PUBLIC = "${filecomment}${package_header}/**\n * ${type_name}\n */\npublic record ${type_name}(${cursor}) {\n}"; + public static final String CODETEMPLATE_RECORDSNIPPET_PUBLIC = "${filecomment}${package_header}${typecomment}public record ${type_name}(${cursor}) {\n}"; public static class Month extends SimpleTemplateVariableResolver { public Month() { diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandlerTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandlerTest.java index ab56f79ab1..a28eb692cf 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandlerTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandlerTest.java @@ -540,12 +540,12 @@ public void testCompletion_javadocCommentRecord() throws Exception { assertEquals("999999999", item.getSortText()); assertEquals(item.getInsertTextFormat(), InsertTextFormat.Snippet); assertNotNull(item.getTextEdit()); - assertEquals("\n * ${0}\n * @param name\n * @param age\n", item.getTextEdit().getLeft().getNewText()); + assertEquals("\n * ${0}\n * Foo\n * @param name\n * @param age\n", item.getTextEdit().getLeft().getNewText()); Range range = item.getTextEdit().getLeft().getRange(); assertEquals(1, range.getStart().getLine()); assertEquals(3, range.getStart().getCharacter()); assertEquals(1, range.getEnd().getLine()); - assertEquals(" * @param name\n * @param age\n", item.getDocumentation().getLeft()); + assertEquals(" * Foo\n * @param name\n * @param age\n", item.getDocumentation().getLeft()); } finally { unit.discardWorkingCopy(); proj.delete(true, monitor); @@ -583,12 +583,12 @@ public void testCompletion_javadocCommentRecordNoSnippet() throws Exception { assertEquals("999999999", item.getSortText()); assertEquals(item.getInsertTextFormat(), InsertTextFormat.PlainText); assertNotNull(item.getTextEdit()); - assertEquals("\n * @param name\n * @param age\n", item.getTextEdit().getLeft().getNewText()); + assertEquals("\n * Foo\n * @param name\n * @param age\n", item.getTextEdit().getLeft().getNewText()); Range range = item.getTextEdit().getLeft().getRange(); assertEquals(1, range.getStart().getLine()); assertEquals(3, range.getStart().getCharacter()); assertEquals(1, range.getEnd().getLine()); - assertEquals(" * @param name\n * @param age\n", item.getDocumentation().getLeft()); + assertEquals(" * Foo\n * @param name\n * @param age\n", item.getDocumentation().getLeft()); } finally { unit.discardWorkingCopy(); proj.delete(true, monitor);