|
13 | 13 | import java.util.function.Consumer; |
14 | 14 | import java.util.function.Predicate; |
15 | 15 | import java.util.logging.Logger; |
16 | | -import java.util.regex.Pattern; |
17 | 16 | import java.util.stream.Collectors; |
18 | 17 | import javax.lang.model.element.*; |
19 | 18 | import javax.lang.model.type.ArrayType; |
@@ -177,161 +176,10 @@ Optional<Element> element(TreePath tree) { |
177 | 176 | return Optional.ofNullable(el); |
178 | 177 | } |
179 | 178 |
|
180 | | - List<org.javacs.lsp.Diagnostic> reportErrors(Path file) { |
181 | | - var root = root(file); |
182 | | - var diags = new ArrayList<org.javacs.lsp.Diagnostic>(); |
183 | | - // Convert diags |
184 | | - for (var d : parent.diags) { |
185 | | - var source = d.getSource(); |
186 | | - if (source == null) continue; |
187 | | - var path = Paths.get(source.toUri()); |
188 | | - if (!file.equals(path)) continue; |
189 | | - if (!isValidFileRange(d)) continue; |
190 | | - diags.add(lspDiagnostic(d, root.getLineMap())); |
191 | | - } |
192 | | - // Check for unused privates |
193 | | - var warnUnused = new WarnUnused(borrow.task); |
194 | | - warnUnused.scan(root, null); |
195 | | - for (var unusedEl : warnUnused.notUsed()) { |
196 | | - diags.add(warnUnused(unusedEl)); |
197 | | - } |
198 | | - // Check for unused exceptions |
199 | | - var notThrown = new HashMap<String, TreePath>(); |
200 | | - new WarnNotThrown(borrow.task).scan(root, notThrown); |
201 | | - for (var name : notThrown.keySet()) { |
202 | | - diags.add(warnNotThrown(name, notThrown.get(name))); |
203 | | - } |
204 | | - // TODO hint fields that could be final |
205 | | - |
206 | | - return diags; |
207 | | - } |
208 | | - |
209 | 179 | private boolean isValidFileRange(javax.tools.Diagnostic<? extends JavaFileObject> d) { |
210 | 180 | return d.getSource().toUri().getScheme().equals("file") && d.getStartPosition() >= 0 && d.getEndPosition() >= 0; |
211 | 181 | } |
212 | 182 |
|
213 | | - /** |
214 | | - * lspDiagnostic(d, lines) converts d to LSP format, with its position shifted appropriately for the latest version |
215 | | - * of the file. |
216 | | - */ |
217 | | - private org.javacs.lsp.Diagnostic lspDiagnostic(javax.tools.Diagnostic<? extends JavaFileObject> d, LineMap lines) { |
218 | | - var start = (int) d.getStartPosition(); |
219 | | - var end = (int) d.getEndPosition(); |
220 | | - var severity = severity(d.getKind()); |
221 | | - var code = d.getCode(); |
222 | | - var message = d.getMessage(null); |
223 | | - var result = new org.javacs.lsp.Diagnostic(); |
224 | | - result.severity = severity; |
225 | | - result.code = code; |
226 | | - result.message = message; |
227 | | - result.range = new Span(start, end).asRange(lines); |
228 | | - return result; |
229 | | - } |
230 | | - |
231 | | - private int severity(javax.tools.Diagnostic.Kind kind) { |
232 | | - switch (kind) { |
233 | | - case ERROR: |
234 | | - return DiagnosticSeverity.Error; |
235 | | - case WARNING: |
236 | | - case MANDATORY_WARNING: |
237 | | - return DiagnosticSeverity.Warning; |
238 | | - case NOTE: |
239 | | - return DiagnosticSeverity.Information; |
240 | | - case OTHER: |
241 | | - default: |
242 | | - return DiagnosticSeverity.Hint; |
243 | | - } |
244 | | - } |
245 | | - |
246 | | - private org.javacs.lsp.Diagnostic warnNotThrown(String name, TreePath path) { |
247 | | - var pos = trees.getSourcePositions(); |
248 | | - var root = path.getCompilationUnit(); |
249 | | - var lines = root.getLineMap(); |
250 | | - var start = pos.getStartPosition(root, path.getLeaf()); |
251 | | - var startLine = (int) lines.getLineNumber(start); |
252 | | - var startColumn = (int) lines.getColumnNumber(start); |
253 | | - var startPos = new Position(startLine - 1, startColumn - 1); |
254 | | - var end = pos.getEndPosition(root, path.getLeaf()); |
255 | | - var endLine = (int) lines.getLineNumber(end); |
256 | | - var endColumn = (int) lines.getColumnNumber(end); |
257 | | - var endPos = new Position(endLine - 1, endColumn - 1); |
258 | | - var d = new org.javacs.lsp.Diagnostic(); |
259 | | - d.message = String.format("'%s' is not thrown in the body of the method", name); |
260 | | - d.range = new Range(startPos, endPos); |
261 | | - d.code = "unused_throws"; |
262 | | - d.severity = DiagnosticSeverity.Information; |
263 | | - d.tags = List.of(DiagnosticTag.Unnecessary); |
264 | | - return d; |
265 | | - } |
266 | | - |
267 | | - private org.javacs.lsp.Diagnostic warnUnused(Element unusedEl) { |
268 | | - var path = trees.getPath(unusedEl); |
269 | | - var root = path.getCompilationUnit(); |
270 | | - var leaf = path.getLeaf(); |
271 | | - var pos = trees.getSourcePositions(); |
272 | | - var start = (int) pos.getStartPosition(root, leaf); |
273 | | - var end = (int) pos.getEndPosition(root, leaf); |
274 | | - if (leaf instanceof VariableTree) { |
275 | | - var v = (VariableTree) leaf; |
276 | | - var offset = (int) pos.getEndPosition(root, v.getType()); |
277 | | - if (offset != -1) { |
278 | | - start = offset; |
279 | | - } |
280 | | - } |
281 | | - var file = Paths.get(root.getSourceFile().toUri()); |
282 | | - var contents = FileStore.contents(file); |
283 | | - var name = unusedEl.getSimpleName(); |
284 | | - if (name.contentEquals("<init>")) { |
285 | | - name = unusedEl.getEnclosingElement().getSimpleName(); |
286 | | - } |
287 | | - var region = contents.subSequence(start, end); |
288 | | - var matcher = Pattern.compile("\\b" + name + "\\b").matcher(region); |
289 | | - if (matcher.find()) { |
290 | | - start += matcher.start(); |
291 | | - end = start + name.length(); |
292 | | - } |
293 | | - var message = String.format("'%s' is not used", name); |
294 | | - String code; |
295 | | - int severity; |
296 | | - if (leaf instanceof VariableTree) { |
297 | | - var parent = path.getParentPath().getLeaf(); |
298 | | - if (parent instanceof MethodTree) { |
299 | | - code = "unused_param"; |
300 | | - severity = DiagnosticSeverity.Hint; |
301 | | - } else if (parent instanceof BlockTree) { |
302 | | - code = "unused_local"; |
303 | | - severity = DiagnosticSeverity.Information; |
304 | | - } else if (parent instanceof ClassTree) { |
305 | | - code = "unused_field"; |
306 | | - severity = DiagnosticSeverity.Information; |
307 | | - } else { |
308 | | - code = "unused_other"; |
309 | | - severity = DiagnosticSeverity.Hint; |
310 | | - } |
311 | | - } else if (leaf instanceof MethodTree) { |
312 | | - code = "unused_method"; |
313 | | - severity = DiagnosticSeverity.Information; |
314 | | - } else if (leaf instanceof ClassTree) { |
315 | | - code = "unused_class"; |
316 | | - severity = DiagnosticSeverity.Information; |
317 | | - } else { |
318 | | - code = "unused_other"; |
319 | | - severity = DiagnosticSeverity.Information; |
320 | | - } |
321 | | - return lspWarnUnused(severity, code, message, start, end, root.getLineMap()); |
322 | | - } |
323 | | - |
324 | | - static org.javacs.lsp.Diagnostic lspWarnUnused( |
325 | | - int severity, String code, String message, int start, int end, LineMap lines) { |
326 | | - var result = new org.javacs.lsp.Diagnostic(); |
327 | | - result.severity = severity; |
328 | | - result.code = code; |
329 | | - result.message = message; |
330 | | - result.tags = List.of(DiagnosticTag.Unnecessary); |
331 | | - result.range = new Span(start, end).asRange(lines); |
332 | | - return result; |
333 | | - } |
334 | | - |
335 | 183 | public static final List<TreePath> CODE_NOT_FOUND = List.of(); |
336 | 184 |
|
337 | 185 | List<TreePath> references(Path toFile, int toLine, int toColumn) { |
@@ -1458,12 +1306,5 @@ private JsonElement data(Ptr ptr, int plusOverloads) { |
1458 | 1306 | return JavaLanguageServer.GSON.toJsonTree(data); |
1459 | 1307 | } |
1460 | 1308 |
|
1461 | | - SemanticColors colors(Path file) { |
1462 | | - var root = root(file); |
1463 | | - var colorizer = new Colorizer(borrow.task); |
1464 | | - colorizer.scan(root, null); |
1465 | | - return colorizer.colors.lspSemanticColors(file, root.getLineMap()); |
1466 | | - } |
1467 | | - |
1468 | 1309 | private static final Logger LOG = Logger.getLogger("main"); |
1469 | 1310 | } |
0 commit comments