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
49
50
51 public CodeSelectorPresentation(DetailAST ast, List<Integer> lines2position) {
52 node = ast;
53 this.lines2position = lines2position;
54 }
55
56
57
58
59
60
61
62
63
64
65 public CodeSelectorPresentation(DetailNode node, List<Integer> lines2position) {
66 this.node = node;
67 this.lines2position = lines2position;
68 }
69
70
71
72
73
74
75 public int getSelectionStart() {
76 return selectionStart;
77 }
78
79
80
81
82
83
84 public int getSelectionEnd() {
85 return selectionEnd;
86 }
87
88
89
90
91 public void findSelectionPositions() {
92 if (node instanceof DetailAST) {
93 findSelectionPositions((DetailAST) node);
94 }
95 else {
96 findSelectionPositions((DetailNode) node);
97 }
98 }
99
100
101
102
103
104
105 private void findSelectionPositions(DetailAST ast) {
106 selectionStart = lines2position.get(ast.getLineNo()) + ast.getColumnNo();
107
108 if (ast.hasChildren() || !TokenUtil.getTokenName(ast.getType()).equals(ast.getText())) {
109 selectionEnd = findLastPosition(ast);
110 }
111 else {
112 selectionEnd = selectionStart;
113 }
114 }
115
116
117
118
119
120
121 private void findSelectionPositions(DetailNode detailNode) {
122 selectionStart = lines2position.get(detailNode.getLineNumber())
123 + detailNode.getColumnNumber();
124
125 selectionEnd = findLastPosition(detailNode);
126 }
127
128
129
130
131
132
133
134 private int findLastPosition(final DetailAST astNode) {
135 final int lastPosition;
136 if (astNode.hasChildren()) {
137 lastPosition = findLastPosition(astNode.getLastChild());
138 }
139 else {
140 lastPosition = lines2position.get(astNode.getLineNo()) + astNode.getColumnNo()
141 + astNode.getText().length();
142 }
143 return lastPosition;
144 }
145
146
147
148
149
150
151
152 private int findLastPosition(final DetailNode detailNode) {
153 final int lastPosition;
154 if (detailNode.getChildren().length == 0) {
155 lastPosition = lines2position.get(detailNode.getLineNumber())
156 + detailNode.getColumnNumber() + detailNode.getText().length();
157 }
158 else {
159 final DetailNode lastChild =
160 detailNode.getChildren()[detailNode.getChildren().length - 1];
161 lastPosition = findLastPosition(lastChild);
162 }
163 return lastPosition;
164 }
165
166 }