How can I change the default error annotation text for my language?

Answered

An error message is displayed for an unclosed tag. 

I created my own class in which I display my text for such cases. 


public class MyAnnotator implements Annotator {
   @Override
   public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
       if (element instanceof PsiErrorElement) {
       holder.newAnnotation(HighlightSeverity.ERROR,  "Chunk tag. Missing closing ]]").create();
       }
   }
}


But the default error text is also displayed. How can I make it so that there is only one text of mine?

 

0
4 comments

Hi,

Please share your BNF and jFlex file (if you have one). I don't understand why token type field reference is displayed instead of element name. Grammar element names can be customized with name modifier:

0

Grammar element names can be customized with name modifier:

But this does not allow you to replace the entire text, but only the name for the token.

 

Lexer.flex

Parser.bnf

 

Now the text that the arrow points to is taken from the token and will be returned by the function toString

public class ModxTokenType extends IElementType {

    public ModxTokenType(@NotNull @NonNls String debugName) {
        super(debugName, ModxLanguage.INSTANCE);
    }

    @Override
    public String toString() {
        return "TokenType." + super.toString();
    }

}
0

In general, I removed the automatic message via com.intellij.codeInsight.highlighting#HighlightErrorFilter

But to what extent is this decision correct?

0

I understand that you muted syntax errors and added an annotator for reporting syntax error. Is it the case? If yes, then this decision is incorrect. Parser should be responsible for creating syntax error messages, so that user can see them as soon as possible in the editor.

You can alter error reporting by implementing your parser util class. It should extend GeneratedParserUtilBase and be registered in BNF as:

{
  ...
  parserUtilClass="com.example.mylang.parser.MyLangParserUtil"
  ...
}

In your util implementation you can override report_error_ method(s) which can handle your special cases.

See https://github.com/JetBrains/Grammar-Kit/blob/master/src/org/intellij/grammar/parser/GeneratedParserUtilBase.java#L748-L819 for the original implementation that creates these error messages.

This information is mentioned in https://github.com/JetBrains/Grammar-Kit#generated-parser-structure

1

Please sign in to leave a comment.