Repository navigation
Expand file tree
/
Copy pathCodeActions.java
More file actions
94 lines (80 loc) · 3.32 KB
/
Copy pathCodeActions.java
File metadata and controls
94 lines (80 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package org.javacs;
import com.google.common.collect.ImmutableList;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.util.JavacTask;
import java.net.URI;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.eclipse.lsp4j.CodeActionParams;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Diagnostic;
class CodeActions {
private final JavacHolder compiler;
private final URI file;
private final Optional<String> textContent;
private final JavacTask task;
private final CompilationUnitTree source;
private final SymbolIndex index;
CodeActions(
JavacHolder compiler,
URI file,
Optional<String> textContent,
int line,
int character,
SymbolIndex index) {
this.compiler = compiler;
this.file = file;
this.textContent = textContent;
FocusedResult compile = compiler.compileFocused(file, textContent, line, character, false);
this.task = compile.task;
this.source = compile.compilationUnit;
this.index = index;
}
public List<Command> find(CodeActionParams params) {
return params.getContext()
.getDiagnostics()
.stream()
.flatMap(diagnostic -> findCodeActionsForDiagnostic(diagnostic))
.collect(Collectors.toList());
}
private Stream<Command> findCodeActionsForDiagnostic(Diagnostic diagnostic) {
return codeActionsFor(diagnostic);
}
private Stream<Command> codeActionsFor(Diagnostic diagnostic) {
if (diagnostic.getCode().equals("compiler.err.cant.resolve.location")) {
return cannotFindSymbolClassName(diagnostic.getMessage())
.map(Stream::of)
.orElseGet(Stream::empty)
.flatMap(this::addImportActions);
} else return Stream.empty();
}
/** Search for symbols on the classpath and sourcepath that match name */
private Stream<Command> addImportActions(String name) {
Stream<Command> sourcePath =
index.allTopLevelClasses()
.filter(c -> c.className.equals(name))
.map(c -> importClassCommand(c.packageName, c.className));
Stream<Command> classPath =
compiler.classPathIndex
.topLevelClasses()
.filter(c -> c.getSimpleName().equals(name))
.map(c -> importClassCommand(c.getPackageName(), c.getSimpleName()));
return Stream.concat(sourcePath, classPath);
}
private Command importClassCommand(String packageName, String className) {
return new Command(
"Import " + packageName + "." + className,
"Java.importClass",
ImmutableList.of(file.toString(), packageName, className));
}
private static final Pattern CANNOT_FIND_SYMBOL = Pattern.compile("class (\\w+)");
public static Optional<String> cannotFindSymbolClassName(String message) {
Matcher matcher = CANNOT_FIND_SYMBOL.matcher(message);
if (matcher.find()) return Optional.of(matcher.group(1));
else return Optional.empty();
}
}