|
19 | 19 |
|
20 | 20 | import java.nio.file.Path; |
21 | 21 | import java.util.List; |
22 | | -import java.util.concurrent.CancellationException; |
23 | | -import java.util.function.Function; |
24 | 22 |
|
| 23 | +import org.checkstyle.autofix.PositionHelper; |
25 | 24 | import org.checkstyle.autofix.parser.CheckstyleViolation; |
26 | | -import org.openrewrite.Cursor; |
27 | 25 | import org.openrewrite.ExecutionContext; |
28 | | -import org.openrewrite.PrintOutputCapture; |
29 | 26 | import org.openrewrite.Recipe; |
30 | 27 | import org.openrewrite.TreeVisitor; |
31 | | -import org.openrewrite.internal.RecipeRunException; |
32 | 28 | import org.openrewrite.java.JavaIsoVisitor; |
33 | 29 | import org.openrewrite.java.tree.J; |
34 | 30 | import org.openrewrite.java.tree.JavaType; |
@@ -93,92 +89,14 @@ && isAtViolationLocation(result)) { |
93 | 89 | private boolean isAtViolationLocation(J.Literal literal) { |
94 | 90 | final J.CompilationUnit cursor = getCursor().firstEnclosing(J.CompilationUnit.class); |
95 | 91 |
|
96 | | - final int line = computeLinePosition(cursor, literal, getCursor()); |
97 | | - final int column = computeColumnPosition(cursor, literal, getCursor()); |
| 92 | + final int line = PositionHelper.computeLinePosition(cursor, literal, getCursor()); |
| 93 | + final int column = PositionHelper.computeColumnPosition(cursor, literal, getCursor()); |
98 | 94 |
|
99 | 95 | return violations.stream().anyMatch(violation -> { |
100 | 96 | return violation.getLine() == line |
101 | 97 | && violation.getColumn() == column |
102 | 98 | && Path.of(violation.getFileName()).equals(sourcePath); |
103 | 99 | }); |
104 | 100 | } |
105 | | - |
106 | | - /** |
107 | | - * Computes the position of a target element within a syntax tree using position calculator. |
108 | | - * This method traverses the given syntax tree and captures the printed output until the |
109 | | - * target element is encountered. When the target is found, a CancellationException |
110 | | - * is thrown to interrupt traversal, and the captured output is passed to the provided |
111 | | - * positionCalculator to compute the position. |
112 | | - * |
113 | | - * @param tree the root of the syntax tree to traverse |
114 | | - * @param targetElement the element whose position is to be computed |
115 | | - * @param cursor the current cursor in the tree traversal |
116 | | - * @param positionCalculator a function to compute the position from the printed output |
117 | | - * @return the computed position of the target element |
118 | | - * @throws IllegalStateException if the target element is not found in the tree |
119 | | - * @throws RecipeRunException if an error occurs during traversal |
120 | | - */ |
121 | | - private int computePosition( |
122 | | - J tree, |
123 | | - J targetElement, |
124 | | - Cursor cursor, |
125 | | - Function<String, Integer> positionCalculator |
126 | | - ) { |
127 | | - final TreeVisitor<?, PrintOutputCapture<TreeVisitor<?, ?>>> printer = |
128 | | - tree.printer(cursor); |
129 | | - |
130 | | - final PrintOutputCapture<TreeVisitor<?, ?>> capture = |
131 | | - new PrintOutputCapture<>(printer) { |
132 | | - @Override |
133 | | - public PrintOutputCapture<TreeVisitor<?, ?>> append(String text) { |
134 | | - if (targetElement.isScope(getContext().getCursor().getValue())) { |
135 | | - super.append(targetElement.getPrefix().getWhitespace()); |
136 | | - throw new CancellationException(); |
137 | | - } |
138 | | - return super.append(text); |
139 | | - } |
140 | | - }; |
141 | | - |
142 | | - final int result; |
143 | | - try { |
144 | | - printer.visit(tree, capture, cursor.getParentOrThrow()); |
145 | | - throw new IllegalStateException("Target element: " + targetElement |
146 | | - + ", not found in the syntax tree."); |
147 | | - } |
148 | | - catch (CancellationException exception) { |
149 | | - result = positionCalculator.apply(capture.getOut()); |
150 | | - } |
151 | | - catch (RecipeRunException exception) { |
152 | | - if (exception.getCause() instanceof CancellationException) { |
153 | | - result = positionCalculator.apply(capture.getOut()); |
154 | | - } |
155 | | - else { |
156 | | - throw exception; |
157 | | - } |
158 | | - } |
159 | | - return result; |
160 | | - } |
161 | | - |
162 | | - private int computeLinePosition(J tree, J targetElement, Cursor cursor) { |
163 | | - return computePosition(tree, targetElement, cursor, |
164 | | - out -> 1 + Math.toIntExact(out.chars().filter(chr -> chr == '\n').count())); |
165 | | - } |
166 | | - |
167 | | - private int computeColumnPosition(J tree, J targetElement, Cursor cursor) { |
168 | | - return computePosition(tree, targetElement, cursor, this::calculateColumnOffset); |
169 | | - } |
170 | | - |
171 | | - private int calculateColumnOffset(String out) { |
172 | | - final int lineBreakIndex = out.lastIndexOf('\n'); |
173 | | - final int result; |
174 | | - if (lineBreakIndex == -1) { |
175 | | - result = out.length(); |
176 | | - } |
177 | | - else { |
178 | | - result = out.length() - lineBreakIndex - 1; |
179 | | - } |
180 | | - return result; |
181 | | - } |
182 | | - |
183 | 101 | } |
184 | 102 | } |
0 commit comments