Create new TextAttributesKey by inheriting from existing one

Answered

For the syntax highlighter in my custom language plugin, I want to create a new TextAttributesKey by inheriting from `DefaultLanguageHighlighterColors.KEYWORD` and changing the font type to italic.

Please note that I find the answers in https://intellij-support.jetbrains.com/hc/en-us/community/posts/360001238560-TextAttributesKey-how-to-modify-it-s-TextAttributes- unsatisfactory, as I don't want to deal with themes -- I'd like a solution that works for any theme.

4 comments
Comment actions Permalink

Inheritance here always means inheriting all attributes. User should modify "Defaults" only and all inheritors will change accordingly. This will not work if you inherit AND modify some attributes. You'll need to define custom attributes as detailed in the linked thread to have full control.

0
Comment actions Permalink

I must admit I don't really understand that concept.  Is this also the reason why createTextAttributesKey with externalName and defaultAttributes is deprecated in favor of using externalName and fallbackAttributeKey?

Anyway, in the example above, I want to copy the KEYWORD attribute to make an italic version of it.  Why can't I do this programmatically at run time instead of configuring it with explicit knowledge of themes?  I'd like to do this specifically to keep consistency across themes.  If I did this the way described this would absolutely fail for new themes, but not if I can do it my way.

0
Comment actions Permalink

There's a

createTempTextAttributesKey

but that doesn't work for the Syntax Highlighter.

0
Comment actions Permalink

I did it, but it is not pretty as the other definitions:

public MySyntaxHighlighter() {
TextAttributes myAttr = new TextAttributes();
myAttr.copyFrom(DefaultLanguageHighlighterColors.KEYWORD.getDefaultAttributes());
myAttr.setFontType(Font.ITALIC);
XINSTRUCTION = createTextAttributesKey("MY_XINSTR", myAttr);
XINSTRUCTION_KEYS = new TextAttributesKey[]{XINSTRUCTION};
}

But I really wouldn't know how to do this if the deprecated method went away.

0

Please sign in to leave a comment.