Search in sources :

Example 16 with AutocompleteCandidate

use of com.twosigma.beakerx.autocomplete.AutocompleteCandidate in project beakerx by twosigma.

the class JavaNodeCompletion method visitErrorNode.

@Override
public void visitErrorNode(ErrorNode arg0) {
    if (arg0.getText().equals("new")) {
        CompilationUnitContext cuc = (CompilationUnitContext) arg0.getParent();
        List<ParseTree> children = cuc.children;
        int tokenIndex = arg0.getSymbol().getTokenIndex();
        if (tokenIndex - 2 >= 0 && tokenIndex + 1 <= children.size()) {
            ParseTree variablePT = children.get(tokenIndex - 2);
            ParseTree typePT = children.get(tokenIndex + 1);
            String type = typePT.getText();
            String variable = variablePT.getText();
            AutocompleteCandidate c1 = new AutocompleteCandidate(JavaCompletionTypes.NAME, variable);
            registry.addCandidate(c1);
            if (type != null)
                classUtils.defineVariable(variable, type);
            return;
        }
    }
    if (arg0.getSymbol().getStartIndex() < cursor && arg0.getSymbol().getStopIndex() + 1 >= cursor) {
        // System.out.println("ERR: "+arg0.getSymbol().getStartIndex()+" "+arg0.getSymbol().getStopIndex()+" "+arg0.getSymbol().getText());
        if (arg0.getParent() instanceof CompilationUnitContext) {
            CompilationUnitContext cuc = (CompilationUnitContext) arg0.getParent();
            AutocompleteCandidate c = new AutocompleteCandidate(JavaCompletionTypes.INITIAL, arg0.getText());
            addQuery(c, cursor);
            AutocompleteCandidate c2 = new AutocompleteCandidate(JavaCompletionTypes.TOPLEVEL, arg0.getText());
            addQuery(c2, cursor);
            completeClassFromPath(cuc, arg0.getText());
            return;
        } else if (arg0.getParent() instanceof BlockStatementContext) {
            if (!arg0.getSymbol().getText().equals(".")) {
                AutocompleteCandidate c = new AutocompleteCandidate(JavaCompletionTypes.BLOCKLEVEL, arg0.getText());
                addQuery(c, cursor);
                c = new AutocompleteCandidate(JavaCompletionTypes.TYPE, arg0.getText());
                addQuery(c, cursor);
                c = new AutocompleteCandidate(JavaCompletionTypes.CUSTOM_TYPE, arg0.getText());
                addQuery(c, cursor);
                c = new AutocompleteCandidate(JavaCompletionTypes.NAME, arg0.getText());
                addQuery(c, cursor);
            } else {
                BlockStatementContext bs = (BlockStatementContext) arg0.getParent();
                if (bs.getChildCount() > 1) {
                    addQuery(classUtils.expandExpression(bs.getText(), registry, classUtils.DO_ALL), cursor);
                }
            }
        } else if (arg0.getParent() instanceof ExpressionContext) {
            // we are the rightmost child of the expression
            ParseTree chld = arg0.getParent().getChild(arg0.getParent().getChildCount() - 1);
            if (!chld.equals(arg0))
                return;
            addQuery(classUtils.expandExpression(arg0.getParent().getText(), registry, classUtils.DO_NON_STATIC), cursor);
        } else if (arg0.getParent() instanceof TypeDeclarationContext && arg0.getParent().getParent() != null && arg0.getParent().getParent() instanceof CompilationUnitContext) {
            AutocompleteCandidate c = new AutocompleteCandidate(JavaCompletionTypes.TOPLEVEL, arg0.getText());
            addQuery(c, cursor);
        } else if (arg0.getParent() instanceof MemberDeclarationContext && arg0.getParent().getParent() != null && arg0.getParent().getParent() instanceof ClassBodyDeclarationContext && arg0.getParent().getParent().getParent() != null && arg0.getParent().getParent().getParent() instanceof ClassBodyContext && arg0.getParent().getParent().getParent().getText().trim().startsWith("<missing '{'>")) {
            AutocompleteCandidate c = new AutocompleteCandidate(JavaCompletionTypes.CLASSLEVEL, arg0.getText());
            addQuery(c, cursor);
        } else if (arg0.getParent() instanceof MemberDeclarationContext && arg0.getParent().getParent() != null && arg0.getParent().getParent() instanceof ClassBodyDeclarationContext && arg0.getParent().getParent().getParent() != null && arg0.getParent().getParent().getParent() instanceof ClassBodyContext) {
            AutocompleteCandidate c = new AutocompleteCandidate(JavaCompletionTypes.TYPE, arg0.getText());
            addQuery(c, cursor);
            c = new AutocompleteCandidate(JavaCompletionTypes.CUSTOM_TYPE, arg0.getText());
            addQuery(c, cursor);
        }
    }
}
Also used : MemberDeclarationContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.MemberDeclarationContext) CompilationUnitContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.CompilationUnitContext) BlockStatementContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.BlockStatementContext) ExpressionContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.ExpressionContext) ClassBodyDeclarationContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.ClassBodyDeclarationContext) AutocompleteCandidate(com.twosigma.beakerx.autocomplete.AutocompleteCandidate) ClassBodyContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.ClassBodyContext) ParseTree(org.antlr.v4.runtime.tree.ParseTree) TypeDeclarationContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.TypeDeclarationContext)

Example 17 with AutocompleteCandidate

use of com.twosigma.beakerx.autocomplete.AutocompleteCandidate in project beakerx by twosigma.

the class JavaAutocomplete method findAutocompleteResult.

private AutocompleteResult findAutocompleteResult(String txt, int cur, ClassUtils cu) {
    List<AutocompleteCandidate> q = new ArrayList<>();
    List<String> ret = new ArrayList<>();
    int startIndex = 0;
    for (int i = cur - 1; i >= 0; i--) {
        if (i < txt.length() && Character.isWhitespace(txt.charAt(i))) {
            String tx = txt.substring(i + 1, cur).trim();
            if (!txt.isEmpty()) {
                if (tx.contains(".")) {
                    q.add(cu.expandExpression(tx, registry, cu.DO_ALL));
                } else {
                    q.add(new AutocompleteCandidate(JavaCompletionTypes.NAME, tx));
                }
                ret = registry.searchCandidates(q);
                startIndex = txt.indexOf(tx) + tx.length();
            }
            break;
        }
    }
    return new AutocompleteResult(ret, startIndex);
}
Also used : ArrayList(java.util.ArrayList) AutocompleteCandidate(com.twosigma.beakerx.autocomplete.AutocompleteCandidate) AutocompleteResult(com.twosigma.beakerx.autocomplete.AutocompleteResult)

Example 18 with AutocompleteCandidate

use of com.twosigma.beakerx.autocomplete.AutocompleteCandidate in project beakerx by twosigma.

the class JavaAutocomplete method find.

private AutocompleteResult find(String txt, int cur, ClassLoader l, Imports imports) {
    registry = AutocompleteRegistryFactory.createRegistry(cps);
    ClassUtils cu = createClassUtils(l);
    setup(cu, registry);
    AutocompleteRegistryFactory.addDefaultImports(cu, registry, imports.toListOfStrings(), cps);
    Lexer lexer = new JavaLexer(new ANTLRInputStream(txt));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Create a parser that reads from the scanner
    JavaParser parser = new JavaParser(tokens);
    parser.removeErrorListeners();
    // start parsing at the compilationUnit rule
    ParserRuleContext t = parser.compilationUnit();
    ParseTreeWalker walker = new ParseTreeWalker();
    List<AutocompleteCandidate> q = new ArrayList<AutocompleteCandidate>();
    JavaImportDeclarationCompletion extractor = new JavaImportDeclarationCompletion(txt, cur, registry, cps, cu);
    JavaNameBuilder extractor2 = new JavaNameBuilder(registry, cu);
    JavaNodeCompletion extractor3 = new JavaNodeCompletion(txt, cur, registry, cu);
    walker.walk(extractor, t);
    if (extractor.getQuery() != null)
        q.addAll(extractor.getQuery());
    walker.walk(extractor2, t);
    walker.walk(extractor3, t);
    if (extractor3.getQuery() != null)
        q.addAll(extractor3.getQuery());
    List<String> ret = registry.searchCandidates(q);
    if (!ret.isEmpty()) {
        return new AutocompleteResult(ret, getStartIndex(extractor, extractor2, extractor3));
    }
    return findAutocompleteResult(txt, cur, cu);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ArrayList(java.util.ArrayList) ClassUtils(com.twosigma.beakerx.autocomplete.ClassUtils) Lexer(org.antlr.v4.runtime.Lexer) AutocompleteCandidate(com.twosigma.beakerx.autocomplete.AutocompleteCandidate) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) AutocompleteResult(com.twosigma.beakerx.autocomplete.AutocompleteResult)

Example 19 with AutocompleteCandidate

use of com.twosigma.beakerx.autocomplete.AutocompleteCandidate in project beakerx by twosigma.

the class JavaNodeCompletion method exitExpression.

@Override
public void exitExpression(ExpressionContext ctx) {
    if (ctx.getStart().getStartIndex() < cursor && ctx.getStop().getStopIndex() + 1 >= cursor) {
        if (ctx.getChildCount() == 1) {
            // System.out.println("EXP: "+ctx.getStart().getStartIndex()+" "+ctx.getStart().getStopIndex()+" "+ctx.getStart().getText());
            String txt = ctx.getText();
            if (txt.contains(" "))
                return;
            if (text.charAt(cursor - 1) == '.') {
            // TODO (do I need it?)
            } else {
                if (txt.contains(".")) {
                    String[] txtv = txt.split("\\.");
                    AutocompleteCandidate c = new AutocompleteCandidate(JavaCompletionTypes.FQ_TYPE, txtv);
                    addQuery(c, AutocompleteJavaResult.getStartIndex(ctx));
                } else {
                    AutocompleteCandidate c = new AutocompleteCandidate(JavaCompletionTypes.NAME, txt);
                    addQuery(c, AutocompleteJavaResult.getStartIndex(ctx));
                    c = new AutocompleteCandidate(JavaCompletionTypes.CUSTOM_TYPE, txt);
                    addQuery(c, AutocompleteJavaResult.getStartIndex(ctx));
                    if (txt.startsWith("n")) {
                        c = new AutocompleteCandidate(JavaCompletionTypes.NEW, txt);
                        addQuery(c, AutocompleteJavaResult.getStartIndex(ctx));
                    }
                }
            }
        } else {
            // System.out.println("EXP: "+ctx.getStart().getStartIndex()+" "+ctx.getStart().getStopIndex()+" "+ctx.getText());
            addQuery(classUtils.expandExpression(ctx.getText(), registry, classUtils.DO_ALL), AutocompleteJavaResult.getStartIndex(ctx));
        }
    }
}
Also used : AutocompleteCandidate(com.twosigma.beakerx.autocomplete.AutocompleteCandidate)

Example 20 with AutocompleteCandidate

use of com.twosigma.beakerx.autocomplete.AutocompleteCandidate in project beakerx by twosigma.

the class JavaNodeCompletion method exitCompilationUnit.

@Override
public void exitCompilationUnit(CompilationUnitContext ctx) {
    if (ctx.getStop() != null && ctx.getStop().getStopIndex() + 1 == cursor) {
        String t = ctx.getText();
        addQuery(classUtils.expandExpression(t, registry, classUtils.DO_ALL), calculateStartIndex(t, ctx));
        String[] txtv = splitByDot(t);
        AutocompleteCandidate c2 = new AutocompleteCandidate(JavaCompletionTypes.FQ_TYPE, txtv);
        addQuery(c2, calculateStartIndex(t, ctx));
    }
}
Also used : AutocompleteCandidate(com.twosigma.beakerx.autocomplete.AutocompleteCandidate)

Aggregations

AutocompleteCandidate (com.twosigma.beakerx.autocomplete.AutocompleteCandidate)39 AutocompleteResult (com.twosigma.beakerx.autocomplete.AutocompleteResult)4 ArrayList (java.util.ArrayList)4 AutocompleteRegistryFactory.createImportAutocompleteCandidate (com.twosigma.beakerx.groovy.autocomplete.AutocompleteRegistryFactory.createImportAutocompleteCandidate)3 AutocompleteRegistryFactory.createImportAutocompleteCandidate (com.twosigma.beakerx.javash.autocomplete.AutocompleteRegistryFactory.createImportAutocompleteCandidate)3 ClassOrInterfaceTypeContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.ClassOrInterfaceTypeContext)3 PrimitiveTypeContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.PrimitiveTypeContext)3 TypeContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.TypeContext)3 ParseTree (org.antlr.v4.runtime.tree.ParseTree)3 AutocompleteRegistry (com.twosigma.beakerx.autocomplete.AutocompleteRegistry)2 VariableDeclaratorContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.VariableDeclaratorContext)2 VariableDeclaratorIdContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.VariableDeclaratorIdContext)2 VariableDeclaratorsContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.VariableDeclaratorsContext)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 Lexer (org.antlr.v4.runtime.Lexer)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)2 ClassUtils (com.twosigma.beakerx.autocomplete.ClassUtils)1 AssignmentExpressionContext (com.twosigma.beakerx.groovy.autocomplete.GroovyParser.AssignmentExpressionContext)1