Skip to content

Commit 1733641

Browse files
Bug 196547 - [formatter] Option to join or rewrap wrapped line comments (#1798)
1 parent 0265ed6 commit 1733641

File tree

4 files changed

+101
-4
lines changed

4 files changed

+101
-4
lines changed

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsTests.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,4 +1404,81 @@ public void testSnippet06() {
14041404
"}"
14051405
);
14061406
}
1407+
public void testJoinLineComment01() {
1408+
this.formatterPrefs.join_line_comments = true;
1409+
String source =
1410+
"""
1411+
class A {
1412+
int a = 5; // one two
1413+
// three
1414+
}
1415+
""";
1416+
formatSource(source,
1417+
"""
1418+
class A {
1419+
int a = 5; // one two three
1420+
}
1421+
""");
1422+
}
1423+
public void testJoinLineComment02() {
1424+
this.formatterPrefs.join_line_comments = true;
1425+
String source =
1426+
"""
1427+
class A {
1428+
int a = 5; // one two
1429+
// three
1430+
}
1431+
""";
1432+
formatSource(source,
1433+
"""
1434+
class A {
1435+
int a = 5; // one two
1436+
// three
1437+
}
1438+
""");
1439+
}
1440+
public void testJoinLineComment03() {
1441+
this.formatterPrefs.join_line_comments = true;
1442+
String source =
1443+
"""
1444+
class A {
1445+
int a = 5; // one two
1446+
// three
1447+
}
1448+
""";
1449+
formatSource(source,
1450+
"""
1451+
class A {
1452+
int a = 5; // one two
1453+
// three
1454+
}
1455+
""");
1456+
}
1457+
public void testJoinLineComment04() {
1458+
this.formatterPrefs.join_line_comments = true;
1459+
String source =
1460+
"""
1461+
class A {
1462+
int a = 5; // one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
1463+
// one two three four five six seven
1464+
// eight nine ten eleven twelve
1465+
// thirteen fourteen fifteen sixteen
1466+
// seventeen eighteen nineteen
1467+
// one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
1468+
// one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
1469+
}
1470+
""";
1471+
formatSource(source,
1472+
"""
1473+
class A {
1474+
int a = 5; // one two three four five six seven eight nine ten eleven twelve thirteen
1475+
// fourteen fifteen sixteen seventeen eighteen nineteen one two three four five
1476+
// six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen
1477+
// seventeen eighteen nineteen one two three four five six seven eight nine ten
1478+
// eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
1479+
// one two three four five six seven eight nine ten eleven twelve thirteen
1480+
// fourteen fifteen sixteen seventeen eighteen nineteen
1481+
}
1482+
""");
1483+
}
14071484
}

org.eclipse.jdt.core/formatter/org/eclipse/jdt/core/formatter/DefaultCodeFormatterConstants.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5270,6 +5270,16 @@ public class DefaultCodeFormatterConstants {
52705270
* @since 3.5
52715271
*/
52725272
public static final String FORMATTER_JOIN_LINES_IN_COMMENTS = JavaCore.PLUGIN_ID + ".formatter.join_lines_in_comments"; //$NON-NLS-1$
5273+
/**
5274+
* <pre>
5275+
* FORMATTER / Option to specify whether the formatter can join consecutive line comments
5276+
* - option id: "org.eclipse.jdt.core.formatter.join_line_comments"
5277+
* - possible values: { TRUE, FALSE }
5278+
* - default: FALSE
5279+
* </pre>
5280+
* @since 3.37
5281+
*/
5282+
public static final String FORMATTER_JOIN_LINE_COMMENTS = JavaCore.PLUGIN_ID + ".formatter.join_line_comments"; //$NON-NLS-1$
52735283
/**
52745284
* <pre>
52755285
* FORMATTER / Option to specify whether or not empty statement should be on a new line

org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CommentsPreparator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,14 @@ public void handleLineComment(int commentIndex) {
204204

205205
List<Token> structure = tokenizeLineComment(commentToken);
206206
if (isContinuation) {
207-
Token first = structure.get(0);
208-
first.breakBefore();
209-
first.setWrapPolicy(
210-
new WrapPolicy(WrapMode.WHERE_NECESSARY, commentIndex - 1, this.lastLineCommentPosition));
207+
if (this.options.join_line_comments) {
208+
structure.remove(0);
209+
} else {
210+
Token first = structure.get(0);
211+
first.breakBefore();
212+
first.setWrapPolicy(
213+
new WrapPolicy(WrapMode.WHERE_NECESSARY, commentIndex - 1, this.lastLineCommentPosition));
214+
}
211215

212216
// merge previous and current line comment
213217
Token previous = this.lastLineComment;

org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatterOptions.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ public static DefaultCodeFormatterOptions getJavaConventionsSettings() {
511511
public int number_of_empty_lines_to_preserve;
512512
public boolean join_wrapped_lines;
513513
public boolean join_lines_in_comments;
514+
public boolean join_line_comments;
514515
public boolean put_empty_statement_on_new_line;
515516
public int tab_size;
516517
public int page_width;
@@ -945,6 +946,7 @@ public Map<String, String> getMap() {
945946
options.put(DefaultCodeFormatterConstants.FORMATTER_NUMBER_OF_EMPTY_LINES_TO_PRESERVE, Integer.toString(this.number_of_empty_lines_to_preserve));
946947
options.put(DefaultCodeFormatterConstants.FORMATTER_JOIN_WRAPPED_LINES, this.join_wrapped_lines ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
947948
options.put(DefaultCodeFormatterConstants.FORMATTER_JOIN_LINES_IN_COMMENTS, this.join_lines_in_comments ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
949+
options.put(DefaultCodeFormatterConstants.FORMATTER_JOIN_LINE_COMMENTS, this.join_line_comments ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
948950
options.put(DefaultCodeFormatterConstants.FORMATTER_PUT_EMPTY_STATEMENT_ON_NEW_LINE, this.put_empty_statement_on_new_line ? DefaultCodeFormatterConstants.TRUE : DefaultCodeFormatterConstants.FALSE);
949951
options.put(DefaultCodeFormatterConstants.FORMATTER_LINE_SPLIT, Integer.toString(this.page_width));
950952
switch(this.tab_char) {
@@ -2514,6 +2516,8 @@ else if (JavaCore.SPACE.equals(settings.get(DefaultCodeFormatterConstants.FORMAT
25142516
if (joinLinesInCommentsOption != null) {
25152517
this.join_lines_in_comments = DefaultCodeFormatterConstants.TRUE.equals(joinLinesInCommentsOption);
25162518
}
2519+
setBoolean(settings, DefaultCodeFormatterConstants.FORMATTER_JOIN_LINE_COMMENTS, DefaultCodeFormatterConstants.TRUE,
2520+
v -> this.join_line_comments = v);
25172521
final Object joinWrappedLinesOption = settings.get(DefaultCodeFormatterConstants.FORMATTER_JOIN_WRAPPED_LINES);
25182522
if (joinWrappedLinesOption != null) {
25192523
this.join_wrapped_lines = DefaultCodeFormatterConstants.TRUE.equals(joinWrappedLinesOption);
@@ -3333,6 +3337,7 @@ public void setDefaultSettings() {
33333337
this.never_indent_line_comments_on_first_column = false;
33343338
this.number_of_empty_lines_to_preserve = 1;
33353339
this.join_lines_in_comments = true;
3340+
this.join_line_comments = false;
33363341
this.join_wrapped_lines = true;
33373342
this.put_empty_statement_on_new_line = false;
33383343
this.tab_size = 4;
@@ -3735,6 +3740,7 @@ public void setJavaConventionsSettings() {
37353740
this.never_indent_line_comments_on_first_column = false;
37363741
this.number_of_empty_lines_to_preserve = 1;
37373742
this.join_lines_in_comments = true;
3743+
this.join_line_comments = false;
37383744
this.join_wrapped_lines = true;
37393745
this.put_empty_statement_on_new_line = true;
37403746
this.tab_size = 8;

0 commit comments

Comments
 (0)