-
Notifications
You must be signed in to change notification settings - Fork 4k
/
OptionKey.cs
97 lines (78 loc) · 2.84 KB
/
OptionKey.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
// 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.Diagnostics.CodeAnalysis;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Options
{
/// <inheritdoc cref="OptionKey2"/>
[NonDefaultable]
public readonly struct OptionKey : IEquatable<OptionKey>
{
/// <inheritdoc cref="OptionKey2.Option"/>
public IOption Option { get; }
/// <inheritdoc cref="OptionKey2.Language"/>
public string? Language { get; }
public OptionKey(IOption option, string? language = null)
{
if (option is null)
{
throw new ArgumentNullException(nameof(option));
}
if (language != null && !option.IsPerLanguage)
{
throw new ArgumentException(WorkspacesResources.A_language_name_cannot_be_specified_for_this_option);
}
if (language == null && option.IsPerLanguage)
{
throw new ArgumentNullException(WorkspacesResources.A_language_name_must_be_specified_for_this_option);
}
Option = option;
Language = language;
}
public override bool Equals(object? obj)
{
return obj is OptionKey key &&
Equals(key);
}
public bool Equals(OptionKey other)
{
return OptionEqual(Option, other.Option) && Language == other.Language;
static bool OptionEqual(IOption thisOption, IOption otherOption)
{
if (thisOption is not IOption2 thisOption2 ||
otherOption is not IOption2 otherOption2)
{
// Third party definition of 'IOption'.
return thisOption.Equals(otherOption);
}
return thisOption2.Equals(otherOption2);
}
}
public override int GetHashCode()
{
var hash = Option?.GetHashCode() ?? 0;
if (Language != null)
{
hash = unchecked((hash * (int)0xA5555529) + Language.GetHashCode());
}
return hash;
}
public override string ToString()
{
if (Option is null)
{
return "";
}
var languageDisplay = Option.IsPerLanguage
? $"({Language}) "
: string.Empty;
return languageDisplay + Option.ToString();
}
public static bool operator ==(OptionKey left, OptionKey right)
=> left.Equals(right);
public static bool operator !=(OptionKey left, OptionKey right)
=> !left.Equals(right);
}
}