/
OptionSet.cs
129 lines (106 loc) · 5.72 KB
/
OptionSet.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Microsoft.CodeAnalysis.Options
{
public abstract partial class OptionSet
{
private const string NoLanguageSentinel = "\0";
private static readonly ImmutableDictionary<string, AnalyzerConfigOptions> s_emptyAnalyzerConfigOptions =
ImmutableDictionary.Create<string, AnalyzerConfigOptions>(StringComparer.Ordinal);
/// <summary>
/// Map from language name to the <see cref="AnalyzerConfigOptions"/> wrapper.
/// </summary>
private ImmutableDictionary<string, AnalyzerConfigOptions> _lazyAnalyzerConfigOptions = s_emptyAnalyzerConfigOptions;
private readonly Func<OptionKey, object?> _getOptionCore;
protected OptionSet()
{
_getOptionCore = GetOptionCore;
}
private protected abstract object? GetOptionCore(OptionKey optionKey);
/// <summary>
/// Gets the value of the option, or the default value if not otherwise set.
/// </summary>
public object? GetOption(OptionKey optionKey)
=> OptionsHelpers.GetPublicOption(optionKey, _getOptionCore);
/// <summary>
/// Gets the value of the option cast to type <typeparamref name="T"/>, or the default value if not otherwise set.
/// </summary>
public T GetOption<T>(OptionKey optionKey)
=> OptionsHelpers.GetOption<T>(optionKey, _getOptionCore);
/// <summary>
/// Gets the value of the option, or the default value if not otherwise set.
/// </summary>
internal object? GetOption(OptionKey2 optionKey)
=> OptionsHelpers.GetOption<object?>(optionKey, _getOptionCore);
/// <summary>
/// Gets the value of the option cast to type <typeparamref name="T"/>, or the default value if not otherwise set.
/// </summary>
internal T GetOption<T>(OptionKey2 optionKey)
=> OptionsHelpers.GetOption<T>(optionKey, _getOptionCore);
/// <summary>
/// Gets the value of the option, or the default value if not otherwise set.
/// </summary>
public T GetOption<T>(Option<T> option)
=> OptionsHelpers.GetOption(option, _getOptionCore);
/// <summary>
/// Gets the value of the option, or the default value if not otherwise set.
/// </summary>
internal T GetOption<T>(Option2<T> option)
=> OptionsHelpers.GetOption(option, _getOptionCore);
/// <summary>
/// Gets the value of the option, or the default value if not otherwise set.
/// </summary>
public T GetOption<T>(PerLanguageOption<T> option, string? language)
=> OptionsHelpers.GetOption(option, language, _getOptionCore);
/// <summary>
/// Gets the value of the option, or the default value if not otherwise set.
/// </summary>
internal T GetOption<T>(PerLanguageOption2<T> option, string? language)
=> OptionsHelpers.GetOption(option, language, _getOptionCore);
/// <summary>
/// Creates a new <see cref="OptionSet" /> that contains the changed value.
/// </summary>
public abstract OptionSet WithChangedOption(OptionKey optionAndLanguage, object? value);
/// <summary>
/// Creates a new <see cref="OptionSet" /> that contains the changed value.
/// </summary>
internal OptionSet WithChangedOption(OptionKey2 optionAndLanguage, object? value)
=> WithChangedOption((OptionKey)optionAndLanguage, value);
/// <summary>
/// Creates a new <see cref="OptionSet" /> that contains the changed value.
/// </summary>
public OptionSet WithChangedOption<T>(Option<T> option, T value)
=> WithChangedOption(new OptionKey(option), value);
/// <summary>
/// Creates a new <see cref="OptionSet" /> that contains the changed value.
/// </summary>
internal OptionSet WithChangedOption<T>(Option2<T> option, T value)
=> WithChangedOption(new OptionKey(option), value);
/// <summary>
/// Creates a new <see cref="OptionSet" /> that contains the changed value.
/// </summary>
public OptionSet WithChangedOption<T>(PerLanguageOption<T> option, string? language, T value)
=> WithChangedOption(new OptionKey(option, language), value);
/// <summary>
/// Creates a new <see cref="OptionSet" /> that contains the changed value.
/// </summary>
internal OptionSet WithChangedOption<T>(PerLanguageOption2<T> option, string? language, T value)
=> WithChangedOption(new OptionKey(option, language), value);
internal AnalyzerConfigOptions AsAnalyzerConfigOptions(IOptionService optionService, string? language)
{
return ImmutableInterlocked.GetOrAdd(
ref _lazyAnalyzerConfigOptions,
language ?? NoLanguageSentinel,
(string language, (OptionSet self, IOptionService optionService) arg) => arg.self.CreateAnalyzerConfigOptions(arg.optionService, (object)language == NoLanguageSentinel ? null : language),
(this, optionService));
}
internal abstract IEnumerable<OptionKey> GetChangedOptions(OptionSet optionSet);
private protected virtual AnalyzerConfigOptions CreateAnalyzerConfigOptions(IOptionService optionService, string? language)
=> new AnalyzerConfigOptionsImpl(this, optionService, language);
}
}