Skip to content

Commit

Permalink
Serializability
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Mar 17, 2022
1 parent cfba8f0 commit 2146869
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Runtime.Serialization;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
Expand All @@ -11,10 +12,19 @@ namespace Microsoft.CodeAnalysis.CSharp.Simplification
{
internal sealed class CSharpSimplifierOptions : SimplifierOptions
{
[DataMember(Order = BaseMemberCount + 0)]
public readonly CodeStyleOption2<bool> VarForBuiltInTypes;

[DataMember(Order = BaseMemberCount + 1)]
public readonly CodeStyleOption2<bool> VarWhenTypeIsApparent;

[DataMember(Order = BaseMemberCount + 2)]
public readonly CodeStyleOption2<bool> VarElsewhere;

[DataMember(Order = BaseMemberCount + 3)]
public readonly CodeStyleOption2<bool> PreferSimpleDefaultExpression;

[DataMember(Order = BaseMemberCount + 4)]
public readonly CodeStyleOption2<PreferBracesPreference> PreferBraces;

public CSharpSimplifierOptions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml.Linq;
using Microsoft.CodeAnalysis.Diagnostics;
using Roslyn.Utilities;
Expand Down Expand Up @@ -37,6 +38,7 @@ internal interface ICodeStyleOption : IObjectWritable
/// hosts that expect the value to be a boolean. Specifically, if the enum value is 0 or 1
/// then those values will write back as false/true.
/// </summary>
[DataContract]
internal sealed partial class CodeStyleOption2<T> : ICodeStyleOption, IEquatable<CodeStyleOption2<T>?>
{
static CodeStyleOption2()
Expand All @@ -48,16 +50,18 @@ static CodeStyleOption2()

private const int SerializationVersion = 1;

private readonly NotificationOption2 _notification;
[DataMember(Order = 0)]
public T Value { get; }

[DataMember(Order = 1)]
public NotificationOption2 Notification { get; }

public CodeStyleOption2(T value, NotificationOption2 notification)
{
Value = value;
_notification = notification ?? throw new ArgumentNullException(nameof(notification));
Notification = notification ?? throw new ArgumentNullException(nameof(notification));
}

public T Value { get; }

object? ICodeStyleOption.Value => this.Value;
ICodeStyleOption ICodeStyleOption.WithValue(object value) => new CodeStyleOption2<T>((T)value, Notification);
ICodeStyleOption ICodeStyleOption.WithNotification(NotificationOption2 notification) => new CodeStyleOption2<T>(Value, notification);
Expand All @@ -72,11 +76,6 @@ ICodeStyleOption ICodeStyleOption.AsCodeStyleOption<TCodeStyleOption>()

private int EnumValueAsInt32 => (int)(object)Value!;

public NotificationOption2 Notification
{
get => _notification;
}

public XElement ToXElement() =>
new("CodeStyleOption", // Ensure that we use "CodeStyleOption" as the name for back compat.
new XAttribute(nameof(SerializationVersion), SerializationVersion),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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.Runtime.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeStyle;
Expand All @@ -11,15 +12,29 @@

namespace Microsoft.CodeAnalysis.Simplification
{
[DataContract]
internal abstract class SimplifierOptions
{
[DataMember(Order = 0)]
public readonly CodeStyleOption2<bool> QualifyFieldAccess;

[DataMember(Order = 1)]
public readonly CodeStyleOption2<bool> QualifyPropertyAccess;

[DataMember(Order = 2)]
public readonly CodeStyleOption2<bool> QualifyMethodAccess;

[DataMember(Order = 3)]
public readonly CodeStyleOption2<bool> QualifyEventAccess;

[DataMember(Order = 4)]
public readonly CodeStyleOption2<bool> PreferPredefinedTypeKeywordInMemberAccess;

[DataMember(Order = 5)]
public readonly CodeStyleOption2<bool> PreferPredefinedTypeKeywordInDeclaration;

protected const int BaseMemberCount = 6;

protected SimplifierOptions(
CodeStyleOption2<bool> qualifyFieldAccess,
CodeStyleOption2<bool> qualifyPropertyAccess,
Expand Down

0 comments on commit 2146869

Please sign in to comment.