I'm trying to make autocompletion for my language using Netbeans Code Completion, Parsing and Lexer APIs with ANTLR. I have generated java classes based on ANTLR grammar file, such as ANTLR Parser, Lexer and BaseListener. My goal is to make autocompletion that is based on the current scope. For instance, class scope, method scope, inner class scope etc.
How do I determine scope by caret position in the editor?
Is there a mechanism that allows to determine current scope before calling code completion popup window to prepare completion items based on current scope?
public class XxxParser extends Parser { private Snapshot snapshot; private XxxcodeParser xxxcodeParser; @Override public void parse(Snapshot snapshot, Task task, SourceModificationEvent event) { this.snapshot = snapshot; ANTLRInputStream input = new ANTLRInputStream(snapshot.getText().toString()); Lexer lexer = new XxxcodeLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); xxxcodeParser = new XxxcodeParser(tokens); xxxcodeParser.addParseListener(new XxxcodeBaseListener()); CompilationUnitContext context = xxxcodeParser.compilationUnit(); }
XxxcodeBaseListener class contains enter and exit methods. When I type letter in the editor,
parse(Snapshot snapshot, Task task, SourceModificationEvent event)
method executes. While parsing current file, listener calls different enter and exit methods such as@Override public void enterClassBody( XxxcodeParser.ClassBodyContext ctx) { }
,@Override public void exitClassBody(XxxcodeParser.ClassBodyContext ctx) { }
etc.