Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make NotificationOption2 a serializable struct #60573

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal CodeStyleOption(CodeStyleOption2<T> codeStyleOptionImpl)
=> _codeStyleOptionImpl = codeStyleOptionImpl;

public CodeStyleOption(T value, NotificationOption notification)
: this(new CodeStyleOption2<T>(value, (NotificationOption2)notification))
: this(new CodeStyleOption2<T>(value, new NotificationOption2(notification.Severity)))
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed partial class CodeStyleOption2<T>
return null;
}

return new CodeStyleOption<T>(option.Value, (NotificationOption?)option.Notification);
return new CodeStyleOption<T>(option.Value, (NotificationOption)option.Notification);
}

[return: NotNullIfNotNull("option")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,14 @@
// 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.Diagnostics.CodeAnalysis;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CodeStyle
{
internal sealed partial class NotificationOption2
internal readonly partial record struct NotificationOption2
{
[return: NotNullIfNotNull("notificationOption")]
public static explicit operator NotificationOption2?(NotificationOption? notificationOption)
{
if (notificationOption is null)
{
return null;
}

return notificationOption.Severity switch
{
ReportDiagnostic.Suppress => None,
ReportDiagnostic.Hidden => Silent,
ReportDiagnostic.Info => Suggestion,
ReportDiagnostic.Warn => Warning,
ReportDiagnostic.Error => Error,
_ => throw ExceptionUtilities.UnexpectedValue(notificationOption.Severity),
};
}

[return: NotNullIfNotNull("notificationOption")]
public static explicit operator NotificationOption?(NotificationOption2? notificationOption)
{
if (notificationOption is null)
{
return null;
}

return notificationOption.Severity switch
public static explicit operator NotificationOption(NotificationOption2 notificationOption)
=> notificationOption.Severity switch
{
ReportDiagnostic.Suppress => NotificationOption.None,
ReportDiagnostic.Hidden => NotificationOption.Silent,
Expand All @@ -45,6 +18,5 @@ internal sealed partial class NotificationOption2
ReportDiagnostic.Error => NotificationOption.Error,
_ => throw ExceptionUtilities.UnexpectedValue(notificationOption.Severity),
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static bool TryGetCodeStyleValue(
/// a NotificationOption, so <paramref name="notification"/> will default to <paramref name="defaultNotification"/>.
/// </summary>
public static bool TryGetCodeStyleValueAndOptionalNotification(
string arg, NotificationOption2 defaultNotification, [NotNullWhen(true)] out string? value, [NotNullWhen(true)] out NotificationOption2? notification)
string arg, NotificationOption2 defaultNotification, [NotNullWhen(true)] out string? value, [NotNullWhen(true)] out NotificationOption2 notification)
{
var args = arg.Split(':');
Debug.Assert(args.Length > 0);
Expand All @@ -86,7 +86,7 @@ public static bool TryGetCodeStyleValueAndOptionalNotification(

// We only support 0 or 1 args. Anything else can't be parsed properly.
value = null;
notification = null;
notification = default;
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static CodeStyleOption2()
public CodeStyleOption2(T value, NotificationOption2 notification)
{
Value = value;
_notification = notification ?? throw new ArgumentNullException(nameof(notification));
_notification = notification;
}

public T Value { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,6 @@ private static CodeStyleOption2<AccessibilityModifiersRequired> ParseAccessibili

private static string GetAccessibilityModifiersRequiredEditorConfigString(CodeStyleOption2<AccessibilityModifiersRequired> option, CodeStyleOption2<AccessibilityModifiersRequired> defaultValue)
{
// If they provide 'never', they don't need a notification level.
if (option.Notification == null)
{
Debug.Assert(s_accessibilityModifiersRequiredMap.ContainsValue(AccessibilityModifiersRequired.Never));
return s_accessibilityModifiersRequiredMap.GetKeyOrDefault(AccessibilityModifiersRequired.Never)!;
}

Debug.Assert(s_accessibilityModifiersRequiredMap.ContainsValue(option.Value));
return $"{s_accessibilityModifiersRequiredMap.GetKeyOrDefault(option.Value)}{GetEditorConfigStringNotificationPart(option, defaultValue)}";
}
Expand Down Expand Up @@ -425,7 +418,7 @@ private static string GetParenthesesPreferenceEditorConfigString(CodeStyleOption
{
Debug.Assert(s_parenthesesPreferenceMap.ContainsValue(option.Value));
var value = s_parenthesesPreferenceMap.GetKeyOrDefault(option.Value) ?? s_parenthesesPreferenceMap.GetKeyOrDefault(ParenthesesPreference.AlwaysForClarity);
return option.Notification == null ? value! : $"{value}{GetEditorConfigStringNotificationPart(option, defaultValue)}";
return $"{value}{GetEditorConfigStringNotificationPart(option, defaultValue)}";
}

private static CodeStyleOption2<UnusedParametersPreference> ParseUnusedParametersPreference(string optionString, CodeStyleOption2<UnusedParametersPreference> defaultValue)
Expand All @@ -443,7 +436,7 @@ private static string GetUnusedParametersPreferenceEditorConfigString(CodeStyleO
{
Debug.Assert(s_unusedParametersPreferenceMap.ContainsValue(option.Value));
var value = s_unusedParametersPreferenceMap.GetKeyOrDefault(option.Value) ?? s_unusedParametersPreferenceMap.GetKeyOrDefault(defaultValue.Value);
return option.Notification == null ? value! : $"{value}{GetEditorConfigStringNotificationPart(option, defaultValue)}";
return $"{value}{GetEditorConfigStringNotificationPart(option, defaultValue)}";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,42 @@
// 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;

#if CODE_STYLE
using WorkspacesResources = Microsoft.CodeAnalysis.CodeStyleResources;
#endif
using System.Runtime.Serialization;

namespace Microsoft.CodeAnalysis.CodeStyle
{
/// <summary>
/// Offers different notification styles for enforcing
/// a code style. Under the hood, it simply maps to <see cref="DiagnosticSeverity"/>
/// </summary>
/// <remarks>
/// This also supports various properties for databinding.
/// </remarks>
/// <completionlist cref="NotificationOption2"/>
internal sealed partial class NotificationOption2 : IEquatable<NotificationOption2?>
[DataContract]
internal readonly partial record struct NotificationOption2(
[property: DataMember(Order = 0)] ReportDiagnostic Severity)
{
/// <summary>
/// Notification option to disable or suppress an option with <see cref="ReportDiagnostic.Suppress"/>.
/// </summary>
public static readonly NotificationOption2 None = new(ReportDiagnostic.Suppress);
public static NotificationOption2 None => new(ReportDiagnostic.Suppress);

/// <summary>
/// Notification option for a silent or hidden option with <see cref="ReportDiagnostic.Hidden"/>.
/// </summary>
public static readonly NotificationOption2 Silent = new(ReportDiagnostic.Hidden);
public static NotificationOption2 Silent => new(ReportDiagnostic.Hidden);

/// <summary>
/// Notification option for a suggestion or an info option with <see cref="ReportDiagnostic.Info"/>.
/// </summary>
public static readonly NotificationOption2 Suggestion = new(ReportDiagnostic.Info);
public static NotificationOption2 Suggestion => new(ReportDiagnostic.Info);

/// <summary>
/// Notification option for a warning option with <see cref="ReportDiagnostic.Warn"/>.
/// </summary>
public static readonly NotificationOption2 Warning = new(ReportDiagnostic.Warn);
public static NotificationOption2 Warning => new(ReportDiagnostic.Warn);

/// <summary>
/// Notification option for an error option with <see cref="ReportDiagnostic.Error"/>.
/// </summary>
public static readonly NotificationOption2 Error = new(ReportDiagnostic.Error);

/// <summary>
/// Diagnostic severity associated with notification option.
/// </summary>
public ReportDiagnostic Severity { get; }

private NotificationOption2(ReportDiagnostic severity)
{
Severity = severity;
}

public override bool Equals(object? obj)
=> obj is NotificationOption2 other && Equals(other);

public bool Equals(NotificationOption2? other)
=> other != null && Severity == other.Severity;

public override int GetHashCode()
=> Severity.GetHashCode();
public static NotificationOption2 Error => new(ReportDiagnostic.Error);
}
}