PerLanguageOption.cs 3.3 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3

using System;
4 5
using System.Collections.Generic;
using System.Collections.Immutable;
P
Pilchie 已提交
6 7 8 9 10 11 12

namespace Microsoft.CodeAnalysis.Options
{
    /// <summary>
    /// An option that can be specified once per language.
    /// </summary>
    /// <typeparam name="T"></typeparam>
13
    public class PerLanguageOption<T> : IOption2, IOption
P
Pilchie 已提交
14
    {
15 16 17 18 19
        /// <summary>
        /// Save per language defaults
        /// </summary>
        private readonly IImmutableDictionary<string, T> _perLanguageDefaults;

P
Pilchie 已提交
20 21 22
        /// <summary>
        /// Feature this option is associated with.
        /// </summary>
23
        public string Feature { get; }
P
Pilchie 已提交
24 25 26 27

        /// <summary>
        /// The name of the option.
        /// </summary>
28
        public string Name { get; }
P
Pilchie 已提交
29 30 31 32 33 34 35 36 37 38 39 40

        /// <summary>
        /// The type of the option value.
        /// </summary>
        public Type Type
        {
            get { return typeof(T); }
        }

        /// <summary>
        /// The default option value.
        /// </summary>
41
        public T DefaultValue { get; }
P
Pilchie 已提交
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
        /// <summary>
        /// The default option value of specific language
        /// </summary>
        internal T GetDefaultValue(string language)
        {
            if (_perLanguageDefaults.Count == 0)
            {
                return DefaultValue;
            }

            T languageSpecificDefault;
            if (!_perLanguageDefaults.TryGetValue(language, out languageSpecificDefault))
            {
                return DefaultValue;
            }

            return languageSpecificDefault;
        }

        public PerLanguageOption(string feature, string name, T defaultValue) :
            this(feature, name, defaultValue, ImmutableDictionary<string, T>.Empty)
        {
        }

        internal PerLanguageOption(
            string feature, string name, T defaultValue, IDictionary<string, T> perLanguageDefaults)
P
Pilchie 已提交
69 70 71
        {
            if (string.IsNullOrWhiteSpace(feature))
            {
72
                throw new ArgumentNullException(nameof(feature));
P
Pilchie 已提交
73 74 75 76
            }

            if (string.IsNullOrWhiteSpace(name))
            {
77
                throw new ArgumentException(nameof(name));
P
Pilchie 已提交
78 79
            }

80 81 82 83 84
            if (perLanguageDefaults == null)
            {
                throw new ArgumentNullException(nameof(perLanguageDefaults));
            }

P
Pilchie 已提交
85 86 87
            this.Feature = feature;
            this.Name = name;
            this.DefaultValue = defaultValue;
88 89 90 91

            this._perLanguageDefaults =
                (perLanguageDefaults as IImmutableDictionary<string, T>) ??
                        ImmutableDictionary.CreateRange<string, T>(perLanguageDefaults);
P
Pilchie 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        }

        Type IOption.Type
        {
            get { return typeof(T); }
        }

        object IOption.DefaultValue
        {
            get { return this.DefaultValue; }
        }

        bool IOption.IsPerLanguage
        {
            get { return true; }
        }

109 110 111 112 113
        object IOption2.GetDefaultValue(string language)
        {
            return this.GetDefaultValue(language);
        }

P
Pilchie 已提交
114 115 116 117 118
        public override string ToString()
        {
            return string.Format("{0} - {1}", this.Feature, this.Name);
        }
    }
119
}