Skip to content

Commit 0265ed6

Browse files
[formatter] Fixed exception when moving NLS tags (#1624)
1 parent 7d2442b commit 0265ed6

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13361,4 +13361,37 @@ public void testIssue1510() {
1336113361
"\t\t\t\t\"\"\"; //$NON-NLS-1$\n" +
1336213362
"\t}\n" +
1336313363
"}");
13364-
}}
13364+
}
13365+
/**
13366+
* https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1624
13367+
*/
13368+
public void testIssue1624() {
13369+
this.formatterPrefs.use_tabs_only_for_leading_indentations = true;
13370+
this.formatterPrefs.alignment_for_logical_operator = Alignment.M_ONE_PER_LINE_SPLIT | Alignment.M_FORCE;
13371+
String source =
13372+
"""
13373+
class A {
13374+
private void foo(String a) {
13375+
boolean b = "GET".equals(a) || "POST".equals(a); //$NON-NLS-1$//$NON-NLS-2$
13376+
if (a == null)
13377+
a = "W"; //$NON-NLS-1$
13378+
// foo
13379+
// bar
13380+
}
13381+
}
13382+
""";
13383+
formatSource(source,
13384+
"""
13385+
class A {
13386+
private void foo(String a) {
13387+
boolean b = "GET".equals(a) //$NON-NLS-1$
13388+
|| "POST".equals(a); //$NON-NLS-1$
13389+
if (a == null)
13390+
a = "W"; //$NON-NLS-1$
13391+
// foo
13392+
// bar
13393+
}
13394+
}
13395+
""");
13396+
}
13397+
}

org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/linewrap/WrapExecutor.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2014, 2020 Mateusz Matela and others.
2+
* Copyright (c) 2014, 2023 Mateusz Matela and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -353,6 +353,7 @@ protected boolean token(final Token token, final int index) {
353353
lineComment.setAlign(WrapExecutor.this.tm.getNLSAlign(index));
354354
lineComment.setInternalStructure(new ArrayList<>());
355355
WrapExecutor.this.tm.insert(index + 1, lineComment);
356+
fixWrapPolicyParents(index, 1);
356357
structureChanged();
357358
return true; // will fill the line comment structure in next step
358359
}
@@ -413,13 +414,38 @@ protected boolean token(final Token token, final int index) {
413414
|| (structure.size() == 1 && structure.get(0).tokenType == TokenNameWHITESPACE)) {
414415
// all the tags have been moved to other lines
415416
WrapExecutor.this.tm.remove(index);
417+
fixWrapPolicyParents(index, -1);
416418
structureChanged();
417419
}
418420

419421
this.nlsTags.clear();
420422
}
421423
return true;
422424
}
425+
426+
private void fixWrapPolicyParents(int changeIndex, int delta) {
427+
TokenTraverser traverser = new TokenTraverser() {
428+
HashMap<WrapPolicy, WrapPolicy> policyCache = new HashMap<>();
429+
430+
@Override
431+
protected boolean token(Token token, int index) {
432+
WrapPolicy policy = token.getWrapPolicy();
433+
if (policy != null && policy.wrapParentIndex > changeIndex) {
434+
WrapPolicy changedWp = this.policyCache.computeIfAbsent(policy,
435+
p -> new WrapPolicy(p.wrapMode, p.wrapParentIndex + delta,
436+
p.groupEndIndex == -1 ? -1 : p.groupEndIndex + delta, p.extraIndent,
437+
p.structureDepth, p.penaltyMultiplier, p.isFirstInGroup, p.indentOnColumn));
438+
token.setWrapPolicy(changedWp);
439+
}
440+
if (token.tokenType == TokenNameCOMMENT_LINE && token.getInternalStructure() != null) {
441+
traverse(token.getInternalStructure(), 0);
442+
structureChanged();
443+
}
444+
return true;
445+
}
446+
};
447+
WrapExecutor.this.tm.traverse(changeIndex, traverser);
448+
}
423449
}
424450

425451
private final static int[] EMPTY_ARRAY = {};

0 commit comments

Comments
 (0)