Skip to content

Commit

Permalink
Provide file & type comments for newly created compilation units
Browse files Browse the repository at this point in the history
- Use the file & type comments (java.templates.typeComment &
  java.templates.fileHeader) settings from the client to construct new
  compilation units
- Initialize the "newtype" template which will use the file & type
  comment template values
- Add testcase

Signed-off-by: Roland Grunberg <[email protected]>
  • Loading branch information
rgrunber committed Apr 8, 2022
1 parent a60432d commit e3f5c74
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,11 @@ private CompilationUnitChange constructNewCUChange(ICompilationUnit cu) throws C
return cuChange;
}

// fileComment/typeComment not enabled.
private String constructCUContent(ICompilationUnit cu, String typeContent, String lineDelimiter) throws CoreException {
// String fileComment = getFileComment(cu, lineDelimiter);
// String typeComment = getTypeComment(cu, lineDelimiter);
String fileComment = CodeGeneration.getFileComment(cu, lineDelimiter);
String typeComment = CodeGeneration.getTypeComment(cu, cu.getElementName(), lineDelimiter);
IPackageFragment pack = (IPackageFragment) cu.getParent();
String content = CodeGeneration.getCompilationUnitContent(cu, null/*fileComment*/, null/*typeComment*/, typeContent, lineDelimiter);
String content = CodeGeneration.getCompilationUnitContent(cu, fileComment, typeComment, typeContent, lineDelimiter);
if (content != null) {

ASTParser parser = ASTParser.newParser(IASTSharedValues.SHARED_AST_LEVEL);
Expand All @@ -379,9 +378,6 @@ private String constructCUContent(ICompilationUnit cu, String typeContent, Strin
buf.append("package ").append(pack.getElementName()).append(';'); //$NON-NLS-1$
}
buf.append(lineDelimiter).append(lineDelimiter);
// if (typeComment != null) {
// buf.append(typeComment).append(lineDelimiter);
// }
buf.append(typeContent);
return buf.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
import org.eclipse.jface.text.templates.Template;

public enum CodeGenerationTemplate {
/**
* New type template
*/
NEWTYPE(CodeTemplatePreferences.CODETEMPLATE_NEWTYPE,
CodeTemplateContextType.NEWTYPE_CONTEXTTYPE,
CodeTemplatePreferences.CODETEMPLATE_NEWTYPE_DEFAULT),
/**
* File comment template
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class CodeTemplatePreferences {
public static final String BODY_SUFFIX = "body"; //$NON-NLS-1$
private static final String BLOCK_SUFFIX = "block"; //$NON-NLS-1$

/**
* A named preference that defines the template for new types
*/
public static final String CODETEMPLATE_NEWTYPE = CODETEMPLATES_PREFIX + "newtype";
/**
* A named preference that defines the template for file comments
*/
Expand Down Expand Up @@ -106,6 +110,10 @@ public class CodeTemplatePreferences {

public static final String RECORDSNIPPET_CONTEXTTYPE = "recordsnippet_context"; //$NON-NLS-1$

/**
* Default value for new type
*/
public static final String CODETEMPLATE_NEWTYPE_DEFAULT = "${filecomment}\n${package_declaration}\n\n${typecomment}\n${type_declaration}";
/**
* Default value for field comments
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public static void initialize() {
templates.put(CodeTemplatePreferences.CODETEMPLATE_SETTERBODY, CodeGenerationTemplate.SETTERBOY.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_CATCHBODY, CodeGenerationTemplate.CATCHBODY.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_METHODBODY, CodeGenerationTemplate.METHODBODY.createTemplate());
templates.put(CodeTemplatePreferences.CODETEMPLATE_NEWTYPE, CodeGenerationTemplate.NEWTYPE.createTemplate());
reloadTemplateStore();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,10 @@ public void testQualifiedType() throws Exception {
buf = new StringBuilder();
buf.append("package test2;\n");
buf.append("\n");
buf.append("/**\n");
buf.append(" * \n");
buf.append(" */\n");
buf.append("\n");
buf.append("public class Test {\n");
buf.append("\n");
buf.append("}\n");
Expand All @@ -492,6 +496,10 @@ public void testQualifiedType() throws Exception {
buf = new StringBuilder();
buf.append("package test2;\n");
buf.append("\n");
buf.append("/**\n");
buf.append(" * \n");
buf.append(" */\n");
buf.append("\n");
buf.append("public interface Test {\n");
buf.append("\n");
buf.append("}\n");
Expand All @@ -500,6 +508,10 @@ public void testQualifiedType() throws Exception {
buf = new StringBuilder();
buf.append("package test2;\n");
buf.append("\n");
buf.append("/**\n");
buf.append(" * \n");
buf.append(" */\n");
buf.append("\n");
buf.append("public enum Test {\n");
buf.append("\n");
buf.append("}\n");
Expand Down Expand Up @@ -598,6 +610,10 @@ public void testTypeInSuperType() throws Exception {
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("/**\n");
buf.append(" * \n");
buf.append(" */\n");
buf.append("\n");
buf.append("public class XXX {\n");
buf.append("\n");
buf.append("}\n");
Expand All @@ -621,6 +637,10 @@ public void testTypeCreation() throws Exception {
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("/**\n");
buf.append(" * \n");
buf.append(" */\n");
buf.append("\n");
buf.append("public class XXX {\n");
buf.append("\n");
buf.append("}\n");
Expand Down Expand Up @@ -649,6 +669,10 @@ public void testTypeInSuperInterface() throws Exception {
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("/**\n");
buf.append(" * \n");
buf.append(" */\n");
buf.append("\n");
buf.append("public interface XXX {\n");
buf.append("\n");
buf.append("}\n");
Expand All @@ -670,6 +694,10 @@ public void testTypeInAnnotation() throws Exception {
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("/**\n");
buf.append(" * \n");
buf.append(" */\n");
buf.append("\n");
buf.append("public @interface Xyz {\n");
buf.append("\n");
buf.append("}\n");
Expand All @@ -691,6 +719,10 @@ public void testTypeInAnnotation_bug153881() throws Exception {
buf = new StringBuilder();
buf.append("package scratch;\n");
buf.append("\n");
buf.append("/**\n");
buf.append(" * \n");
buf.append(" */\n");
buf.append("\n");
buf.append("public @interface Unimportant {\n");
buf.append("\n");
buf.append("}\n");
Expand Down Expand Up @@ -908,6 +940,10 @@ public void testParameterizedType1() throws Exception {
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("/**\n");
buf.append(" * \n");
buf.append(" */\n");
buf.append("\n");
buf.append("public class XXY<T> {\n");
buf.append("\n");
buf.append("}\n");
Expand All @@ -916,6 +952,10 @@ public void testParameterizedType1() throws Exception {
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("/**\n");
buf.append(" * \n");
buf.append(" */\n");
buf.append("\n");
buf.append("public interface XXY<T> {\n");
buf.append("\n");
buf.append("}\n");
Expand Down Expand Up @@ -1464,6 +1504,10 @@ public void testTypeInSealedTypeDeclaration() throws Exception {
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("/**\n");
buf.append(" * \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 @@ -164,6 +164,15 @@ public void testUpdateTypeCommentTemplate() {
assertEquals("/** */", template.getPattern());
}

@Test
public void testUpdateNewTypeTemplate() {
preferenceManager.initialize();

Template template = JavaManipulation.getCodeTemplateStore().findTemplateById(CodeTemplateContextType.NEWTYPE_ID);
assertNotNull(template);
assertEquals(CodeTemplatePreferences.CODETEMPLATE_NEWTYPE_DEFAULT, template.getPattern());
}

@Test
public void testInsertSpacesTabSize() {
preferenceManager.initialize();
Expand Down

0 comments on commit e3f5c74

Please sign in to comment.