Search in sources :

Example 1 with ExpressionContext

use of com.twosigma.beakerx.javash.autocomplete.JavaParser.ExpressionContext 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)

Aggregations

AutocompleteCandidate (com.twosigma.beakerx.autocomplete.AutocompleteCandidate)1 BlockStatementContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.BlockStatementContext)1 ClassBodyContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.ClassBodyContext)1 ClassBodyDeclarationContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.ClassBodyDeclarationContext)1 CompilationUnitContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.CompilationUnitContext)1 ExpressionContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.ExpressionContext)1 MemberDeclarationContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.MemberDeclarationContext)1 TypeDeclarationContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.TypeDeclarationContext)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1