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

EditorOptionDefinition.cs « EditorOptions « TextLogic « Def « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 78941224929f2a3e407e7c8ec0339afdeeb24230 (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
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  Licensed under the MIT License. See License.txt in the project root for license information.
//
using System;

using Microsoft.VisualStudio.Utilities;

namespace Microsoft.VisualStudio.Text.Editor
{
    /// <summary>
    /// The definition of an editor option.
    /// </summary>
    /// <remarks>
    /// This is a MEF component part, and should be exported with:
    /// [Export(typeof(EditorOptionDefinition))]
    /// </remarks>
    public abstract class EditorOptionDefinition
    {
        /// <summary>
        /// Gets the default value of the option.
        /// </summary>
        /// <remarks> The type of the value must be the same as the <see cref="ValueType"/>.</remarks>
        public abstract object DefaultValue { get; }

        /// <summary>
        /// Gets the actual type of the option. This is used to ensure
        /// that setting the option by using the editor options registry
        /// is type-safe.
        /// </summary>
        public abstract Type ValueType { get; }

        /// <summary>
        /// Gets the name of the option from the options registry.
        /// </summary>
        public abstract string Name { get; }

        /// <summary>
        /// Determines whether this option is applicable for the given scope (for example, a text buffer).
        /// The default implementation returns <c>true</c>. An option, by default, is applicable to any scope.
        /// </summary>
        /// <remarks>This method will not be called for the global scope. Every option is
        /// valid by definition in the global scope.</remarks>
        public virtual bool IsApplicableToScope(IPropertyOwner scope)
        {
            return true;
        }

        /// <summary>
        /// Determines whether the proposed value is valid.
        /// </summary>
        /// <param name="proposedValue">The proposed value for this option.</param>
        /// <returns><c>true</c> if the value is valid, otherwise <c>false</c>.</returns>
        /// <remarks>By the time the value is passed to this method, it has already
        /// been checked to be of the correct ValueType.
        /// The implementer of this method may modify the value.</remarks>
        public virtual bool IsValid(ref object proposedValue)
        {
            return true;
        }

        #region Object overrides

        /// <summary>
        /// Determines whether two <see cref="EditorOptionDefinition"/> objects are the same.
        /// </summary>
        /// <param name="obj">The object to be compared.</param>
        /// <returns><c>true</c> if the two objects are the same, otherwise <c>false</c>.</returns>
        public override bool Equals(object obj)
        {
            EditorOptionDefinition other = obj as EditorOptionDefinition;
            return other != null && other.Name == this.Name;
        }

        /// <summary>
        /// Gets the hash code of this type.
        /// </summary>
        /// <returns>The hash code.</returns>
        public override int GetHashCode()
        {
            return this.Name.GetHashCode();
        }

        #endregion
    }

    /// <summary>
    /// Represents the definition of an editor option.
    /// </summary>
    public abstract class EditorOptionDefinition<T> : EditorOptionDefinition
    {
        /// <summary>
        /// Gets the actual type of the option.
        /// </summary>
        public sealed override Type ValueType { get { return typeof(T); } }

        /// <summary>
        /// Gets the name of the option.
        /// </summary>
        public sealed override string Name { get { return Key.Name; } }

        /// <summary>
        /// Gets the default value of the option.
        /// </summary>
        public sealed override object DefaultValue { get { return Default; } }

        /// <summary>Determines whether the proposed value is valid.
        /// </summary>
        /// <param name="proposedValue">The proposed value for this option.</param>
        /// <returns><c>true</c> if the value is valid, otherwise <c>false</c>.</returns>
        /// <remarks>By the time the value is passed to this method, it has already
        /// been checked to be of the correct ValueType.
        /// The implementer of this method may modify the value.</remarks>
        public sealed override bool IsValid(ref object proposedValue)
        {
            if (proposedValue is T)
            {
                T value = (T)proposedValue;

                var result = IsValid(ref value);
                proposedValue = value;

                return result;
            }

            return false;
        }

        /// <summary>
        /// Gets the key of this option.
        /// </summary>
        public abstract EditorOptionKey<T> Key { get; }

        /// <summary>
        /// Gets the default value of this option.
        /// </summary>
        public virtual T Default { get { return default(T); } }

        /// <summary>
        /// Determines whether the proposed value is valid.
        /// </summary>
        /// <param name="proposedValue">The proposed value for this option.</param>
        /// <returns><c>true</c> if the value is valid, otherwise <c>false</c>.</returns>
        /// <remarks>The implementer of this method may modify the value.</remarks>
        public virtual bool IsValid(ref T proposedValue) { return true; }
    }

}