Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TextSearchTagger.cs « TextSearch « Impl « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 150a67b60e761e6de716c3f836e42bfc906f47e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  Licensed under the MIT License. See License.txt in the project root for license information.
//
// This file contain implementations details that are subject to change without notice.
// Use at your own risk.
//
namespace Microsoft.VisualStudio.Text.Find.Implementation
{
    using Microsoft.VisualStudio.Text;
    using Microsoft.VisualStudio.Text.Operations;
    using Microsoft.VisualStudio.Text.Tagging;
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// A general tagger that takes a search term and tags all matching occurences of it.
    /// </summary>
    /// <remarks>
    /// This tagger -- like most others -- will not raise a TagsChanged event when the buffer changes.
    /// </remarks>
    internal sealed class TextSearchTagger<T> : ITextSearchTagger<T> where T : ITag
    {
        // search service to use for doing the real search
        ITextSearchService2 _searchService;

        // list of items to search for and tag
        internal IList<BackgroundSearch<T>> _searchTerms = new List<BackgroundSearch<T>>();

        // buffer over which search is being performed
        ITextBuffer _buffer;

        public TextSearchTagger(ITextSearchService2 searchService, ITextBuffer buffer)
        {
            _searchService = searchService;
            _buffer = buffer;
        }

        #region Private Helpers

        private void InvalidateTags(SnapshotSpan span)
        {
            EventHandler<SnapshotSpanEventArgs> tagsChangedListeners = this.TagsChanged;

            if (tagsChangedListeners != null)
            {
                tagsChangedListeners.Invoke(this, new SnapshotSpanEventArgs(span));
            }
        }

        private void InvalidateTags()
        {
            this.InvalidateTags(new SnapshotSpan(_buffer.CurrentSnapshot, 0, _buffer.CurrentSnapshot.Length));
        }

        internal void ResultsCalculated(ITextSnapshot snapshot, NormalizedSpanCollection spans)
        {
            if (spans.Count > 0)
            {
                SnapshotSpan changedSpan = new SnapshotSpan(snapshot, Span.FromBounds(spans[0].Start, spans[spans.Count - 1].End));
                this.InvalidateTags(changedSpan);
            }
        }

        #endregion

        #region ITextSearchTagger<T> Members

        private NormalizedSnapshotSpanCollection _searchSpans;
        public NormalizedSnapshotSpanCollection SearchSpans
        {
            get
            {
                return _searchSpans;
            }
            set
            {
                if (value != null)
                {
                    if (value.Count == 0)
                    {
                        //Treat an empty collection as if it were null.
                        value = null;
                    }
                    else if (value[0].Snapshot.TextBuffer != _buffer)
                    {
                        throw new ArgumentException("The provided SearchSpan value must belong to the same buffer as the tagger itself.");
                    }
                }

                if (value == _searchSpans)
                {
                    return;
                }

                _searchSpans = value;

                this.InvalidateTags();
            }
        }

        public void TagTerm(string searchTerm, FindOptions searchOptions, Func<SnapshotSpan, T> tagFactory)
        {
            if ((searchOptions & FindOptions.SearchReverse) == FindOptions.SearchReverse)
            {
                throw new ArgumentException("FindOptions.SearchReverse is invalid as searches are performed forwards to ensure all matches in a requested search span are found.", nameof(searchOptions));
            }

            if ((searchOptions & FindOptions.Wrap) == FindOptions.Wrap)
            {
                throw new ArgumentException("FindOptions.Wrap is invalid as searches are performed forwards with no wrapping to ensure all matches in a requested span are found.", nameof(searchOptions));
            }

            _searchTerms.Add(new BackgroundSearch<T>(_searchService, _buffer, searchTerm, searchOptions, tagFactory, this.ResultsCalculated));

            this.InvalidateTags();
        }

        public void ClearTags()
        {
            if (_searchTerms.Count == 0)
            {
                return;
            }

            ITextSnapshot snapshot = _buffer.CurrentSnapshot;
            int start = int.MaxValue;
            int end = int.MinValue;

            foreach (BackgroundSearch<T> search in _searchTerms)
            {
                // Abort any ongoing background search operation since we no longer are interested in the results
                NormalizedSnapshotSpanCollection results = search.Results;

                if ((results != null) && (results.Count > 0))
                {
                    int s = results[0].Start.TranslateTo(snapshot, PointTrackingMode.Negative);
                    if (s < start)
                        start = s;

                    int e = results[results.Count - 1].End.TranslateTo(snapshot, PointTrackingMode.Positive);
                    if (e > end)
                        end = e;
                }

                search.Dispose();
            }

            // Clear all currently tagging search terms
            _searchTerms.Clear();

            // Notify listeners of changed tags over the span where we had any results.
            if (start < end)
                this.InvalidateTags(new SnapshotSpan(snapshot, start, end - start));
        }

        #endregion

        #region ITagger<T> Members

        public IEnumerable<ITagSpan<T>> GetTags(NormalizedSnapshotSpanCollection requestedSpans)
        {
            //We should always be called with a non-empty span.
            if (requestedSpans != null && requestedSpans.Count > 0)
            {
                ITextSnapshot searchSnapshot = _buffer.CurrentSnapshot;
                requestedSpans = new NormalizedSnapshotSpanCollection(searchSnapshot, TextSearchNavigator.TranslateTo(requestedSpans[0].Snapshot, requestedSpans, searchSnapshot));

                if ((_searchSpans != null) && (_searchSpans.Count > 0))
                {
                    //The search has been narrowed via _searchSpan ... limit the request to the search range (after making sure it is on the correct snapshot).
                    if (_searchSpans[0].Snapshot != searchSnapshot)
                    {
                        NormalizedSpanCollection newSpans = TextSearchNavigator.TranslateTo(_searchSpans[0].Snapshot, _searchSpans, searchSnapshot);
                        _searchSpans = new NormalizedSnapshotSpanCollection(searchSnapshot, newSpans);
                    }

                    requestedSpans = new NormalizedSnapshotSpanCollection(searchSnapshot, NormalizedSpanCollection.Intersection(requestedSpans, _searchSpans));

                    if (requestedSpans.Count == 0)
                    {
                        yield break;
                    }
                }

                foreach (var search in _searchTerms)
                {
                    //Queue up a search if we need one.
                    search.QueueSearch(requestedSpans);

                    //Report any results from the search (if we've got them)
                    var results = search.Results;
                    if (results.Count > 0) 
                    {
                        //Results could be on an old snapshot (and, if so, a new search has already been queued up) but we need to get the results on the current snapshot.
                        if (results[0].Snapshot != searchSnapshot)
                        {
                            results = new NormalizedSnapshotSpanCollection(searchSnapshot, TextSearchNavigator.TranslateTo(results[0].Snapshot, results, searchSnapshot));
                        }

                        if (_searchSpans != null)
                        {
                            results = new NormalizedSnapshotSpanCollection(searchSnapshot, NormalizedSpanCollection.Intersection(results, _searchSpans));
                        }

                        int start = 0;
                        foreach (var span in requestedSpans)
                        {
                            start = TextSearchTagger<T>.IndexOfContainingSpan(results, span.Start, start, false);
                            if (start >= results.Count)
                                break;      //All done.

                            int end = TextSearchTagger<T>.IndexOfContainingSpan(results, span.End, start, true);

                            while (start < end)
                            {
                                T tag = search.TagFactory.Invoke(results[start]);

                                if (tag != null)
                                {
                                    yield return new TagSpan<T>(results[start], tag);
                                }

                                start++;
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Search spans from [start ... spans.Count-1] for a span that contains point.
        /// If a span does contain point, return the index + 1
        /// If a span does not contain point, return the index of the first span that starts after point (or spans.Count if there are none).
        /// </summary>
        private static int IndexOfContainingSpan(NormalizedSpanCollection spans, int point, int start, bool isEndPoint)
        {
            int lo = start;
            int hi = spans.Count;
            while (lo < hi)
            {
                int mid = (lo + hi) / 2;
                Span s = spans[mid];

                if (s.End < point)
                {
                    lo = mid + 1;
                }
                else if (s.Start > point)
                {
                    hi = mid;
                }
                else
                {
                    //We know s.Start <= point <= s.End
                    //
                    //If point is an endPoint
                    //  we want to return mid + 1 if a span ending at point overlaps s (== point != s.Start). Otherwise return mid.
                    //
                    //If point is a startPoint
                    //  we want to return mid if a span starting at point overlaps s (== point == s.End). Otherwise return mid + 1.
                    if (isEndPoint)
                    {
                        return (point != s.Start) ? (mid + 1) : mid;
                    }
                    else
                    {
                        return (point == s.End) ? (mid + 1) : mid;
                    }
                }
            }

            return lo;
        }

        public event EventHandler<SnapshotSpanEventArgs> TagsChanged;

        #endregion
    }
}