Skip to content

Commit 1815e21

Browse files
committed
fix indentation for multi line method args
1 parent 8169884 commit 1815e21

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,14 @@ private Map<TreePath, String> computeCustomTextBlocksIndent(List<TreePath> textB
269269
if (parentToIndent.containsKey(parent)) {
270270
continue;
271271
}
272+
// Tree finalParent = (((JCMethodInvocation) parent).getMethodSelect() instanceof JCFieldAccess ?
273+
// ((JCMethodInvocation) parent).getMethodSelect() : parent;
272274
List<Tree> allArguments = new ArrayList<>(((JCMethodInvocation) parent).getArguments());
273-
parentToIndent.put(parent, computePrefixIndentation(parent, allArguments));
275+
276+
parentToIndent.put(
277+
parent,
278+
computePrefixIndentation(
279+
((JCMethodInvocation) parent).getMethodSelect(), allArguments, false));
274280
} else if (parent.getKind() == Kind.PLUS) {
275281
while (parentPath.getParentPath().getLeaf().getKind() == Kind.PLUS) {
276282
parentPath = parentPath.getParentPath();
@@ -280,19 +286,24 @@ private Map<TreePath, String> computeCustomTextBlocksIndent(List<TreePath> textB
280286
if (parentToIndent.containsKey(parent)) {
281287
continue;
282288
}
283-
parentToIndent.put(parent, computePrefixIndentation(parent, flattenExpressionTree(parent)));
289+
parentToIndent.put(parent, computePrefixIndentation(parent, flattenExpressionTree(parent), true));
284290
}
285291
}
286292

287293
return textBlockToParent.entrySet().stream()
288294
.collect(Collectors.toMap(Map.Entry::getKey, e -> parentToIndent.get(e.getValue())));
289295
}
290296

291-
private String computePrefixIndentation(Tree parentPath, List<Tree> childExpressions) {
297+
private String computePrefixIndentation(
298+
Tree parentPath, List<Tree> childExpressions, boolean shouldUseStartLineParent) {
292299
int startParentPosition = getStartPosition(parentPath);
293300
int startParentLine = lineMap.getLineNumber(startParentPosition);
294301
int endParentPosition = getEndPosition(unit, parentPath);
295-
int lineParentStartPosition = lineMap.getStartPosition(startParentLine);
302+
int endParentLine = lineMap.getLineNumber(endParentPosition);
303+
// the only relevant lineParentEndPosition should be the one that doesn't end with a """
304+
// because that one is not valid
305+
int lineParentStartPosition =
306+
lineMap.getStartPosition(shouldUseStartLineParent ? startParentLine : endParentLine);
296307
int startParentColumn = CharMatcher.whitespace()
297308
.negate()
298309
.indexIn(input.substring(lineParentStartPosition, endParentPosition));
@@ -305,8 +316,9 @@ private String computePrefixIndentation(Tree parentPath, List<Tree> childExpress
305316
int endPos = getEndPosition(unit, expression);
306317
int endLine = lineMap.getLineNumber(endPos);
307318
allRanges.add(Range.closed(startLine, endLine));
319+
int parentLine = shouldUseStartLineParent ? startParentLine : endParentLine;
308320
// ignore indentation if the current argument is on the same line as the parent
309-
if (startLine == startParentLine) {
321+
if (startLine == parentLine) {
310322
continue;
311323
}
312324
// if this is a line that ends with a textBlock (ending with a textBlock if the line starts with

palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/RSL.input

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,38 @@ class RSLs {
154154
""", """
155155
ccc
156156
""");
157+
158+
String x = String.format("""
159+
this very long string that does something using arguments %s %s %s
160+
""",
161+
"@", "@", "something");
162+
163+
String x = String.format(someExtraValue ("""
164+
this very long string that does something using arguments %s %s %s
165+
""" + """
166+
my other value
167+
"""),
168+
"@", "@", "something");
169+
170+
CreateABuilder value = CreateABuilder.builder().v("""
171+
this is a content that should be indented
172+
""")
173+
.otherValue("my other values")
174+
.build();
175+
176+
CreateABuilder value = CreateABuilder.builder().v("""
177+
this is a content that should be indented
178+
""" + """
179+
and this one
180+
""")
181+
.otherValue("my other values")
182+
.build();
183+
184+
CreateABuilder value = CreateABuilder.builder()
185+
.myValue("this is a value")
186+
.someTextBlockContent("""
187+
this is a content that should be indented
188+
""")
189+
.otherValue("my other values")
190+
.build();
157191
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,34 @@ class RSLs {
152152
"""
153153
ccc
154154
""");
155+
156+
String x = String.format("""
157+
this very long string that does something using arguments %s %s %s
158+
""", "@", "@", "something");
159+
160+
String x = String.format(someExtraValue("""
161+
this very long string that does something using arguments %s %s %s
162+
""" + """
163+
my other value
164+
"""), "@", "@", "something");
165+
166+
CreateABuilder value =
167+
CreateABuilder.builder().v("""
168+
this is a content that should be indented
169+
""").otherValue("my other values").build();
170+
171+
CreateABuilder value =
172+
CreateABuilder.builder().v("""
173+
this is a content that should be indented
174+
""" + """
175+
and this one
176+
""").otherValue("my other values").build();
177+
178+
CreateABuilder value = CreateABuilder.builder()
179+
.myValue("this is a value")
180+
.someTextBlockContent("""
181+
this is a content that should be indented
182+
""")
183+
.otherValue("my other values")
184+
.build();
155185
}

0 commit comments

Comments
 (0)