Skip to content

Commit bc94435

Browse files
cushonronshapiro
authored andcommitted
Ensure leading space is present in line comments
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=156080131
1 parent 33d7112 commit bc94435

File tree

6 files changed

+31
-7
lines changed

6 files changed

+31
-7
lines changed

core/src/main/java/com/google/googlejavaformat/Doc.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,9 @@ float computeWidth() {
722722
if (tok.isComment()) {
723723
if (idx > 0) {
724724
return idx;
725+
} else if (tok.isSlashSlashComment() && !tok.getOriginalText().startsWith("// ")) {
726+
// Account for line comments with missing spaces, see computeFlat.
727+
return tok.length() + 1;
725728
} else {
726729
return tok.length();
727730
}
@@ -731,6 +734,12 @@ float computeWidth() {
731734

732735
@Override
733736
String computeFlat() {
737+
// TODO(cushon): commentsHelper.rewrite doesn't get called for spans that fit in a single
738+
// line. That's fine for multi-line comment reflowing, but problematic for adding missing
739+
// spaces in line comments.
740+
if (tok.isSlashSlashComment() && !tok.getOriginalText().startsWith("// ")) {
741+
return "// " + tok.getOriginalText().substring("//".length());
742+
}
734743
return tok.getOriginalText();
735744
}
736745

core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ private List<String> wrapLineComments(
104104
List<String> lines, int column0, JavaFormatterOptions options) {
105105
List<String> result = new ArrayList<>();
106106
for (String line : lines) {
107+
// Add missing leading spaces to line comments: `//foo` -> `// foo`
108+
if (!line.startsWith("// ")) {
109+
line = "// " + line.substring("//".length());
110+
}
107111
while (line.length() + column0 > options.maxLineLength()) {
108112
int idx = options.maxLineLength() - column0;
109113
// only break on whitespace characters, and ignore the leading `// `

core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public void onlyWrapLineCommentOnWhitespace_noLeadingWhitespace() throws Excepti
383383
"class T {\n"
384384
+ " public static void main(\n"
385385
+ " String[]\n"
386-
+ " args) { //one_long_incredibly"
386+
+ " args) { // one_long_incredibly"
387387
+ "_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a"
388388
+ "_chance_to_interrupt;\n"
389389
+ " }\n"

core/src/test/resources/com/google/googlejavaformat/java/testdata/B20844369.output

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ public class B20844369 {
1111
+ "(?<tokenHash>[0-9a-fA-F]{8})" // subtype's token hash
1212
+ ")?"; // end optional subtype
1313

14-
int x = //foo
15-
42 + //bar
14+
int x = // foo
15+
42 + // bar
1616
1;
1717

1818
int x =
19-
//foo
19+
// foo
2020
42
21-
+ //bar
21+
+ // bar
2222
1;
2323

2424
int x = /*foo*/
25-
42 + //bar
25+
42 + // bar
2626
1;
2727

2828
int x =
2929
/*foo*/
3030
42
31-
+ //bar
31+
+ // bar
3232
1;
3333
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class B38241237 {
2+
//foo
3+
//bar
4+
//one long incredibly unbroken sentence moving from topic to topic so that no-one had a chance to interrupt
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class B38241237 {
2+
// foo
3+
// bar
4+
// one long incredibly unbroken sentence moving from topic to topic so that no-one had a chance to
5+
// interrupt
6+
}

0 commit comments

Comments
 (0)