Skip to content

Commit dfc5cf1

Browse files
dansanduleacbulldozer-bot[bot]
authored andcommitted
Fix: NON-NLS comments move before + in string concatenations (#45)
NON-NLS comments are moved behind `+` tokens in string concatenations. This is a heuristic that doesn't look at what the thing before the `+` is, so it might produce confusing results.
1 parent 5b16d8f commit dfc5cf1

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

changelog/@unreleased/pr-45.v2.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type: fix
2+
fix:
3+
description: NON-NLS comments are moved behind `+` tokens in string concatenations.
4+
This is a heuristic that doesn't look at what the thing before the `+` is, so
5+
it might produce confusing results.
6+
links:
7+
- https://github.com/palantir/palantir-java-format/pull/45

palantir-java-format/src/main/java/com/palantir/javaformat/OpsBuilder.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,21 +541,32 @@ public final ImmutableList<Op> build() {
541541
tokOps.put(j, SPACE);
542542
}
543543
// Now we've seen the Token; output the toksAfter.
544+
545+
// Reordering of NON-NLS comments that might follow a `+` in a chain of string concatenations, in
546+
// order to move the comments before the Break that precedes the `+` token.
547+
boolean nonNlsCommentsAfterPlus = token.getToksAfter().stream()
548+
.anyMatch(OpsBuilder::isNonNlsComment)
549+
&& token.getTok().getText().equals("+")
550+
&& k > 0
551+
&& ops.get(k - 1) instanceof Break;
552+
553+
int tokAfterPos = nonNlsCommentsAfterPlus ? k - 1 : k + 1;
554+
544555
for (Input.Tok tokAfter : token.getToksAfter()) {
545556
if (tokAfter.isComment()) {
546557
boolean breakAfter = tokAfter.isJavadocComment()
547558
|| (tokAfter.isSlashStarComment()
548559
&& tokenOp.breakAndIndentTrailingComment().isPresent());
549560
if (breakAfter) {
550-
tokOps.put(k + 1, Break.make(
561+
tokOps.put(tokAfterPos, Break.make(
551562
FillMode.FORCED, "", tokenOp.breakAndIndentTrailingComment().orElse(
552563
Const.ZERO)));
553564
} else {
554-
tokOps.put(k + 1, SPACE);
565+
tokOps.put(tokAfterPos, SPACE);
555566
}
556-
tokOps.putAll(k + 1, makeComment(tokAfter));
567+
tokOps.putAll(tokAfterPos, makeComment(tokAfter));
557568
if (breakAfter) {
558-
tokOps.put(k + 1, Break.make(FillMode.FORCED, "", ZERO));
569+
tokOps.put(tokAfterPos, Break.make(FillMode.FORCED, "", ZERO));
559570
}
560571
}
561572
}
@@ -620,6 +631,10 @@ public final ImmutableList<Op> build() {
620631
return newOps.build();
621632
}
622633

634+
private static boolean isNonNlsComment(Input.Tok tokAfter) {
635+
return tokAfter.isSlashSlashComment() && tokAfter.getText().contains("$NON-NLS");
636+
}
637+
623638
private static boolean isForcedBreak(Op op) {
624639
return op instanceof Break && ((Break) op).isForced();
625640
}

palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/NON-NLS.output

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ package test;
33
class MultilineStringConstant {
44

55
// NON-NLS comments are required for i18n, it's important they are kept with their strings.
6-
private static final String MULTIPLE_LINE_NON_NLS = "field_0,"
7-
+ //$NON-NLS-1$
8-
"field_1,"
9-
+ //$NON-NLS-1$
10-
"field_2,"
11-
+ //$NON-NLS-1$
12-
"field_3,"
13-
+ //$NON-NLS-1$
14-
"field_4"; //$NON-NLS-1$
6+
private static final String MULTIPLE_LINE_NON_NLS = "field_0," //$NON-NLS-1$
7+
+ "field_1," //$NON-NLS-1$
8+
+ "field_2," //$NON-NLS-1$
9+
+ "field_3," //$NON-NLS-1$
10+
+ "field_4"; //$NON-NLS-1$
1511

1612
private static final String MULTIPLE_LINE_NO_COMMENT =
1713
"field_0," + "field_1," + "field_2," + "field_3," + "field_4";

0 commit comments

Comments
 (0)