How to highlight error text in SyntaxEditor ?

2 Answers 185 Views
SyntaxEditor
Stéphane
Top achievements
Rank 1
Iron
Stéphane asked on 09 Dec 2021, 07:50 AM

Hello,

I use the SyntaxEditor with the MS Roslyn compiler toolbox.

1) The location of the source of the compiler error (e.g., an unknown identifier) is defined by the first and last character positions of the identifier.

How to convert these positions into line number and character position in the SyntaxEditor?

 

2)  How to underline (e.g. in wavy red, like in Visual studio) temporarily, the source of the error, in the SyntaxEditor?

Thanks

Stephane

2 Answers, 1 is accepted

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 13 Dec 2021, 04:18 PM

Hello Stéphane,

The desired functionality can be achieved by creating a custom tagger and overriding its GetTags method. You can expose an Errors property, for example, of type ObservableCollection and have it contain items which hold the line number, start and end of each error (possibly in a Tuple<int, int, int>). Here's what I have in mind:

        public override IEnumerable<TagSpan<UnderlineTag>> GetTags(NormalizedSnapshotSpanCollection spans)
        {
            foreach (var error in this.Errors)
            {
                TextSnapshot snapshot = this.Document.CurrentSnapshot;
                var lineSpan = this.Document.CurrentSnapshot.GetLineFromLineNumber(error.Item1);
                var start = lineSpan.Span.Start + error.Item2;
                var length = error.Item3 - error.Item2;
                TextSnapshotSpan tempSnapshotSpan = new TextSnapshotSpan(lineSpan.Snapshot,
                    new Telerik.Windows.SyntaxEditor.Core.Text.Span(start, length));

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

To have the wavy red underline, you can use the ErrorUnderlineDefinition:

            this.errorsTagger = new CustomErrorTagger(this.syntaxEditor, TextSearchUnderlineTagger.ErrorUnderlineDefinition);
            this.syntaxEditor.TaggersRegistry.RegisterTagger(this.errorsTagger);

For your convenience, I've prepared a small sample project to demonstrate this in action.

Please have a look and let me know if a similar approach would work for you.

Regards,
Dilyan Traykov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Stéphane
Top achievements
Rank 1
Iron
answered on 14 Dec 2021, 12:23 PM

Hello Dilyan,

Thank you for your answer and demo project,  it is perfect for me.

Regards

Stéphane

 

Tags
SyntaxEditor
Asked by
Stéphane
Top achievements
Rank 1
Iron
Answers by
Dilyan Traykov
Telerik team
Stéphane
Top achievements
Rank 1
Iron
Share this question
or