Skip to content

Commit

Permalink
Type definition snippets should respect java.templates.typeComment.
Browse files Browse the repository at this point in the history
Signed-off-by: Roland Grunberg <[email protected]>
  • Loading branch information
rgrunber committed Oct 23, 2024
1 parent b40aa35 commit e738410
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<IType> types = Arrays.asList(cu.getAllTypes());
int postfix = 0;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -198,23 +198,23 @@ 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
*/
public static final String CODETEMPLATE_INTERFACESNIPPET_DEFAULT = "${filecomment}${package_header}interface ${type_name} {\n\n\t${cursor}\n}";
/**
* 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
*/
public static final String CODETEMPLATE_RECORDSNIPPET_DEFAULT = "${filecomment}${package_header}record ${type_name}(${cursor}) {\n}";
/**
* 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <A>\n");
buf.append(" * @param <B>\n");
buf.append(" */\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -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<T> {\n");
buf.append("\n");
buf.append("}\n");
Expand All @@ -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<T> {\n");
buf.append("\n");
buf.append("}\n");
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e738410

Please sign in to comment.