Dynamic TextAttributes effect

Answered

I saw that `highlightManager.addRangeHighlight()` with `TextAttributes` is deprecated, and I know that the new API uses the `TextAttributesKey` way. But as I see that,  `TextAttributesKey` is used to load static attributes from schema like:

https://intellij-support.jetbrains.com/hc/en-us/community/posts/360001238560/comments/360000183480

But in my plugin, I'm dynamically creating the colors and effects, so I could not load static colors from schema using `TextAttributesKey`:

object MyHighlightingAction : AnAction() {

    final override fun actionPerformed(e: AnActionEvent){
        val defaultBackground = EditorColorsManager.getInstance().globalScheme.defaultBackground
        val effect = color.alphaBlend(defaultBackground, 0.8)
        val attributes = TextAttributes(null, null, effect, EffectType.BOXED, Font.PLAIN)
        val highlighters = LinkedList<RangeHighlighter>()
        try {
            highlightManager.addRangeHighlight(this,
                startOffset,
                endOffset,
                attributes, 
                false, 
                false,
                highlighters)
        } catch (_: Exception) {
        }
        return highlighters
    }

}

 

Since the `highlightManager.addRangeHighlight()` with `TextAttributes` is deprecated, how can I migrate to the new API?

1
1 comment

Hi,

The reason for deprecating TextAttributes in favor of TextAttributeKey is that the former is more friendly for Code With Me and remote development. Thanks to passing TextAttributesKey IDs, client IDEs are able to apply colors according to their local schemes, while with raw TextAttributes it is not possible (in this case, colors applied to the editor may be inconsistent with a local color scheme).

You can still use the TextAttributes API if you cannot switch to keys. I've got information that it is not planned to remove it in the near future as there are cases where it is needed. Please note that using it may cause mentioned color issues in remote environments.

It is recommended to use TextAttributesKey whenever possible.

1

Please sign in to leave a comment.