How to use decorators to change the text color?

SyntaxEditor for WPF Forum

Posted 3 years ago by Sunshine - Appeon
Version: 21.1.0
Avatar

In some cases, such as defining unused variables, I need to mark the color of this variable as gray. How to achieve this effect in this situation? Is there any relevant sample code?

Comments (3)

Posted 3 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

If you'd like to change the color of some ranges of text, the two ways to do that are (1) via the syntax language's lexer, or (2) with classification tags.  For something like your situation, the lexer won't really know which variables are used or unused.  You'd probably need to determine that kind of thing in a parser and would have to return a hash set of unused variable names in the parse data returned by the parser.

Then if you set up a classification tagger, you can override the syntax highlighting of various ranges of text.  Classification taggers are what you want to use when you still want your lexer to drive syntax highlighting via tokens, but you need to change the foreground and/or background or certain text ranges.  Several QuickStarts like AdornmentsHighlightWord and AdornmentsIntraTextNotes show the use of classification taggers.


Actipro Software Support

Posted 3 years ago by Sunshine - Appeon
Avatar

Sorry, neither method is suitable for this situation.

For example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Diagnostics;

The code above has used the classification marker.

"Using" belongs to the keyword.

"System" belongs to the namespace.

Assuming that the "use system" content is not used in the code, then in the editor I want to change the display to "using System", for example to make it less transparent or to change the color.

In this case, the definition of the category tag cannot be changed because it will affect other content.

Is there a way to directly change the transparency of the text in the specified range?

Posted 3 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

What you are describing is the #1 that I was mentioning where the syntax language's ILexer breaks up the text into tokens (keyword, identifier, etc.).  Each IToken is assigned an IClassificationType, and that IClassificationType can have an IHighlightingStyle associated with it in the IHighlightingStyleRegistry in use.  The style is what determines what colors text will tokenize to.

If you also add a classification tagger service to your syntax language, it allows you to override the colors of various ranges.  So in your tagger, you could detect that the word "using" is on the line and have your tagger return an IClassificationType that triggers a registered IHighlightingStyle with foreground of red for that text range.  That would still have it be a keyword token, but it would visually have a red foreground.

We don't currently have a way to make text semi-transparent, but that's a good suggestion to possibly add to IHighlightingStyle in the future.  We'll write that down.


Actipro Software Support

The latest build of this product (v24.1.2) was released 1 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.