in tools/linter/src/main/java/com/google/cloud/verticals/foundations/dataharmonization/tools/linter/rules/SpacingFunctionCall.java [76:130]
private void addIndents(BaseTreeNode treeNode) {
IndexedDepthFirstIterator childWalker = treeNode.dfsWalker();
while (childWalker.hasNext()) {
TreeChildReference entry = childWalker.next();
// If the node is internal, or a non-newline or non-comment terminal node, do not enter the
// inner loop.
if (entry.node().isInternal() || !(entry.node().isNewline() || entry.node().isComment())) {
continue;
}
// If a newline or comment is found, start trimming the spaces after it.
while (childWalker.hasNext()) {
TreeChildReference maybeSpaceEntry = childWalker.peek();
// If a non-whitespace or newline or linter created node is reached, stop trimming spaces.
if (!maybeSpaceEntry.node().isWhitespace()
|| maybeSpaceEntry.node().isNewline()
|| maybeSpaceEntry.node().isLinterCreated()) {
break;
}
// Remove existing spaces.
childWalker.next();
childWalker.remove();
}
// If the current and next nodes are newlines, do not insert an indent.
if (entry.node().isNewline() && childWalker.peek().node().isNewline()) {
continue;
}
// If the next token is the last token of treeNode (i.e. the last )) we're done.
if (treeNode.isInternal()
&& childWalker.peek().node() == Iterables.getLast(treeNode.asInternal().getChildren())) {
break;
}
// Insert a new indent.
BaseTreeNode space = new TerminalNode(indent, treeNode, true, TerminalNode.Type.SPACE);
childWalker.insert(space);
// The second to last token should be a newline or whitespace preceded by a newline, to ensure
// that the ')' is on its own line.
List<BaseTreeNode> children = treeNode.asInternal().getChildren();
if (!(children.get(children.size() - 2).isNewline()
|| (children.get(children.size() - 2).isWhitespace()
&& children.get(children.size() - 3).isNewline()))) {
BaseTreeNode newlineNode =
new TerminalNode(NEWLINE, treeNode, true, TerminalNode.Type.NEWLINE);
children.add(children.size() - 1, newlineNode);
}
}
}