View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.checkstyle;
6   
7   import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
8   import com.puppycrawl.tools.checkstyle.api.DetailAST;
9   import com.puppycrawl.tools.checkstyle.api.TokenTypes;
10  
11  /**
12   * Checks node/closing brackets to be the last symbols on the line.
13   *
14   * <p>This is how a correct bracket structure should look like:
15   *
16   * <pre>
17   * String text = String.format(
18   *   "some text: %s",
19   *   new Foo().with(
20   *     "abc",
21   *     "foo"
22   *   )
23   * );
24   * </pre>
25   *
26   * <p>The motivation for such formatting is simple - we want to see the entire
27   * block as fast as possible. When you look at a block of code you should be
28   * able to see where it starts and where it ends. In exactly the same way
29   * we organize curled brackets.
30   *
31   * <p>In other words, when you open a bracket and can't close it at the same
32   * line - you should leave it as the last symbol at this line.
33   *
34   * @since 0.3
35   */
36  public final class BracketsStructureCheck extends AbstractCheck {
37  
38      @Override
39      public int[] getDefaultTokens() {
40          return new int[] {
41              TokenTypes.LITERAL_NEW,
42              TokenTypes.METHOD_CALL,
43          };
44      }
45  
46      @Override
47      public int[] getAcceptableTokens() {
48          return this.getDefaultTokens();
49      }
50  
51      @Override
52      public int[] getRequiredTokens() {
53          return this.getDefaultTokens();
54      }
55  
56      @Override
57      public void visitToken(final DetailAST ast) {
58          if (ast.getType() == TokenTypes.METHOD_CALL
59              || ast.getType() == TokenTypes.LITERAL_NEW) {
60              this.checkParams(ast);
61          } else {
62              final DetailAST brackets = ast.findFirstToken(TokenTypes.LPAREN);
63              if (brackets != null) {
64                  this.checkParams(brackets);
65              }
66          }
67      }
68  
69      /**
70       * Checks params statement to satisfy the rule.
71       * @param node Tree node, containing method call statement.
72       */
73      private void checkParams(final DetailAST node) {
74          final DetailAST closing = node.findFirstToken(TokenTypes.RPAREN);
75          if (closing != null) {
76              this.checkLines(node, node.getLineNo(), closing.getLineNo());
77          }
78      }
79  
80      /**
81       * Checks params statement to satisfy the rule.
82       * @param node Tree node, containing method call statement.
83       * @param start First line
84       * @param end Final line
85       */
86      private void checkLines(final DetailAST node, final int start,
87          final int end) {
88          if (start != end) {
89              final DetailAST elist = node.findFirstToken(TokenTypes.ELIST);
90              final int pline = elist.getLineNo();
91              if (pline == start) {
92                  this.log(pline, "Parameters should start on a new line");
93              }
94              this.checkExpressionList(elist, end);
95          }
96      }
97  
98      /**
99       * Checks expression list if closing bracket is on new line.
100      * @param elist Tree node, containing expression list
101      * @param end Final line
102      */
103     private void checkExpressionList(final DetailAST elist, final int end) {
104         if (elist.getChildCount() > 0) {
105             DetailAST last = elist.getLastChild();
106             while (last.getChildCount() > 0) {
107                 last = last.getLastChild();
108             }
109             final int lline = last.getLineNo();
110             if (lline == end) {
111                 this.log(lline, "Closing bracket should be on a new line");
112             }
113         }
114     }
115 
116 }