Search in sources :

Example 1 with AutocompleteCandidate

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

the class GroovyAutocomplete method tryFindAutocomplete.

private AutocompleteResult tryFindAutocomplete(String txt, int cur, ClassLoader l, Imports imports) {
    registry = AutocompleteRegistryFactory.createRegistry(cps);
    GroovyClassUtils cu = createClassUtils(l);
    setup(cu, registry);
    AutocompleteRegistryFactory.addDefaultImports(cu, registry, imports.toListOfStrings(), cps);
    AutocompleteRegistryFactory.moreSetup(cu);
    Lexer lexer = new GroovyLexer(new ANTLRInputStream(txt));
    lexer.removeErrorListeners();
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Create a parser that reads from the scanner
    GroovyParser parser = new GroovyParser(tokens);
    parser.removeErrorListeners();
    // start parsing at the compilationUnit rule
    ParserRuleContext t = parser.compilationUnit();
    ParseTreeWalker walker = new ParseTreeWalker();
    List<AutocompleteCandidate> q = new ArrayList<>();
    GroovyImportDeclarationCompletion extractor = new GroovyImportDeclarationCompletion(txt, cur, registry, cps, cu);
    GroovyNameBuilder extractor2 = new GroovyNameBuilder(registry, cu);
    GroovyNodeCompletion extractor3 = new GroovyNodeCompletion(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) 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 2 with AutocompleteCandidate

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

the class GroovyAutocomplete 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(GroovyCompletionTypes.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 3 with AutocompleteCandidate

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

the class GroovyNameBuilder method handleRestOfCases.

private void handleRestOfCases(AssignmentExpressionContext ctx) {
    if (GroovyCompletionTypes.debug)
        System.out.println(((ExpressionContext) ctx.getChild(2)).getStart().getType());
    String typpen = null;
    switch(((ExpressionContext) ctx.getChild(2)).getStart().getType()) {
        case GroovyLexer.STRING:
            typpen = "String";
            break;
        case GroovyLexer.INTEGER:
            typpen = "Integer";
            break;
        case GroovyLexer.DECIMAL:
            typpen = "Double";
            break;
    }
    AutocompleteCandidate c = new AutocompleteCandidate(GroovyCompletionTypes.NAME, ctx.getChild(0).getText());
    registry.addCandidate(c);
    if (GroovyCompletionTypes.debug)
        logger.info("define variable of type " + ctx.getChild(0).getText() + " " + typpen);
    if (typpen != null)
        classUtils.defineVariable(ctx.getChild(0).getText(), typpen);
}
Also used : AutocompleteCandidate(com.twosigma.beakerx.autocomplete.AutocompleteCandidate)

Example 4 with AutocompleteCandidate

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

the class GroovyNameBuilder method handleRuleContext.

private void handleRuleContext(AssignmentExpressionContext ctx, ParseTree child) {
    ParseTree t = findChildrenByType(child, ClassNameExpressionContext.class);
    if (t != null) {
        String ttype = t.getText().trim();
        AutocompleteCandidate c = new AutocompleteCandidate(GroovyCompletionTypes.NAME, ctx.getChild(0).getText());
        registry.addCandidate(c);
        if (GroovyCompletionTypes.debug)
            logger.info("define variable of type " + ctx.getChild(0).getText() + " " + ttype);
        if (ttype != null)
            classUtils.defineVariable(ctx.getChild(0).getText(), ttype);
    }
}
Also used : AutocompleteCandidate(com.twosigma.beakerx.autocomplete.AutocompleteCandidate) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 5 with AutocompleteCandidate

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

the class GroovyNameBuilder method nameDeclaration.

private void nameDeclaration(DeclarationRuleContext ctx) {
    ParseTree t = findChildrenByType(ctx.getChild(3).getChild(0), ClassNameExpressionContext.class);
    if (t != null) {
        String ttype = t.getText().trim();
        AutocompleteCandidate c = new AutocompleteCandidate(GroovyCompletionTypes.NAME, ctx.getChild(1).getText());
        registry.addCandidate(c);
        if (GroovyCompletionTypes.debug)
            logger.info("define variable of type " + ctx.getChild(1).getText() + " " + ttype);
        if (ttype != null)
            classUtils.defineVariable(ctx.getChild(1).getText(), ttype);
    }
}
Also used : AutocompleteCandidate(com.twosigma.beakerx.autocomplete.AutocompleteCandidate) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

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