Search in sources :

Example 1 with AutocompleteResult

use of com.twosigma.beakerx.autocomplete.AutocompleteResult 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 AutocompleteResult

use of com.twosigma.beakerx.autocomplete.AutocompleteResult 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 AutocompleteResult

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

the class GroovyEvaluatorAutocompleteClassNameExpressionTest method autocompleteToClassWithPackage.

@Test
public void autocompleteToClassWithPackage() throws Exception {
    String code = "def f = new java.text.SimpleDateFor";
    // when
    AutocompleteResult autocomplete = groovyEvaluator.autocomplete(code, code.length());
    // then
    assertThat(autocomplete.getMatches()).isNotEmpty();
    assertThat(autocomplete.getStartIndex()).isEqualTo(code.length() - 13);
}
Also used : AutocompleteResult(com.twosigma.beakerx.autocomplete.AutocompleteResult) Test(org.junit.Test)

Example 4 with AutocompleteResult

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

the class GroovyEvaluatorAutocompleteClassNameExpressionTest method autocompleteToClassWithoutPackage.

@Test
public void autocompleteToClassWithoutPackage() throws Exception {
    String code = "def f = new Inte";
    // when
    AutocompleteResult autocomplete = groovyEvaluator.autocomplete(code, code.length());
    // then
    assertThat(autocomplete.getMatches().size()).isEqualTo(1);
    assertThat(autocomplete.getMatches().get(0)).isEqualTo("Integer");
    assertThat(autocomplete.getStartIndex()).isEqualTo(code.length() - 4);
}
Also used : AutocompleteResult(com.twosigma.beakerx.autocomplete.AutocompleteResult) Test(org.junit.Test)

Example 5 with AutocompleteResult

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

the class GroovyEvaluatorAutocompleteTest method shouldAutocompleteToB.

@Test
public void shouldAutocompleteToB() throws Exception {
    String code = "import java.awt.Color\n" + "println Color.B";
    // when
    AutocompleteResult autocomplete = groovyEvaluator.autocomplete(code, code.length());
    // then
    assertThat(autocomplete.getMatches()).isNotEmpty();
    assertThat(autocomplete.getStartIndex()).isEqualTo(code.length() - 1);
}
Also used : AutocompleteResult(com.twosigma.beakerx.autocomplete.AutocompleteResult) Test(org.junit.Test)

Aggregations

AutocompleteResult (com.twosigma.beakerx.autocomplete.AutocompleteResult)107 Test (org.junit.Test)102 Ignore (org.junit.Ignore)20 BaseEvaluator (com.twosigma.beakerx.evaluator.BaseEvaluator)10 Collectors (java.util.stream.Collectors)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 AfterClass (org.junit.AfterClass)10 BeforeClass (org.junit.BeforeClass)10 EvaluatorTest (com.twosigma.beakerx.evaluator.EvaluatorTest)6 KERNEL_PARAMETERS (com.twosigma.beakerx.evaluator.EvaluatorTest.KERNEL_PARAMETERS)6 EvaluatorTest.getTestTempFolderFactory (com.twosigma.beakerx.evaluator.EvaluatorTest.getTestTempFolderFactory)6 TestBeakerCellExecutor.cellExecutor (com.twosigma.beakerx.evaluator.TestBeakerCellExecutor.cellExecutor)6 JavaEvaluator (com.twosigma.beakerx.javash.evaluator.JavaEvaluator)6 KernelTest (com.twosigma.beakerx.KernelTest)4 AutocompleteCandidate (com.twosigma.beakerx.autocomplete.AutocompleteCandidate)4 TestGroovyEvaluator (com.twosigma.beakerx.groovy.TestGroovyEvaluator)4 ArrayList (java.util.ArrayList)4 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 Lexer (org.antlr.v4.runtime.Lexer)2