Is there a way too show whitespace characters in the syntax editor?

1 Answer 96 Views
SyntaxEditor
Mattias
Top achievements
Rank 1
Mattias asked on 10 Aug 2022, 11:31 AM
It would be good too see if it's either a tab or many spaces that are used in the code.

1 Answer, 1 is accepted

Sort by
0
Stenly
Telerik team
answered on 15 Aug 2022, 11:08 AM

Hello Mattias,

To show the whitespaces, you could highlight them using a custom tagger. An example of this can be found in the Taggers example from our Demos application.

The following code snippet shows a sample implementation of this tagger:

public class CustomTextHighlightTagger : TaggerBase<TextHighlightTag>
{
    public static readonly ITextFormatDefinitionKey CustomHighlightDefinition =
        new TextFormatDefinitionKey("CustomHighlightDefinition");

    private ITextFormatDefinitionKey formatDefinitionKey;
    private string searchWord;

    public CustomTextHighlightTagger(RadSyntaxEditor editor, ITextFormatDefinitionKey definitionKey, string searchWord)
        : base(editor)
    {
        this.formatDefinitionKey = definitionKey;

        this.searchWord = searchWord;
        editor.TextFormatDefinitions.AddLast("CustomHighlightDefinition", new TextFormatDefinition(new Telerik.Windows.Controls.SyntaxEditor.UI.Pen(Brushes.Orange, 1)));
    }

    public override IEnumerable<TagSpan<TextHighlightTag>> GetTags(NormalizedSnapshotSpanCollection spans)
    {
        if (string.IsNullOrEmpty(this.searchWord))
        {
            yield break;
        }

        TextSnapshot snapshot = this.Document.CurrentSnapshot;
        foreach (TextSnapshotSpan snapshotSpan in spans)
        {
            string lineString = snapshotSpan.GetText();
            int index = lineString.IndexOf(this.searchWord);
            while (index != -1)
            {
                TextSnapshotSpan tempSnapshotSpan = new TextSnapshotSpan(snapshot,
                    new Telerik.Windows.SyntaxEditor.Core.Text.Span(snapshotSpan.Start + index, searchWord.Length));

                yield return new TagSpan<TextHighlightTag>(tempSnapshotSpan, new TextHighlightTag(this.formatDefinitionKey));

                index = lineString.IndexOf(this.searchWord, index + this.searchWord.Length);
            }
        }
    }
}

Registering the custom tagger in the RadSyntaxEditor instance:

var whitespaceHighlightsTagger = new CustomTextHighlightTagger(this.syntaxEditor, CustomTextHighlightTagger.CustomHighlightDefinition, " ");
this.syntaxEditor.TaggersRegistry.RegisterTagger(whitespaceHighlightsTagger);

The produced result:

Regarding the highlighting of a tabulation, I have tried incorporating this functionality, while keeping the above logic about the whitespaces, however, I was unsuccessful.

Could you give this suggestion a try?

Regards,
Stenly
Progress Telerik

The Premier Dev Conference is back! 

Coming to you live from Progress360 in-person or on your own time, DevReach for all. Register Today.


Tags
SyntaxEditor
Asked by
Mattias
Top achievements
Rank 1
Answers by
Stenly
Telerik team
Share this question
or