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