TextAttributesKey: how to modify it's TextAttributes?

What's needed: Create new  TextAttributesKey that inherit another TextAttributesKey and then change the Font style for the new TextAttributesKey.

At the moment I do next:

public static final TextAttributesKey BOLD_FONT =
TextAttributesKey.createTextAttributesKey("bold font", createBoldTextAttributes());

private static TextAttributes createBoldTextAttributes() {
TextAttributes textAttributes =
DefaultLanguageHighlighterColors.DOC_COMMENT.getDefaultAttributes().clone();
/** Set or revert FontType. See {@link Font} */
textAttributes.setFontType(textAttributes.getFontType() ^ Font.BOLD);
return textAttributes;
}

Unfortunately, here we use deprecated TextAttributesKey.createTextAttributesKey() method. Also, a user can't get back to inherited Font style through GUI. And that applies only to the current theme.

The only way to get access to TextAttributes is using TextAttributesKey.getDefaultAttributes() which return parent's TextAttributes and changing it will change parent's TextAttributes as well, which is not desired.

Will highly appreciate any idea and suggestions!

5 comments

you do 

`TextAttributesKey MY_KEY = TextAttributesKey.createTextAttributesKey("MY_KEY");`

and then 

`void change(EditorColorsScheme myScheme) {

  myScheme.setAttributes(MY_KEY, myAttributes);

}`

1

What you can do in your plugin is define bold font for two of the built-in schemes: Default and Darcula. You can find more information on how to do this here: www.jetbrains.org/intellij/sdk/docs/reference_guide/color_scheme_management.html, for example:

<list>
<option name="MY_KEY">
<value>
<option name="FONT_TYPE" value="1"/>
</value>
</option>
</list>

In your code you can just define the attribute itself:

TextAttributesKey MY_KEY = TextAttributesKey.createTextAttributesKey("MY_KEY")

All the custom schemes are based either on Default or Darcula and if they won't have any definition of their own, they will automatically inherit the same bold style.

 

1

Thank you guys for your responses! Looks like Rustam Vishnyakov suggestion works the best in my case. Just a few questions:

<additionalTextAttributes scheme="Darcula" file="colorSchemes/JdcrLangDefault.xml"/>

Will add the same functionality to "Default" scheme as well. Is that intended behavior? (Works great for me as I don't need to put one more line for"Default" scheme) Also, that doesn't work vice-versa: adding for "Default" scheme will not add it to "Darcula" scheme.

Looks like all text inside PsiDocComment element is shown with Italic font. But if I explicitly apply Bold font then I lost that Italic. If I apply Plain (or override Italic) then "underline" Italic still presents. Is that intended behavior? Shouldn't that "underline" Italic been concatenated with anything applied on top of it?...

PS Also, the report is mine, looks like the Non-null check is needed either at compile time or at runtime.

0

I've been not correct about adding the same functionality to "Default" scheme if declare only "Darcula" one. It happened only in the Settings preview. After changing Schema everything work as expected in the Editor.

Actually, there's plenty misleading with that Highlighting preview in settings. For example, it's shown Tag Value for Doc Comment Markup highlighted together with Tag itself, which is not true:


 At the Editor:

0

Please sign in to leave a comment.