Coverage Report - com.puppycrawl.tools.checkstyle.gui.CodeSelectorPresentation
 
Classes in this File Line Coverage Branch Coverage Complexity
CodeSelectorPresentation
100%
36/36
100%
10/10
1.556
 
 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  3
     public CodeSelectorPresentation(DetailAST ast, ImmutableList<Integer> lines2position) {
 50  3
         node = ast;
 51  3
         this.lines2position = lines2position;
 52  3
     }
 53  
 
 54  
     /**
 55  
      * Constructor.
 56  
      * @param node DetailNode node.
 57  
      * @param lines2position list to map lines.
 58  
      * @noinspection AssignmentToCollectionOrArrayFieldFromParameter
 59  
      */
 60  2
     public CodeSelectorPresentation(DetailNode node, ImmutableList<Integer> lines2position) {
 61  2
         this.node = node;
 62  2
         this.lines2position = lines2position;
 63  2
     }
 64  
 
 65  
     /**
 66  
      * Returns selection start position.
 67  
      * @return selection start position.
 68  
      */
 69  
     public int getSelectionStart() {
 70  5
         return selectionStart;
 71  
     }
 72  
 
 73  
     /**
 74  
      * Returns selection end position.
 75  
      * @return selection end position.
 76  
      */
 77  
     public int getSelectionEnd() {
 78  5
         return selectionEnd;
 79  
     }
 80  
 
 81  
     /**
 82  
      * Find start and end selection positions from AST line and Column.
 83  
      */
 84  
     public void findSelectionPositions() {
 85  5
         if (node instanceof DetailAST) {
 86  3
             findSelectionPositions((DetailAST) node);
 87  
         }
 88  
         else {
 89  2
             findSelectionPositions((DetailNode) node);
 90  
         }
 91  5
     }
 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  3
         selectionStart = lines2position.get(ast.getLineNo()) + ast.getColumnNo();
 99  
 
 100  3
         if (ast.getChildCount() == 0
 101  2
                 && TokenUtils.getTokenName(ast.getType()).equals(ast.getText())) {
 102  1
             selectionEnd = selectionStart;
 103  
         }
 104  
         else {
 105  2
             selectionEnd = findLastPosition(ast);
 106  
         }
 107  3
     }
 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  2
         selectionStart = lines2position.get(detailNode.getLineNumber())
 115  2
                             + detailNode.getColumnNumber();
 116  
 
 117  2
         selectionEnd = findLastPosition(detailNode);
 118  2
     }
 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  4
         if (astNode.getChildCount() == 0) {
 128  2
             lastPosition = lines2position.get(astNode.getLineNo()) + astNode.getColumnNo()
 129  2
                     + astNode.getText().length();
 130  
         }
 131  
         else {
 132  2
             lastPosition = findLastPosition(astNode.getLastChild());
 133  
         }
 134  4
         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  3
         if (detailNode.getChildren().length == 0) {
 145  2
             lastPosition = lines2position.get(detailNode.getLineNumber())
 146  2
                     + detailNode.getColumnNumber() + detailNode.getText().length();
 147  
         }
 148  
         else {
 149  1
             final DetailNode lastChild =
 150  1
                     detailNode.getChildren()[detailNode.getChildren().length - 1];
 151  1
             lastPosition = findLastPosition(lastChild);
 152  
         }
 153  3
         return lastPosition;
 154  
     }
 155  
 }