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

TagAggregatorFactoryService.cs « TagAggregator « Impl « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d4a62b322d15f1d2ed990ca0f89367246207a9ce (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
//
//  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.Tagging.Implementation
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Immutable;
    using System.ComponentModel.Composition;
    using System.Linq;

    using Microsoft.VisualStudio.Utilities;
    using Microsoft.VisualStudio.Text.Editor;
    using Microsoft.VisualStudio.Text.Projection;
    using Microsoft.VisualStudio.Text.Utilities;
    using Microsoft.VisualStudio.Threading;

    /// <summary>
    /// Exports the TagAggregator provider, both the buffer and view version.
    /// </summary>
    [Export(typeof(IBufferTagAggregatorFactoryService))]
    [Export(typeof(IViewTagAggregatorFactoryService))]
    internal sealed class TagAggregatorFactoryService : IBufferTagAggregatorFactoryService, IViewTagAggregatorFactoryService
    {
        [ImportMany(typeof(ITaggerProvider))]
        internal List<Lazy<ITaggerProvider, INamedTaggerMetadata>> BufferTaggerProviders { get; set; }

        [ImportMany(typeof(IViewTaggerProvider))]
        internal List<Lazy<IViewTaggerProvider, IViewTaggerMetadata>> ViewTaggerProviders { get; set; }

        [Import]
        internal IBufferGraphFactoryService BufferGraphFactoryService { get; set; }

        [Import]
        internal IContentTypeRegistryService ContentTypeRegistryService { get; set; }

        [Import]
        internal JoinableTaskContext JoinableTaskContext { get; set; }

        [Import]
        internal GuardedOperations GuardedOperations { get; set; }

        internal ImmutableDictionary<ContentAndTypeData, IEnumerable<Lazy<ITaggerProvider, INamedTaggerMetadata>>> _bufferTaggerProviderMap = ImmutableDictionary<ContentAndTypeData, IEnumerable<Lazy<ITaggerProvider, INamedTaggerMetadata>>>.Empty;
        internal ImmutableDictionary<ContentAndTypeData, IEnumerable<Lazy<IViewTaggerProvider, IViewTaggerMetadata>>> _viewTaggerProviderMap = ImmutableDictionary<ContentAndTypeData, IEnumerable<Lazy<IViewTaggerProvider, IViewTaggerMetadata>>>.Empty;

        #region IBufferTagAggregatorFactoryService Members

        public ITagAggregator<T> CreateTagAggregator<T>(ITextBuffer textBuffer) where T : ITag
        {
            return CreateTagAggregator<T>(textBuffer, TagAggregatorOptions.None);
        }

        public ITagAggregator<T> CreateTagAggregator<T>(ITextBuffer textBuffer, TagAggregatorOptions options) where T : ITag
        {
            if (textBuffer == null)
                throw new ArgumentNullException(nameof(textBuffer));

            return new TagAggregator<T>(this, null, this.BufferGraphFactoryService.CreateBufferGraph(textBuffer), options);

        }

        #endregion

        #region IViewTagAggregatorFactoryService Members

        public ITagAggregator<T> CreateTagAggregator<T>(ITextView textView) where T : ITag
        {
            return CreateTagAggregator<T>(textView, TagAggregatorOptions.None);
        }

        public ITagAggregator<T> CreateTagAggregator<T>(ITextView textView, TagAggregatorOptions options) where T : ITag
        {
            if (textView == null)
                throw new ArgumentNullException(nameof(textView));

            return new TagAggregator<T>(this, textView, textView.BufferGraph, options);
        }

        #endregion

        internal IEnumerable<Lazy<ITaggerProvider, INamedTaggerMetadata>> GetBufferTaggersForType(IContentType type, Type taggerType)
        {
            var key = new ContentAndTypeData(type, taggerType);

            IEnumerable<Lazy<ITaggerProvider, INamedTaggerMetadata>> taggers;
            if (!_bufferTaggerProviderMap.TryGetValue(key, out taggers))
            {
                taggers = new List<Lazy<ITaggerProvider, INamedTaggerMetadata>>(this.BufferTaggerProviders.Where(f => Match(type, taggerType, f.Metadata)));

                ImmutableInterlocked.Update(ref _bufferTaggerProviderMap, (s) => s.Add(key, taggers));
            }

            return taggers;
        }

        internal IEnumerable<Lazy<IViewTaggerProvider, IViewTaggerMetadata>> GetViewTaggersForType(IContentType type, Type taggerType)
        {
            var key = new ContentAndTypeData(type, taggerType);

            IEnumerable<Lazy<IViewTaggerProvider, IViewTaggerMetadata>> taggers;
            if (!_viewTaggerProviderMap.TryGetValue(key, out taggers))
            {
                taggers = new List<Lazy<IViewTaggerProvider, IViewTaggerMetadata>>(this.ViewTaggerProviders.Where(f => Match(type, taggerType, f.Metadata)));

                ImmutableInterlocked.Update(ref _viewTaggerProviderMap, (s) => s.Add(key, taggers));
            }

            return taggers;
        }

        private static bool Match(IContentType bufferContentType, Type taggerType, INamedTaggerMetadata tagMetadata)
        {
            bool contentTypeMatch = false;

            foreach (string contentType in tagMetadata.ContentTypes)
            {
                if (bufferContentType.IsOfType(contentType))
                {
                    contentTypeMatch = true;
                    break;
                }
            }

            if (contentTypeMatch)
            {
                // Now find out if it can provide tags of the type we want
                foreach (Type type in tagMetadata.TagTypes)
                {
                    // This producer is used if it claims to produce a tag
                    // that this type is assignable from.
                    if (taggerType.IsAssignableFrom(type))
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        internal class ContentAndTypeData
        {
            public readonly IContentType ContentType;
            public readonly Type TaggerType;

            public ContentAndTypeData(IContentType contentType, Type taggerType)
            {
                this.ContentType = contentType;
                this.TaggerType = taggerType;
            }

            public override bool Equals(object obj)
            {
                var other = obj as ContentAndTypeData;
                return (other != null) && (other.ContentType == this.ContentType) && (other.TaggerType == this.TaggerType);
            }

            public override int GetHashCode()
            {
                return this.ContentType.GetHashCode() ^ this.TaggerType.GetHashCode();
            }
        }
    }
}