How to dynamically colorize selected text?

SyntaxEditor for WPF Forum

Posted 4 years ago by Michael Fraser
Version: 16.1.0633

I am interested to know if it is possible to colorize ranges of user-selected text on the fly.

Assuming the plain text syntax editor contains the following text: "Aaa bbb ccc ddd eee fff", the user manually selects "Aaa" and wants to make that range red then the user selects "ccc" and wants to turn that range blue (by clicking on buttons with the corresponding colors), what would be the programatic approach to achieveing this?

I am at beginner level and I did go through the documentation resources and examples, but I haven't managed to come up with a solution to this requirement.
If you could provide a working code snippet it would me greatly appreciated.

Comments (3)

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

Hi Michael,

While SyntaxEditor is geared more for lexical tokenization of text being what colors the text, we do support classification tagging of various ranges and you can add a highlighting style for that classification tag so that the tagged range gets a different foreground, etc. 

The Adornments 6 - Code Reviewing QuickStart is actually similar to this idea where you highlight a range and it colors the background of that range, while also injecting a button within the text.  Your scenario probably isn't as complex as that, such as you don't really need the button.  Check that sample out.

Just note that while classification tagging will work fine to achieve highlighting various ranges, if you use it extensively (hundreds or thousands of tags in a document), then you might start to see editor performance drop.

Actipro Software Support

Posted 4 years ago by Michael Fraser

Hello. This indeed seems to be what I am looking for.
I have checked the sample folder, but I am not entirely sure how to implement this feature into my plain text syntax editor.

Could you provide some basic steps I could follow? It would be much appreciated, thank you.

Answer - Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Michael,

At a minimum, you need to make a syntax language class that registers a tagger provider, which will create/return a tagger class you define.  This would need to be in the syntax language:

this.RegisterService(new CodeDocumentTaggerProvider<YourTagger>(typeof(YourTagger)));

YourTagger is a class that should inherit CollectionTagger<IClassificationTag> and have a constructor like:

public YourTagger(ICodeDocument document) : base("YourTagger", null, document, true) {}

Once you use that syntax language then you can add classification tags to the tagger and they should show up in editor as long as you've registered an IHighlightingStyle in the AmbientHighlightingStyleRegistry for the custom ClassificationType you will use.  To get the tagger from code for a document, do something like this:

YourTagger tagger = null;
if (editor.Document.Properties.TryGetValue(typeof(YourTagger), out tagger)) {
    var versionRange = new TextSnapshotRange(editor.ActiveView.CurrentSnapshot, 1, 4).ToVersionRange(
        TextRangeTrackingModes.ExpandFirstEdge | TextRangeTrackingModes.DeleteWhenZeroLength);
    var tag = new ClassificationTag(ClassificationTypes.Keyword);
    tagger.Add(new TagVersionRange<ClassificationTag>(versionRange, tag));

That example shows how to mark a range (1-4) of text with a Keyword classification type.  As long as that classification type has been registered with a style (like the normal blue one), then it will render blue.

Hope that helps!

Actipro Software Support

The latest build of this product (v2020.1.1) was released 6 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.