Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e415213

Browse files
[ggj]fix: fix block comment to construct file header (#168)
* fix bazel rules * multiple line comment * add edge unit test * use helper
1 parent d6e8893 commit e415213

File tree

4 files changed

+37
-23
lines changed

4 files changed

+37
-23
lines changed

src/main/java/com/google/api/generator/engine/ast/BlockComment.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.google.api.generator.engine.ast;
1616

17-
import com.google.api.generator.engine.escaper.MetacharEscaper;
1817
import com.google.auto.value.AutoValue;
1918

2019
@AutoValue
@@ -38,14 +37,6 @@ public static BlockComment withComment(String comment) {
3837
public abstract static class Builder {
3938
public abstract Builder setComment(String comment);
4039

41-
// Private accessor.
42-
abstract String comment();
43-
44-
public abstract BlockComment autoBuild();
45-
46-
public BlockComment build() {
47-
setComment(MetacharEscaper.escaper(comment()));
48-
return autoBuild();
49-
}
40+
public abstract BlockComment build();
5041
}
5142
}

src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ public class JavaWriterVisitor implements AstNodeVisitor {
6464

6565
private static final String COLON = ":";
6666
private static final String COMMA = ",";
67-
private static final String BLOCK_COMMENT_START = "/**";
67+
private static final String BLOCK_COMMENT_START = "/*";
6868
private static final String BLOCK_COMMENT_END = "*/";
6969
private static final String DOT = ".";
7070
private static final String ESCAPED_QUOTE = "\"";
7171
private static final String EQUALS = "=";
7272
private static final String LEFT_ANGLE = "<";
7373
private static final String LEFT_BRACE = "{";
7474
private static final String LEFT_PAREN = "(";
75+
private static final String JAVADOC_COMMENT_START = "/**";
7576
private static final String QUESTION_MARK = "?";
7677
private static final String RIGHT_ANGLE = ">";
7778
private static final String RIGHT_BRACE = "}";
@@ -518,17 +519,21 @@ public void visit(LineComment lineComment) {
518519
}
519520

520521
public void visit(BlockComment blockComment) {
521-
// Split comments by new line and embrace the comment block with `/** */`.
522-
String sourceComment = blockComment.comment();
523-
String formattedSource =
524-
JavaFormatter.format(
525-
String.format("%s %s %s", BLOCK_COMMENT_START, sourceComment, BLOCK_COMMENT_END));
526-
buffer.append(formattedSource);
522+
// Split comments by new line and embrace the comment block with `/* */`.
523+
StringBuilder sourceComment = new StringBuilder();
524+
sourceComment.append(BLOCK_COMMENT_START).append(NEWLINE);
525+
Arrays.stream(blockComment.comment().split("\\r?\\n"))
526+
.forEach(
527+
comment -> {
528+
sourceComment.append(String.format("%s %s%s", ASTERISK, comment, NEWLINE));
529+
});
530+
sourceComment.append(BLOCK_COMMENT_END);
531+
buffer.append(JavaFormatter.format(sourceComment.toString()));
527532
}
528533

529534
public void visit(JavaDocComment javaDocComment) {
530535
StringBuilder sourceComment = new StringBuilder();
531-
sourceComment.append(BLOCK_COMMENT_START).append(NEWLINE);
536+
sourceComment.append(JAVADOC_COMMENT_START).append(NEWLINE);
532537
Arrays.stream(javaDocComment.comment().split("\\r?\\n"))
533538
.forEach(
534539
comment -> {

src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public void writeNewObjectExprImports_withArgs() {
9494
.setArguments(Arrays.asList(fileExpr))
9595
.build();
9696
newObjectExpr.accept(writerVisitor);
97-
System.out.println(writerVisitor.write());
9897
assertEquals(
9998
writerVisitor.write(),
10099
String.format(

src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ public void writeBlockCommentStatement_basic() {
343343
String content = "this is a test comment";
344344
BlockComment blockComment = BlockComment.builder().setComment(content).build();
345345
CommentStatement commentStatement = CommentStatement.withComment(blockComment);
346-
String expected = "/** this is a test comment */\n";
346+
String expected = String.format(createLines(3), "/*\n", "* this is a test comment\n", "*/\n");
347347
commentStatement.accept(writerVisitor);
348348
assertEquals(writerVisitor.write(), expected);
349349
}
@@ -421,11 +421,30 @@ public void writeJavaDocCommentStatement_allComponents() {
421421
}
422422

423423
@Test
424-
public void writeBlockComment_specialChar() {
425-
String content = "Testing special characters: \b\t\n\r\"`'?/\\,.[]{}|-_!@#$%^()";
424+
public void writeBlockComment_shortLines() {
425+
String content = "Apache License \nThis is a test file header";
426426
BlockComment blockComment = BlockComment.builder().setComment(content).build();
427427
String expected =
428-
"/** Testing special characters: \\b\\t\\n\\r\"`'?/\\\\,.[]{}|-_!@#$%^() */\n";
428+
String.format(
429+
createLines(4), "/*\n", "* Apache License\n", "* This is a test file header\n", "*/\n");
430+
blockComment.accept(writerVisitor);
431+
assertEquals(writerVisitor.write(), expected);
432+
}
433+
434+
@Test
435+
public void writeBlockComment_newLineInBetween() {
436+
String content =
437+
"Apache License \nLicensed under the Apache License, Version 2.0 (the \"License\");\n\nyou may not use this file except in compliance with the License.";
438+
BlockComment blockComment = BlockComment.builder().setComment(content).build();
439+
String expected =
440+
String.format(
441+
createLines(6),
442+
"/*\n",
443+
"* Apache License\n",
444+
"* Licensed under the Apache License, Version 2.0 (the \"License\");\n",
445+
"*\n",
446+
"* you may not use this file except in compliance with the License.\n",
447+
"*/\n");
429448
blockComment.accept(writerVisitor);
430449
assertEquals(writerVisitor.write(), expected);
431450
}

0 commit comments

Comments
 (0)