public void apply()

in tools/linter/src/main/java/com/google/cloud/verticals/foundations/dataharmonization/tools/linter/rules/SpacingArrays.java [72:125]


  public void apply(BaseTreeNode node) {
    IndexedDepthFirstIterator childWalker = node.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 (node.isInternal()
          && childWalker.peek().node() == Iterables.getLast(node.asInternal().getChildren())) {
        break;
      }

      // Insert a new indent.
      BaseTreeNode space = new TerminalNode(indent, node, 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 = node.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, node, true, TerminalNode.Type.NEWLINE);
        children.add(children.size() - 1, newlineNode);
      }
    }
  }