1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.indentation;
21  
22  import com.puppycrawl.tools.checkstyle.api.DetailAST;
23  
24  /**
25   * Handler for lambda expressions.
26   *
27   */
28  public class LambdaHandler extends AbstractExpressionHandler {
29  
30      /**
31       * Construct an instance of this handler with the given indentation check,
32       * abstract syntax tree, and parent handler.
33       *
34       * @param indentCheck the indentation check
35       * @param ast the abstract syntax tree
36       * @param parent the parent handler
37       */
38      public LambdaHandler(IndentationCheck indentCheck,
39                           DetailAST ast, AbstractExpressionHandler parent) {
40          super(indentCheck, "lambda", ast, parent);
41      }
42  
43      @Override
44      public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
45          return getIndent();
46      }
47  
48      /**
49       * {@inheritDoc}.
50       * @noinspection MethodWithMultipleReturnPoints
51       */
52      @Override
53      protected IndentLevel getIndentImpl() {
54          if (getParent() instanceof MethodCallHandler) {
55              return getParent().getSuggestedChildIndent(this);
56          }
57  
58          DetailAST parent = getMainAst().getParent();
59          if (getParent() instanceof NewHandler) {
60              parent = parent.getParent();
61          }
62  
63          // Use the start of the parent's line as the reference indentation level.
64          IndentLevel level = new IndentLevel(getLineStart(parent));
65  
66          // If the start of the lambda is the first element on the line;
67          // assume line wrapping with respect to its parent.
68          final DetailAST firstChild = getMainAst().getFirstChild();
69          if (getLineStart(firstChild) == firstChild.getColumnNo()) {
70              level = new IndentLevel(level, getIndentCheck().getLineWrappingIndentation());
71          }
72  
73          return level;
74      }
75  
76      @Override
77      public void checkIndentation() {
78          // If the argument list is the first element on the line
79          final DetailAST firstChild = getMainAst().getFirstChild();
80          if (getLineStart(firstChild) == firstChild.getColumnNo()) {
81              final IndentLevel level = getIndent();
82              if (!level.isAcceptable(firstChild.getColumnNo())) {
83                  logError(firstChild, "arguments", firstChild.getColumnNo(), level);
84              }
85          }
86  
87          // If the "->" is the first element on the line, assume line wrapping.
88          if (getLineStart(getMainAst()) == getMainAst().getColumnNo()) {
89              final IndentLevel level =
90                  new IndentLevel(getIndent(), getIndentCheck().getLineWrappingIndentation());
91              if (!level.isAcceptable(getMainAst().getColumnNo())) {
92                  logError(getMainAst(), "", getMainAst().getColumnNo(), level);
93              }
94          }
95      }
96  
97  }