Search in sources :

Example 1 with AccessSymbol

use of meghanada.analyze.AccessSymbol in project meghanada-server by mopemope.

the class JavaCompletion method completionMethods.

private Collection<? extends CandidateUnit> completionMethods(Source source, int line, int column, String searchWord) {
    final int prefixIdx = searchWord.lastIndexOf('#');
    final int classIdx = searchWord.lastIndexOf(':');
    final String pkg = source.getPackageName();
    if (classIdx > 0 && prefixIdx > 0) {
        final String prefix = searchWord.substring(prefixIdx + 1);
        // return methods of prefix class
        String fqcn = searchWord.substring(classIdx + 1, prefixIdx);
        fqcn = ClassNameUtils.replace(fqcn, ClassNameUtils.CAPTURE_OF, "");
        return reflectWithFQCN(fqcn, prefix).stream().sorted(methodComparing(prefix)).collect(Collectors.toList());
    }
    // chained method completion
    if (classIdx > 0) {
        // return methods of prefix class
        String fqcn = searchWord.substring(classIdx + 1, searchWord.length());
        fqcn = ClassNameUtils.replace(fqcn, ClassNameUtils.CAPTURE_OF, "");
        return reflect(pkg, fqcn, "").stream().sorted(defaultComparing()).collect(Collectors.toList());
    } else {
        String prefix = "";
        if (prefixIdx > 0) {
            prefix = searchWord.substring(prefixIdx + 1);
        }
        // search near method call and return methods of prefix class
        final List<AccessSymbol> targets = new ArrayList<>(8);
        targets.addAll(source.getMethodCall(line));
        targets.addAll(source.getFieldAccess(line));
        log.debug("targets:{}", targets);
        int size = targets.size();
        int startColumn = column;
        while (size > 0 && startColumn-- > 0) {
            for (AccessSymbol as : targets) {
                if (as.match(line, startColumn) && nonNull(as.returnType)) {
                    final String fqcn = ClassNameUtils.replace(as.returnType, ClassNameUtils.CAPTURE_OF, "");
                    return reflect(pkg, fqcn, prefix).stream().sorted(methodComparing(prefix)).collect(Collectors.toList());
                }
            }
        }
        return Collections.emptyList();
    }
}
Also used : AccessSymbol(meghanada.analyze.AccessSymbol) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)1 AccessSymbol (meghanada.analyze.AccessSymbol)1