I configured the grammar of a structured language and now want to code the autocompletion behaviour. Is there a way to generate that based on the grammar which is defined like this?
RootObject ::= ROOT ( NameAttr | TitleAttr )* END
private NameAttr ::= NAME string
private TitleAttr ::= TITLE string
Hitting the Autocompletion Hotkey after ROOT should suggest END, NAME and TITLE - which is clearly defined in the grammar
Here is a link to the complete grammar: https://raw.githubusercontent.com/dnltsk/intellij-mapfile-plugin/master/src/org/dnltsk/mapfileplugin/Mapfile.bnf
After I figured out that the an PsiElement already contains a generic error description like "FooTokenType.NAME, FooTokenType.TITLE or FooTokenType.END expected, got 'IntellijIdeaRulezzz'"
I've managed the Autocompletion in a very pragmatical way:
public void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {
PsiElement element = parameters.getPosition().getParent();
String genericErrorDescription = ((PsiErrorElementImpl) element).getErrorDescription();
errorDescription = errorDescription.substring(0, errorDescription.indexOf(" expected, got "));
errorDescription = errorDescription.replaceAll("FooTokenType\\.", "");
String[] suggestedTokens = errorDescription.split("(, )|( or )");
for (String suggestedToken : suggestedTokens) {
resultSet.addElement(LookupElementBuilder.create(suggestedToken));
}
}
Which results in the expected behavior. I hope this helps others and please let me know if there is a better solution around.