use of meghanada.analyze.TypeScope in project meghanada-server by mopemope.
the class LocationSearcher method searchLocalVariable.
private Optional<Location> searchLocalVariable(final Source source, final int line, final int col, final String symbol) {
final EntryMessage entryMessage = log.traceEntry("line={} col={} symbol={}", line, col, symbol);
final Map<String, Variable> variableMap = source.getVariableMap(line);
log.trace("variables={}", variableMap);
final Optional<Variable> variable = Optional.ofNullable(variableMap.get(symbol));
final Optional<Location> location = variable.map(var -> {
if (var.isDecl()) {
final Location loc = new Location(source.getFile().getPath(), var.range.begin.line, var.range.begin.column);
return Optional.of(loc);
} else {
final String fqcn = var.fqcn;
final Location loc = getFQCNLocation(fqcn);
return Optional.ofNullable(loc);
}
}).orElseGet(() -> {
// isField
final Optional<TypeScope> ts = source.getTypeScope(line);
if (!ts.isPresent()) {
return Optional.empty();
}
return ts.get().getField(symbol).map(fieldSymbol -> new Location(source.getFile().getPath(), fieldSymbol.range.begin.line, fieldSymbol.range.begin.column));
});
log.traceExit(entryMessage);
return location;
}
use of meghanada.analyze.TypeScope in project meghanada-server by mopemope.
the class JavaCompletion method completionSymbols.
private static Collection<? extends CandidateUnit> completionSymbols(Source source, int line, String prefix) {
Set<CandidateUnit> result = new HashSet<>(32);
// prefix search
log.debug("Search variables prefix:{} line:{}", prefix, line);
Optional<TypeScope> typeScope = source.getTypeScope(line);
if (!typeScope.isPresent()) {
return result;
}
String fqcn = typeScope.get().getFQCN();
// add this member
for (MemberDescriptor c : JavaCompletion.reflectSelf(fqcn, true, prefix)) {
if (c.getName().startsWith(prefix)) {
result.add(c);
}
}
if (fqcn.contains(ClassNameUtils.INNER_MARK)) {
// add parent
String parentClass = fqcn;
while (true) {
int i = parentClass.lastIndexOf('$');
if (i < 0) {
break;
}
parentClass = parentClass.substring(0, i);
for (MemberDescriptor c : JavaCompletion.reflectSelf(parentClass, true, prefix)) {
if (c.getName().startsWith(prefix)) {
result.add(c);
}
}
}
}
log.debug("self fqcn:{}", fqcn);
Map<String, Variable> symbols = source.getDeclaratorMap(line);
log.debug("search variables size:{} result:{}", symbols.size(), symbols);
for (Map.Entry<String, Variable> e : symbols.entrySet()) {
String k = e.getKey();
Variable v = e.getValue();
log.debug("check variable name:{}", k);
if (k.startsWith(prefix)) {
log.debug("match variable name:{}", k);
if (!v.isField) {
result.add(v.toCandidateUnit());
}
}
}
// import
for (Map.Entry<String, String> e : source.getImportedClassMap().entrySet()) {
String k = e.getKey();
String v = e.getValue();
if (k.startsWith(prefix)) {
result.add(ClassIndex.createClass(v));
}
}
// static import
for (Map.Entry<String, String> e : source.staticImportClass.entrySet()) {
String methodName = e.getKey();
String clazz = e.getValue();
for (MemberDescriptor md : JavaCompletion.reflectWithFQCN(clazz, methodName)) {
if (md.getName().equals(methodName)) {
result.add(md);
}
}
}
// Add class
if (Character.isUpperCase(prefix.charAt(0))) {
// completion
CachedASMReflector reflector = CachedASMReflector.getInstance();
boolean fuzzySearch = Config.load().useClassFuzzySearch();
if (fuzzySearch) {
result.addAll(reflector.fuzzySearchClasses(prefix.toLowerCase()));
} else {
result.addAll(reflector.searchClasses(prefix.toLowerCase()));
}
}
List<CandidateUnit> list = new ArrayList<>(result);
list.sort(comparing(source, prefix));
return list;
}
Aggregations