2017-04-11 39 views
0

我配置了結構化語言的語法,現在想要編寫自動完成行爲。有沒有一種方法可以基於這樣定義的語法來生成?如何通過語法結構生成自動完成?

RootObject ::= ROOT (NameAttr | TitleAttr)* END 
private NameAttr ::= NAME string 
private TitleAttr ::= TITLE string 

擊中自動完成熱鍵ROOT後應建議END,姓名和頭銜 - 這是在語法

這裏明確規定是完整的語法鏈接:https://raw.githubusercontent.com/dnltsk/intellij-mapfile-plugin/master/src/org/dnltsk/mapfileplugin/Mapfile.bnf

回答

0

之後我想通了一個PsiElement已經包含像"FooTokenType.NAME, FooTokenType.TITLE or FooTokenType.END expected, got 'IntellijIdeaRulezzz'"這樣的通用錯誤描述我已經以非常實用的方式管理了自動完成:

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

這會導致預期的行爲。我希望這有助於其他人,請讓我知道是否有更好的解決方案。