File tree Expand file tree Collapse file tree 6 files changed +31
-7
lines changed
main/java/com/google/googlejavaformat
java/com/google/googlejavaformat/java
resources/com/google/googlejavaformat/java/testdata Expand file tree Collapse file tree 6 files changed +31
-7
lines changed Original file line number Diff line number Diff line change @@ -722,6 +722,9 @@ float computeWidth() {
722
722
if (tok .isComment ()) {
723
723
if (idx > 0 ) {
724
724
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 ;
725
728
} else {
726
729
return tok .length ();
727
730
}
@@ -731,6 +734,12 @@ float computeWidth() {
731
734
732
735
@ Override
733
736
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
+ }
734
743
return tok .getOriginalText ();
735
744
}
736
745
Original file line number Diff line number Diff line change @@ -104,6 +104,10 @@ private List<String> wrapLineComments(
104
104
List <String > lines , int column0 , JavaFormatterOptions options ) {
105
105
List <String > result = new ArrayList <>();
106
106
for (String line : lines ) {
107
+ // Add missing leading spaces to line comments: `//foo` -> `// foo`
108
+ if (!line .startsWith ("// " )) {
109
+ line = "// " + line .substring ("//" .length ());
110
+ }
107
111
while (line .length () + column0 > options .maxLineLength ()) {
108
112
int idx = options .maxLineLength () - column0 ;
109
113
// only break on whitespace characters, and ignore the leading `// `
Original file line number Diff line number Diff line change @@ -383,7 +383,7 @@ public void onlyWrapLineCommentOnWhitespace_noLeadingWhitespace() throws Excepti
383
383
"class T {\n "
384
384
+ " public static void main(\n "
385
385
+ " String[]\n "
386
- + " args) { //one_long_incredibly"
386
+ + " args) { // one_long_incredibly"
387
387
+ "_unbroken_sentence_moving_from_topic_to_topic_so_that_no-one_had_a"
388
388
+ "_chance_to_interrupt;\n "
389
389
+ " }\n "
Original file line number Diff line number Diff line change @@ -11,23 +11,23 @@ public class B20844369 {
11
11
+ "(?<tokenHash>[0-9a-fA-F]{8})" // subtype's token hash
12
12
+ ")?"; // end optional subtype
13
13
14
- int x = //foo
15
- 42 + //bar
14
+ int x = // foo
15
+ 42 + // bar
16
16
1;
17
17
18
18
int x =
19
- //foo
19
+ // foo
20
20
42
21
- + //bar
21
+ + // bar
22
22
1;
23
23
24
24
int x = /*foo*/
25
- 42 + //bar
25
+ 42 + // bar
26
26
1;
27
27
28
28
int x =
29
29
/*foo*/
30
30
42
31
- + //bar
31
+ + // bar
32
32
1;
33
33
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments