1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.gui;
21
22 import java.util.List;
23
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.DetailNode;
26 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
27
28
29
30
31 public class CodeSelectorPresentation {
32
33
34 private final Object node;
35
36 private final List<Integer> lines2position;
37
38 private int selectionStart;
39
40 private int selectionEnd;
41
42
43
44
45
46
47
48 public CodeSelectorPresentation(DetailAST ast, List<Integer> lines2position) {
49 node = ast;
50 this.lines2position = lines2position;
51 }
52
53
54
55
56
57
58
59 public CodeSelectorPresentation(DetailNode node, List<Integer> lines2position) {
60 this.node = node;
61 this.lines2position = lines2position;
62 }
63
64
65
66
67
68 public int getSelectionStart() {
69 return selectionStart;
70 }
71
72
73
74
75
76 public int getSelectionEnd() {
77 return selectionEnd;
78 }
79
80
81
82
83 public void findSelectionPositions() {
84 if (node instanceof DetailAST) {
85 findSelectionPositions((DetailAST) node);
86 }
87 else {
88 findSelectionPositions((DetailNode) node);
89 }
90 }
91
92
93
94
95
96 private void findSelectionPositions(DetailAST ast) {
97 selectionStart = lines2position.get(ast.getLineNo()) + ast.getColumnNo();
98
99 if (ast.getChildCount() == 0
100 && TokenUtil.getTokenName(ast.getType()).equals(ast.getText())) {
101 selectionEnd = selectionStart;
102 }
103 else {
104 selectionEnd = findLastPosition(ast);
105 }
106 }
107
108
109
110
111
112 private void findSelectionPositions(DetailNode detailNode) {
113 selectionStart = lines2position.get(detailNode.getLineNumber())
114 + detailNode.getColumnNumber();
115
116 selectionEnd = findLastPosition(detailNode);
117 }
118
119
120
121
122
123
124 private int findLastPosition(final DetailAST astNode) {
125 final int lastPosition;
126 if (astNode.getChildCount() == 0) {
127 lastPosition = lines2position.get(astNode.getLineNo()) + astNode.getColumnNo()
128 + astNode.getText().length();
129 }
130 else {
131 lastPosition = findLastPosition(astNode.getLastChild());
132 }
133 return lastPosition;
134 }
135
136
137
138
139
140
141 private int findLastPosition(final DetailNode detailNode) {
142 final int lastPosition;
143 if (detailNode.getChildren().length == 0) {
144 lastPosition = lines2position.get(detailNode.getLineNumber())
145 + detailNode.getColumnNumber() + detailNode.getText().length();
146 }
147 else {
148 final DetailNode lastChild =
149 detailNode.getChildren()[detailNode.getChildren().length - 1];
150 lastPosition = findLastPosition(lastChild);
151 }
152 return lastPosition;
153 }
154
155 }