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

EditorOptions.cs « EditorOptions « Impl « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cb974108a858237f8bef0c78660e41913de8b526 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//
//  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.EditorOptions.Implementation
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Globalization;
    using System.Linq;
    using Microsoft.VisualStudio.Text.Editor;
    using Microsoft.VisualStudio.Text.Utilities;
    using Microsoft.VisualStudio.Utilities;

    internal class EditorOptions : IEditorOptions
    {
        IPropertyOwner Scope { get; set; }

        HybridDictionary OptionsSetLocally { get; set; }

        private EditorOptionsFactoryService _factory;

        FrugalList<WeakReference> DerivedEditorOptions = new FrugalList<WeakReference>();

        internal EditorOptions(EditorOptions parent,
                               IPropertyOwner scope,
                               EditorOptionsFactoryService factory)
        {
            _parent = parent;
            _factory = factory;
            this.Scope = scope;

            this.OptionsSetLocally = new HybridDictionary();

            if (parent != null)
            {
                parent.AddDerivedOptions(this);
            }
        }

        #region IEditorOptions Members

        private EditorOptions _parent;
        public IEditorOptions Parent 
        { 
            get
            {
                return _parent;
            }
            set
            {
                if (_parent == value)
                    return;

                // _parent == null => this is the global options instance
                if (_parent == null)
                    throw new InvalidOperationException("Cannot change the Parent of the global options.");

                if (value == null)
                    throw new ArgumentNullException(nameof(value));

                if (value == this)
                    throw new ArgumentException("The Parent of this instance of IEditorOptions cannot be set to itself.");

                EditorOptions newParent = value as EditorOptions;

                if (newParent == null)
                    throw new ArgumentException("New parent must be an instance of IEditorOptions generated by the same factory as this instance.");

                var oldParent = _parent;

                _parent.RemovedDerivedOptions(this);
                _parent = newParent;
                _parent.AddDerivedOptions(this);

                this.CheckForCycle();

                // TODO: Should we be more specific?  Should there be a
                // version of OptionsChanged that says "everything has changed"?
                
                // Send out an event for each supported option that isn't already
                // set locally (since the update in parent won't change the
                // observed value).
                foreach (var definition in _factory.GetInstantiatedOptions(this.Scope))
                {
                    if (!this.OptionsSetLocally.Contains(definition.Name))
                    {
                        object oldValue = oldParent.GetOptionForChild(definition);
                        object newValue = _parent.GetOptionForChild(definition);

                        if (!object.Equals(oldValue, newValue))
                            RaiseChangedEvent(definition);
                    }
                }
            }
        }

        public T GetOptionValue<T>(string optionId)
        {
            var definition = _factory.GetOptionDefinitionOrThrow(optionId);

            if (!typeof(T).IsAssignableFrom(definition.ValueType))
                throw new InvalidOperationException("Invalid type requested for the given option.");

            object value = this.GetOptionValue(definition);
            return (T)value;
        }

        public T GetOptionValue<T>(EditorOptionKey<T> key)
        {
            return GetOptionValue<T>(key.Name);
        }

        public object GetOptionValue(string optionId)
        {
            return GetOptionValue(_factory.GetOptionDefinitionOrThrow(optionId));
        }

        private object GetOptionValue(EditorOptionDefinition definition)
        {
            object value;

            if (!TryGetOption(definition, out value))
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "The specified option is not valid in this scope: {0}", definition.Name));

            return value;
        }

        public void SetOptionValue(string optionId, object value)
        {
            EditorOptionDefinition definition = _factory.GetOptionDefinitionOrThrow(optionId);

            // Make sure the type of the provided value is correct
            if (!definition.ValueType.IsAssignableFrom(value.GetType()))
            {
                throw new ArgumentException("Specified option value is of an invalid type", nameof(value));
            }
            // Make sure the option is valid, also
            else if(!definition.IsValid(ref value))
            {
                throw new ArgumentException("The supplied value failed validation for the option.", nameof(value));
            }
            // Finally, set the option value locally
            else
            {
                object currentValue = this.GetOptionValue(definition);
                OptionsSetLocally[optionId] = value;

                if (!object.Equals(currentValue, value))
                {
                    RaiseChangedEvent(definition);
                }
            }
        }

        public void SetOptionValue<T>(EditorOptionKey<T> key, T value)
        {
            SetOptionValue(key.Name, value);
        }

        public bool IsOptionDefined(string optionId, bool localScopeOnly)
        {
            if (localScopeOnly && (_parent != null))    //All options with valid definitions are set for the root.
                return OptionsSetLocally.Contains(optionId);

            EditorOptionDefinition definition = _factory.GetOptionDefinition(optionId);
            if ((definition != null) &&
                (Scope == null || definition.IsApplicableToScope(Scope)))
            {
                return true;
            }

            return false;
        }

        public bool IsOptionDefined<T>(EditorOptionKey<T> key, bool localScopeOnly)
        {
            if (localScopeOnly && (_parent != null))    //All options with valid definitions are set for the root.
            {
                return OptionsSetLocally.Contains(key.Name);
            }

            EditorOptionDefinition definition = _factory.GetOptionDefinition(key.Name);
            if ((definition != null) &&
                (Scope == null || definition.IsApplicableToScope(Scope)) &&
                definition.ValueType.IsEquivalentTo(typeof(T)))
            {
                return true;
            }

            return false;
        }

        public bool ClearOptionValue(string optionId)
        {
            if (this.Parent == null)
            {
                // Can't clear options on the Global options
                return false;
            }

            if (OptionsSetLocally.Contains(optionId))
            {
                object currentValue = OptionsSetLocally[optionId];

                OptionsSetLocally.Remove(optionId);

                EditorOptionDefinition definition = _factory.GetOptionDefinitionOrThrow(optionId);
                object inheritedValue =  this.GetOptionValue(definition);

                // See what the inherited option value was.  If it isn't changing,
                // then we don't need to raise an event.
                if (!object.Equals(currentValue, inheritedValue))
                {
                    RaiseChangedEvent(definition);
                }

                return true;
            }

            return false;
        }

        public bool ClearOptionValue<T>(EditorOptionKey<T> key)
        {
            return ClearOptionValue(key.Name);
        }

        public IEnumerable<EditorOptionDefinition> SupportedOptions
        {
            get
            {
                return _factory.GetSupportedOptions(this.Scope);
            }
        }

        public IEditorOptions GlobalOptions
        {
            get 
            { 
                return _factory.GlobalOptions;
            }
        }

        public event EventHandler<EditorOptionChangedEventArgs> OptionChanged;

        #endregion

        #region Private Helpers

        //A hook so we can tell whether or not the options have been hooked. Used only by unit tests.
        private object OptionChangedValue { get { return this.OptionChanged; } }

        private void RaiseChangedEvent(EditorOptionDefinition definition)
        {
            // First, send out local events, but only if the change is valid in this scope
            if (Scope == null || definition.IsApplicableToScope(Scope))
            {
                var tempEvent = OptionChanged;
                if (tempEvent != null)
                    tempEvent(this, new EditorOptionChangedEventArgs(definition.Name));
            }

            // Get rid of the expired refs
            DerivedEditorOptions.RemoveAll(weakref => !weakref.IsAlive);

            // Now, notify a copy of the derived options (since an event might modify the DerivedEditorOptions).
            foreach (var weakRef in new FrugalList<WeakReference>(DerivedEditorOptions))
            {
                EditorOptions derived = weakRef.Target as EditorOptions;
                if (derived != null)
                    derived.OnParentOptionChanged(definition);
            }
        }

        private void CheckForCycle()
        {
            EditorOptions parent = _parent;
            HashSet<EditorOptions> visited = new HashSet<EditorOptions>();

            while (parent != null)
            {
                if (visited.Contains(parent))
                    throw new ArgumentException("Cycles are not allowed in the Parent chain.");

                visited.Add(parent);
                parent = parent._parent;
            }
        }

        #endregion

        #region Internal "event" handling

        internal void AddDerivedOptions(EditorOptions derived)
        {
            // Get rid of the expired refs
            DerivedEditorOptions.RemoveAll(weakref => !weakref.IsAlive);

            DerivedEditorOptions.Add(new WeakReference(derived));
        }

        internal void RemovedDerivedOptions(EditorOptions derived)
        {
            foreach (var weakRef in DerivedEditorOptions)
            {
                if (weakRef.Target == derived)
                {
                    DerivedEditorOptions.Remove(weakRef);
                    break;
                }
            }
        }

        internal void OnParentOptionChanged(EditorOptionDefinition definition)
        {
            // We only notify if the given option isn't already set locally, since it
            // would be overriden by a parent option changing.
            if (!this.OptionsSetLocally.Contains(definition.Name))
                RaiseChangedEvent(definition);
        }

        #endregion

        private bool TryGetOption(EditorOptionDefinition definition, out object value)
        {
            value = null;

            if (Scope != null && !definition.IsApplicableToScope(Scope))
                return false;

            value = this.GetOptionForChild(definition);
            return true;
        }

        /// <summary>
        /// Get the given option from this (or its ancestors).  The caller should have already checked to ensure
        /// the given option is valid in the scope being requested.
        /// </summary>
        /// <param name="definition">Definition of the option to find.</param>
        /// <returns>The option's current value.</returns>
        internal object GetOptionForChild(EditorOptionDefinition definition)
        {
            if (OptionsSetLocally.Contains(definition.Name))
            {
                return OptionsSetLocally[definition.Name];
            }

            if (_parent == null)
            {
                return definition.DefaultValue;
            }

            return _parent.GetOptionForChild(definition);
        }
    }   
}