locked
Implementing Error Markers RRS feed

  • Question

  • In the page on implementing error markers a method is suggested on how to make squiggly wavy red lines.

    QuestionThis page is filed under "LEGACY interfaces in the Editor", is this approach superseded by a newer method? I am targeting vs2015. 

    Implementing such features can be daunting, and I cannot find tutorials or examples on this subject. Do they exist? If not, could I request such a example, and where would I do that?

    Regards!

    Saturday, April 23, 2016 11:17 AM

Answers

  • You are right. Just to answer my own question, this is the way to do it.

        internal sealed class ErrorTagger : ITagger<ErrorTag> {
            private readonly ITextView _view;
            private readonly ITextBuffer _sourceBuffer;
            private readonly ITagAggregator<MyTokenTag> _aggregator;
    
            internal ErrorTagger(
                    ITextView view, 
                    ITextBuffer buffer,
                    ITagAggregator<MyTokenTag> asmTagAggregator) {
                this._view = view;
                this._sourceBuffer = buffer;
                this._aggregator = asmTagAggregator;
            }
    
            event EventHandler<SnapshotSpanEventArgs> TagsChanged;
    
            IEnumerable<ITagSpan<ErrorTag>> ITagger<ErrorTag>.GetTags(NormalizedSnapshotSpanCollection spans) {
                foreach (IMappingTagSpan<MyTokenTag> tagSpan in _aggregator.GetTags(spans)) {
                    NormalizedSnapshotSpanCollection tagSpans = tagSpan.Span.GetSpans(spans[0].Snapshot);
    		if (test_On_tagSpan) { // test whether tagSpan has an error here
    		    yield return new TagSpan<ErrorTag>(tagSpans[0], new ErrorTag("syntax error", "my error message"));
                    }
                }
            }
        }
    	
        [Export(typeof(IViewTaggerProvider))]
        [ContentType(MyContentType)]
        [TagType(typeof(ErrorTag))]
        internal sealed class ErrorTaggerProvider : IViewTaggerProvider {
    
            [Import]
            private IBufferTagAggregatorFactoryService _aggregatorFactory = null;
    
            public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag {
                ITagAggregator<MyTokenTag> asmTagAggregator = _aggregatorFactory.CreateTagAggregator<MyTokenTag>(buffer);
                return new ErrorTagger(textView, buffer, asmTagAggregator) as ITagger<T>;
            }
        }


    Thursday, April 28, 2016 6:51 PM

All replies

  • Hi Henk-Jan Lebbink,

    >>"This page is filed under "LEGACY interfaces in the Editor", is this approach superseded by a newer method? "

    Yes, the newer method is MEF. Visual Studio allow us extending most editor features by using the Managed Extensibility Framework (MEF). We could implement it by implement TextMarkerTag. Following example show us how to add Highlighting Text for text editor.

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 25, 2016 7:04 AM
  • You are right. Just to answer my own question, this is the way to do it.

        internal sealed class ErrorTagger : ITagger<ErrorTag> {
            private readonly ITextView _view;
            private readonly ITextBuffer _sourceBuffer;
            private readonly ITagAggregator<MyTokenTag> _aggregator;
    
            internal ErrorTagger(
                    ITextView view, 
                    ITextBuffer buffer,
                    ITagAggregator<MyTokenTag> asmTagAggregator) {
                this._view = view;
                this._sourceBuffer = buffer;
                this._aggregator = asmTagAggregator;
            }
    
            event EventHandler<SnapshotSpanEventArgs> TagsChanged;
    
            IEnumerable<ITagSpan<ErrorTag>> ITagger<ErrorTag>.GetTags(NormalizedSnapshotSpanCollection spans) {
                foreach (IMappingTagSpan<MyTokenTag> tagSpan in _aggregator.GetTags(spans)) {
                    NormalizedSnapshotSpanCollection tagSpans = tagSpan.Span.GetSpans(spans[0].Snapshot);
    		if (test_On_tagSpan) { // test whether tagSpan has an error here
    		    yield return new TagSpan<ErrorTag>(tagSpans[0], new ErrorTag("syntax error", "my error message"));
                    }
                }
            }
        }
    	
        [Export(typeof(IViewTaggerProvider))]
        [ContentType(MyContentType)]
        [TagType(typeof(ErrorTag))]
        internal sealed class ErrorTaggerProvider : IViewTaggerProvider {
    
            [Import]
            private IBufferTagAggregatorFactoryService _aggregatorFactory = null;
    
            public ITagger<T> CreateTagger<T>(ITextView textView, ITextBuffer buffer) where T : ITag {
                ITagAggregator<MyTokenTag> asmTagAggregator = _aggregatorFactory.CreateTagAggregator<MyTokenTag>(buffer);
                return new ErrorTagger(textView, buffer, asmTagAggregator) as ITagger<T>;
            }
        }


    Thursday, April 28, 2016 6:51 PM