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/corrections/proposals/NewCUProposal.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/NewCUProposal.java index f6a1a43909..7d1b2088b8 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/NewCUProposal.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/corrections/proposals/NewCUProposal.java @@ -29,6 +29,7 @@ import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IPackageFragmentRoot; import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.ASTParser; @@ -363,7 +364,8 @@ private String constructCUContent(ICompilationUnit cu, String typeContent, Strin if (fileComment != null && !fileComment.isEmpty()) { fileComment += lineDelimiter; } - String typeComment = CodeGeneration.getTypeComment(cu, cu.getElementName(), lineDelimiter); + String typeName = JavaCore.removeJavaLikeExtension(cu.getElementName()); + String typeComment = CodeGeneration.getTypeComment(cu, typeName, lineDelimiter); IPackageFragment pack = (IPackageFragment) cu.getParent(); String content = CodeGeneration.getCompilationUnitContent(cu, fileComment, typeComment, typeContent, lineDelimiter); if (content != null) { 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/correction/JavadocQuickFixTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/JavadocQuickFixTest.java index 2b6dbad13c..6ca848660d 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/JavadocQuickFixTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/JavadocQuickFixTest.java @@ -943,6 +943,7 @@ public void testMissingTypeComment() throws Exception { buf = new StringBuilder(); buf.append("package test1;\n"); buf.append("/**\n"); + buf.append(" * E\n"); buf.append(" * @param \n"); buf.append(" * @param \n"); buf.append(" */\n"); diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/UnresolvedTypesQuickFixTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/UnresolvedTypesQuickFixTest.java index 608ea4fa9e..61d2e5a677 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/UnresolvedTypesQuickFixTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/correction/UnresolvedTypesQuickFixTest.java @@ -489,9 +489,8 @@ public void testQualifiedType() throws Exception { buf.append("package test2;\n"); buf.append("\n"); buf.append("/**\n"); - buf.append(" * \n"); + buf.append(" * Test\n"); buf.append(" */\n"); - buf.append("\n"); buf.append("public class Test {\n"); buf.append("\n"); buf.append("}\n"); @@ -501,9 +500,8 @@ public void testQualifiedType() throws Exception { buf.append("package test2;\n"); buf.append("\n"); buf.append("/**\n"); - buf.append(" * \n"); + buf.append(" * Test\n"); buf.append(" */\n"); - buf.append("\n"); buf.append("public interface Test {\n"); buf.append("\n"); buf.append("}\n"); @@ -513,9 +511,8 @@ public void testQualifiedType() throws Exception { buf.append("package test2;\n"); buf.append("\n"); buf.append("/**\n"); - buf.append(" * \n"); + buf.append(" * Test\n"); buf.append(" */\n"); - buf.append("\n"); buf.append("public enum Test {\n"); buf.append("\n"); buf.append("}\n"); @@ -615,9 +612,8 @@ public void testTypeInSuperType() throws Exception { buf.append("package test1;\n"); buf.append("\n"); buf.append("/**\n"); - buf.append(" * \n"); + buf.append(" * XXX\n"); buf.append(" */\n"); - buf.append("\n"); buf.append("public class XXX {\n"); buf.append("\n"); buf.append("}\n"); @@ -642,9 +638,8 @@ public void testTypeCreation() throws Exception { buf.append("package test1;\n"); buf.append("\n"); buf.append("/**\n"); - buf.append(" * \n"); + buf.append(" * XXX\n"); buf.append(" */\n"); - buf.append("\n"); buf.append("public class XXX {\n"); buf.append("\n"); buf.append("}\n"); @@ -674,9 +669,8 @@ public void testTypeInSuperInterface() throws Exception { buf.append("package test1;\n"); buf.append("\n"); buf.append("/**\n"); - buf.append(" * \n"); + buf.append(" * XXX\n"); buf.append(" */\n"); - buf.append("\n"); buf.append("public interface XXX {\n"); buf.append("\n"); buf.append("}\n"); @@ -699,9 +693,8 @@ public void testTypeInAnnotation() throws Exception { buf.append("package test1;\n"); buf.append("\n"); buf.append("/**\n"); - buf.append(" * \n"); + buf.append(" * Xyz\n"); buf.append(" */\n"); - buf.append("\n"); buf.append("public @interface Xyz {\n"); buf.append("\n"); buf.append("}\n"); @@ -724,9 +717,8 @@ public void testTypeInAnnotation_bug153881() throws Exception { buf.append("package scratch;\n"); buf.append("\n"); buf.append("/**\n"); - buf.append(" * \n"); + buf.append(" * Unimportant\n"); buf.append(" */\n"); - buf.append("\n"); buf.append("public @interface Unimportant {\n"); buf.append("\n"); buf.append("}\n"); @@ -945,9 +937,8 @@ public void testParameterizedType1() throws Exception { buf.append("package test1;\n"); buf.append("\n"); buf.append("/**\n"); - buf.append(" * \n"); + buf.append(" * XXY\n"); buf.append(" */\n"); - buf.append("\n"); buf.append("public class XXY {\n"); buf.append("\n"); buf.append("}\n"); @@ -957,9 +948,8 @@ public void testParameterizedType1() throws Exception { buf.append("package test1;\n"); buf.append("\n"); buf.append("/**\n"); - buf.append(" * \n"); + buf.append(" * XXY\n"); buf.append(" */\n"); - buf.append("\n"); buf.append("public interface XXY {\n"); buf.append("\n"); buf.append("}\n"); @@ -1507,9 +1497,8 @@ public void testTypeInSealedTypeDeclaration() throws Exception { buf.append("package test1;\n"); buf.append("\n"); buf.append("/**\n"); - buf.append(" * \n"); + buf.append(" * F\n"); buf.append(" */\n"); - buf.append("\n"); buf.append("public final class F implements E {\n"); buf.append("\n"); buf.append("}\n"); 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);