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);
}
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);
}
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);
}
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);
}
}
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);
}
}
Aggregations