Skip to content

Commit f9bc8dd

Browse files
committed
comments
1 parent d52de5e commit f9bc8dd

2 files changed

Lines changed: 13 additions & 17 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2655,7 +2655,6 @@ void visitDot(ExpressionTree node0) {
26552655
List<ExpressionTree> items = new ArrayList<>(stack);
26562656

26572657
boolean needDot = false;
2658-
boolean isTextBlock = false;
26592658

26602659
// The dot chain started with a primary expression: output it normally, and indent
26612660
// the rest of the chain +4.
@@ -2667,6 +2666,7 @@ void visitDot(ExpressionTree node0) {
26672666
scan(getArrayBase(node), null);
26682667
token(".");
26692668
} else {
2669+
boolean isTextBlock = false;
26702670
// Special case for text blocks: if the node is a string literal that ends with """,
26712671
// don't add a break after it
26722672
if (node instanceof LiteralTree && node.getKind() == Tree.Kind.STRING_LITERAL) {

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
172172
return null;
173173
}
174174
int pos = getStartPosition(literalTree);
175-
if (input.substring(pos, Math.min(input.length(), pos + 3)).equals(TEXT_BLOCK_DELIMITER)) {
175+
if (input.substring(pos, Math.min(input.length(), pos + TEXT_BLOCK_DELIMITER.length()))
176+
.equals(TEXT_BLOCK_DELIMITER)) {
176177
textBlocks.add(getCurrentPath());
177178
return null;
178179
}
@@ -269,11 +270,8 @@ private Map<TreePath, String> computeCustomTextBlocksIndent(List<TreePath> textB
269270
Tree parent = parentPath.getLeaf();
270271
if (parent instanceof MethodInvocationTree) {
271272
textBlockToParent.put(textBlock, parent);
272-
if (parentToIndent.containsKey(parent)) {
273-
continue;
274-
}
275273
List<Tree> allArguments = new ArrayList<>(((JCMethodInvocation) parent).getArguments());
276-
parentToIndent.put(
274+
parentToIndent.computeIfAbsent(
277275
parent,
278276
// A method can be split in multiple lines (eg. for field access
279277
// Class.builder()
@@ -282,18 +280,18 @@ private Map<TreePath, String> computeCustomTextBlocksIndent(List<TreePath> textB
282280
// In this case the parent of the arguments should be the method ("Class.builder().method")
283281
// the indentation of the arguments will be relative to the indentation of the last line of
284282
// the method name.
285-
computePrefixIndentation(
286-
((JCMethodInvocation) parent).getMethodSelect(), allArguments, false));
283+
parentTree -> computePrefixIndentation(
284+
((JCMethodInvocation) parentTree).getMethodSelect(), allArguments, false));
287285
} else if (parent.getKind() == Kind.PLUS) {
288286
while (parentPath.getParentPath().getLeaf().getKind() == Kind.PLUS) {
289287
parentPath = parentPath.getParentPath();
290288
}
291-
parent = parentPath.getLeaf();
292-
textBlockToParent.put(textBlock, parent);
293-
if (parentToIndent.containsKey(parent)) {
294-
continue;
295-
}
296-
parentToIndent.put(parent, computePrefixIndentation(parent, flattenExpressionTree(parent), true));
289+
Tree concatenationRoot = parentPath.getLeaf();
290+
textBlockToParent.put(textBlock, concatenationRoot);
291+
parentToIndent.computeIfAbsent(
292+
concatenationRoot,
293+
concatenationRootTree -> computePrefixIndentation(
294+
concatenationRootTree, flattenExpressionTree(concatenationRootTree), true));
297295
}
298296
}
299297

@@ -313,14 +311,12 @@ private String computePrefixIndentation(
313311
.negate()
314312
.indexIn(input.substring(lineParentStartPosition, endParentPosition));
315313
int extraIndent = 4;
316-
RangeSet<Integer> allRanges = TreeRangeSet.create();
317314
for (Tree expression : childExpressions) {
318315
int startingPos = getStartPosition(expression);
319316
int startLine = lineMap.getLineNumber(startingPos);
320317
int lineStartPosition = lineMap.getStartPosition(startLine);
321318
int endPos = getEndPosition(unit, expression);
322319
int endLine = lineMap.getLineNumber(endPos);
323-
allRanges.add(Range.closed(startLine, endLine));
324320
int parentLine = shouldUseStartLineParent ? startParentLine : endParentLine;
325321
// ignore indentation if the current argument is on the same line as the parent
326322
if (startLine == parentLine) {
@@ -329,7 +325,7 @@ private String computePrefixIndentation(
329325
// if this is a line that ends a textBlock (if the line starts with triple quotes & the current tree
330326
// starts after)
331327
int startColumn = CharMatcher.whitespace().negate().indexIn(input.substring(lineStartPosition, endPos));
332-
if (input.startsWith("\"\"\"", lineStartPosition + startColumn)
328+
if (input.startsWith(TEXT_BLOCK_DELIMITER, lineStartPosition + startColumn)
333329
&& startingPos != lineStartPosition + startColumn) {
334330
continue;
335331
}

0 commit comments

Comments
 (0)