View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.gui;
21  
22  import java.util.List;
23  
24  import com.google.common.collect.ImmutableList;
25  import com.puppycrawl.tools.checkstyle.api.DetailAST;
26  import com.puppycrawl.tools.checkstyle.api.DetailNode;
27  import com.puppycrawl.tools.checkstyle.utils.TokenUtils;
28  
29  /**
30   * Presentation model for CodeSelector.
31   * @author unknown
32   */
33  public class CodeSelectorPresentation {
34      /** DetailAST or DetailNode node. */
35      private final Object node;
36      /** Mapping. */
37      private final List<Integer> lines2position;
38      /** Selection start position. */
39      private int selectionStart;
40      /** Selection end position. */
41      private int selectionEnd;
42  
43      /**
44       * Constructor.
45       * @param ast ast node.
46       * @param lines2position list to map lines.
47       * @noinspection AssignmentToCollectionOrArrayFieldFromParameter
48       */
49      public CodeSelectorPresentation(DetailAST ast, ImmutableList<Integer> lines2position) {
50          node = ast;
51          this.lines2position = lines2position;
52      }
53  
54      /**
55       * Constructor.
56       * @param node DetailNode node.
57       * @param lines2position list to map lines.
58       * @noinspection AssignmentToCollectionOrArrayFieldFromParameter
59       */
60      public CodeSelectorPresentation(DetailNode node, ImmutableList<Integer> lines2position) {
61          this.node = node;
62          this.lines2position = lines2position;
63      }
64  
65      /**
66       * Returns selection start position.
67       * @return selection start position.
68       */
69      public int getSelectionStart() {
70          return selectionStart;
71      }
72  
73      /**
74       * Returns selection end position.
75       * @return selection end position.
76       */
77      public int getSelectionEnd() {
78          return selectionEnd;
79      }
80  
81      /**
82       * Find start and end selection positions from AST line and Column.
83       */
84      public void findSelectionPositions() {
85          if (node instanceof DetailAST) {
86              findSelectionPositions((DetailAST) node);
87          }
88          else {
89              findSelectionPositions((DetailNode) node);
90          }
91      }
92  
93      /**
94       * Find start and end selection positions from AST line and Column.
95       * @param ast DetailAST node for which selection finds
96       */
97      private void findSelectionPositions(DetailAST ast) {
98          selectionStart = lines2position.get(ast.getLineNo()) + ast.getColumnNo();
99  
100         if (ast.getChildCount() == 0
101                 && TokenUtils.getTokenName(ast.getType()).equals(ast.getText())) {
102             selectionEnd = selectionStart;
103         }
104         else {
105             selectionEnd = findLastPosition(ast);
106         }
107     }
108 
109     /**
110      * Find start and end selection positions from DetailNode line and Column.
111      * @param detailNode DetailNode node for which selection finds
112      */
113     private void findSelectionPositions(DetailNode detailNode) {
114         selectionStart = lines2position.get(detailNode.getLineNumber())
115                             + detailNode.getColumnNumber();
116 
117         selectionEnd = findLastPosition(detailNode);
118     }
119 
120     /**
121      * Finds the last position of node without children.
122      * @param astNode DetailAST node.
123      * @return Last position of node without children.
124      */
125     private int findLastPosition(final DetailAST astNode) {
126         final int lastPosition;
127         if (astNode.getChildCount() == 0) {
128             lastPosition = lines2position.get(astNode.getLineNo()) + astNode.getColumnNo()
129                     + astNode.getText().length();
130         }
131         else {
132             lastPosition = findLastPosition(astNode.getLastChild());
133         }
134         return lastPosition;
135     }
136 
137     /**
138      * Finds the last position of node without children.
139      * @param detailNode DetailNode node.
140      * @return Last position of node without children.
141      */
142     private int findLastPosition(final DetailNode detailNode) {
143         final int lastPosition;
144         if (detailNode.getChildren().length == 0) {
145             lastPosition = lines2position.get(detailNode.getLineNumber())
146                     + detailNode.getColumnNumber() + detailNode.getText().length();
147         }
148         else {
149             final DetailNode lastChild =
150                     detailNode.getChildren()[detailNode.getChildren().length - 1];
151             lastPosition = findLastPosition(lastChild);
152         }
153         return lastPosition;
154     }
155 }