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 18, 2024
1 parent b40aa35 commit d004cd6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 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 @@ -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 @@ -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 d004cd6

Please sign in to comment.