mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
refactor(lsp/java): move qualified name finding logic to JavaCompilerService
This commit is contained in:
+3
-16
@@ -81,22 +81,9 @@ class AddImportAction : BaseJavaCodeAction() {
|
||||
return
|
||||
}
|
||||
|
||||
var found = false
|
||||
val simpleName = jcDiagnostic.args[1]
|
||||
for (name in compiler.publicTopLevelTypes()) {
|
||||
var klass = name
|
||||
|
||||
// This will be true in a test environment
|
||||
if (klass.contains("/")) {
|
||||
klass = name.replace('/', '.')
|
||||
}
|
||||
|
||||
if (klass.endsWith(".$simpleName")) {
|
||||
found = true
|
||||
// There is at least one class to import
|
||||
break
|
||||
}
|
||||
}
|
||||
val found =
|
||||
jcDiagnostic.args[1]?.toString()?.let { compiler.findQualifiedNames(it, true).isNotEmpty() }
|
||||
?: false
|
||||
|
||||
visible = found
|
||||
enabled = found
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.TreeSet;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -45,6 +46,12 @@ public interface CompilerProvider {
|
||||
|
||||
Path[] findMemberReferences(String className, String memberName);
|
||||
|
||||
default List<String> findQualifiedNames(String simpleName) {
|
||||
return findQualifiedNames(simpleName, false);
|
||||
}
|
||||
|
||||
List<String> findQualifiedNames(String simpleName, boolean onlyOne);
|
||||
|
||||
ParseTask parse(Path file);
|
||||
|
||||
ParseTask parse(JavaFileObject file);
|
||||
|
||||
+19
@@ -220,6 +220,25 @@ public class JavaCompilerService implements CompilerProvider {
|
||||
return candidates.toArray(new Path[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> findQualifiedNames(String simpleName, boolean onlyOne) {
|
||||
final var names = new ArrayList<String>();
|
||||
for (var name : publicTopLevelTypes()) {
|
||||
// This will be true in a test environment
|
||||
if (name.contains("/")) {
|
||||
name = name.replace('/', '.');
|
||||
}
|
||||
|
||||
if (name.endsWith(".$simpleName")) {
|
||||
names.add(name);
|
||||
if (onlyOne) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParseTask parse(Path file) {
|
||||
Parser parser = Parser.parseFile(file);
|
||||
|
||||
Reference in New Issue
Block a user