-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fix layering of C# formatting options #79083
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
Changes from 5 commits
b82d180
e62026e
6e54804
5aeae54
f826c99
809bf21
91950fc
f60ba09
2bb20b2
6083288
c8ec941
3477b93
d3bec4f
69d90ed
da4763a
5f79ae3
80330bc
f2ae58f
5a82752
f87109b
0ffb308
996803a
a1b4dce
1cf12da
b975ccb
f104982
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,9 @@ | |
|
|
||
| #pragma warning disable RS0030 // Do not used banned APIs | ||
|
|
||
| using System; | ||
| using System.Runtime.CompilerServices; | ||
| using Humanizer; | ||
| using Microsoft.CodeAnalysis.Formatting; | ||
| using Microsoft.CodeAnalysis.Options; | ||
|
|
||
|
|
@@ -133,7 +136,7 @@ public SpacePlacementInternalStorageMapping(IOption2 internalOption, SpacePlacem | |
| public static Option<bool> SpaceBeforeSemicolonsInForStatement { get; } = CSharpFormattingOptions2.SpaceBeforeSemicolonsInForStatement.ToPublicOption(); | ||
|
|
||
| /// <inheritdoc cref="CSharpFormattingOptions2.SpacingAroundBinaryOperator"/> | ||
| public static Option<BinaryOperatorSpacingOptions> SpacingAroundBinaryOperator { get; } = CSharpFormattingOptions2.SpacingAroundBinaryOperator.ToPublicOption(); | ||
| public static Option<BinaryOperatorSpacingOptions> SpacingAroundBinaryOperator { get; } = ConvertEnumOption<BinaryOperatorSpacingOptionsInternal, BinaryOperatorSpacingOptions>(CSharpFormattingOptions2.SpacingAroundBinaryOperator.ToPublicOption()); | ||
|
|
||
| /// <inheritdoc cref="CSharpFormattingOptions2.IndentBraces"/> | ||
| public static Option<bool> IndentBraces { get; } = CSharpFormattingOptions2.IndentBraces.ToPublicOption(); | ||
|
|
@@ -151,7 +154,7 @@ public SpacePlacementInternalStorageMapping(IOption2 internalOption, SpacePlacem | |
| public static Option<bool> IndentSwitchCaseSectionWhenBlock { get; } = CSharpFormattingOptions2.IndentSwitchCaseSectionWhenBlock.ToPublicOption(); | ||
|
|
||
| /// <inheritdoc cref="CSharpFormattingOptions2.LabelPositioning"/> | ||
| public static Option<LabelPositionOptions> LabelPositioning { get; } = CSharpFormattingOptions2.LabelPositioning.ToPublicOption(); | ||
| public static Option<LabelPositionOptions> LabelPositioning { get; } = ConvertEnumOption<LabelPositionOptionsInternal, LabelPositionOptions>(CSharpFormattingOptions2.LabelPositioning.ToPublicOption()); | ||
|
|
||
| /// <inheritdoc cref="CSharpFormattingOptions2.WrappingPreserveSingleLine"/> | ||
| public static Option<bool> WrappingPreserveSingleLine { get; } = CSharpFormattingOptions2.WrappingPreserveSingleLine.ToPublicOption(); | ||
|
|
@@ -186,4 +189,73 @@ public SpacePlacementInternalStorageMapping(IOption2 internalOption, SpacePlacem | |
|
|
||
| /// <inheritdoc cref="CSharpFormattingOptions2.NewLineForClausesInQuery"/> | ||
| public static Option<bool> NewLineForClausesInQuery { get; } = CSharpFormattingOptions2.NewLineForClausesInQuery.ToPublicOption(); | ||
|
|
||
| private static Option<TToEnum> ConvertEnumOption<TFromEnum, TToEnum>(Option<TFromEnum> option) | ||
| where TFromEnum : struct, Enum | ||
| where TToEnum : struct, Enum | ||
| { | ||
| // Ensure that this is only called for enums that are actually compatible with each other. | ||
| Contract.ThrowIfTrue(typeof(TFromEnum).GetEnumUnderlyingType() != typeof(int)); | ||
|
||
| Contract.ThrowIfTrue(typeof(TToEnum).GetEnumUnderlyingType() != typeof(int)); | ||
|
|
||
| var definition = (OptionDefinition<TFromEnum>)((IOption2)option).Definition; | ||
| var newDefaultValue = Convert<TFromEnum, TToEnum>(Unsafe.Unbox<TFromEnum>(definition.DefaultValue)); | ||
| var newSerializer = ConvertSerializer<TFromEnum, TToEnum>(definition.Serializer); | ||
|
|
||
| var newDefinition = new OptionDefinition<TToEnum>( | ||
| defaultValue: newDefaultValue, newSerializer, definition.Group, definition.ConfigName, definition.StorageMapping, definition.IsEditorConfigOption); | ||
|
|
||
| return new(newDefinition, option.Feature, option.Name, option.StorageLocations); | ||
| } | ||
|
|
||
| private static EditorConfigValueSerializer<TToEnum>? ConvertSerializer<TFromEnum, TToEnum>(EditorConfigValueSerializer<TFromEnum> serializer) | ||
| where TFromEnum : struct | ||
| where TToEnum : struct | ||
| { | ||
| return new( | ||
| value => Convert<TFromEnum, TToEnum>(serializer.ParseValue(value)), | ||
| value => serializer.SerializeValue(Convert<TToEnum, TFromEnum>(value))); | ||
| } | ||
|
|
||
| private static Optional<TToEnum> Convert<TFromEnum, TToEnum>(Optional<TFromEnum> optional) | ||
| where TFromEnum : struct | ||
| where TToEnum : struct | ||
| { | ||
| if (!optional.HasValue) | ||
| return default; | ||
|
|
||
| return Convert<TFromEnum, TToEnum>(optional.Value); | ||
| } | ||
|
|
||
| private static TToEnum Convert<TFromEnum, TToEnum>(TFromEnum value) | ||
| where TFromEnum : struct | ||
| where TToEnum : struct | ||
| { | ||
| var intValue = Unsafe.As<TFromEnum, int>(ref value); | ||
| return Unsafe.As<int, TToEnum>(ref intValue); | ||
| } | ||
| } | ||
|
|
||
| public enum LabelPositionOptions | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thse are not new apis. they just moved from the shared location her.e we don't want it in teh shared location as that adds public types to multiple assemblies, which is bad (esp. for code that then references both assemblies. |
||
| { | ||
| /// Placed in the Zeroth column of the text editor | ||
| LeftMost = LabelPositionOptionsInternal.LeftMost, | ||
|
|
||
| /// Placed at one less indent to the current context | ||
| OneLess = LabelPositionOptionsInternal.OneLess, | ||
|
|
||
| /// Placed at the same indent as the current context | ||
| NoIndent = LabelPositionOptionsInternal.NoIndent, | ||
| } | ||
|
|
||
| public enum BinaryOperatorSpacingOptions | ||
| { | ||
| /// Single Spacing | ||
| Single = BinaryOperatorSpacingOptionsInternal.Single, | ||
|
|
||
| /// Ignore Formatting | ||
| Ignore = BinaryOperatorSpacingOptionsInternal.Ignore, | ||
|
|
||
| /// Remove Spacing | ||
| Remove = BinaryOperatorSpacingOptionsInternal.Remove, | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.