Contextual syntax highlighting not based only on lexer

I'm trying to figure out how I can extend my syntax highlighter to provide contextual highlighting similar to the Java editor, e.g., different highlights for class variables vs. member variables vs. constants.  The standard syntax highlighter is just passed a token type and not the actual token.  I'm assuming this is some kind of second pass highlighting, but I can't seem to figure out how it's done.  Any tips?

Thanks in advance!

UPDATE: I may have just figured this out.  It looks like it may occur through ColoredItemPresentation.getTextAttributesKey().  Going to try that out this evening and see whether it does it or not.

UPDATE 2: Nope, that just seems to affect things like the structure view, not the editor.

UPDATE 3: Looks like I need an Annotator for this (http://www.jetbrains.org/intellij/sdk/docs/reference_guide/custom_language_support/syntax_highlighting_and_error_highlighting.html).  I'll resolve this as answered once I confirm.

UPDATE 4: Yup, that does it.

4 comments
Comment actions Permalink

Hi Scott,

Yeah, I use an Annotator for this sort of thing. There's a slight delay before the items are highlighted but it works well.

Cheers,
Colin

0
Comment actions Permalink

Thanks for confirming, Colin.  Now for the follow-on question...do you know if there's any way for an annotator to replace the text attributes completely?  In my custom language, some keywords are also valid to be used as identifiers (yes, it's made for some interesting parser rules!).  I'd prefer to have these annotated by default as keywords during lexer-based highlighting and then, if the token was actually an identifier (declaration or usage), I would highlight it as such in the annotator.  I've tried this and it doesn't seem to work.

The alternative is to have first-pass highlighting not highlight these as keywords and then do so in the annotator, but that makes keyword highlighting lag a bit.

Thoughts?

0
Comment actions Permalink

Hi Scott,

yes, that is pretty easy. Just erase all text-attributes first. For this, use com.intellij.lang.annotation.Annotation#setEnforcedTextAttributes and give it as argument com.intellij.openapi.editor.markup.TextAttributes#ERASE_MARKER. In code this looks like

annotation.setEnforcedTextAttributes(TextAttributes.ERASE_MARKER);


You can find an example for instance here in the function setHighlighingStrict.

Cheers
Patrick

0
Comment actions Permalink

Perfect!  Thanks, Patrick!

0

Please sign in to leave a comment.