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() {
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
Original file line number Diff line number Diff 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 `// `
Original file line number Diff line number Diff 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 "
Original file line number Diff line number Diff 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}
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