-
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 18 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,8 @@ | |
|
|
||
| #pragma warning disable RS0030 // Do not used banned APIs | ||
|
|
||
| using System; | ||
| using System.Runtime.CompilerServices; | ||
| using Microsoft.CodeAnalysis.Formatting; | ||
| using Microsoft.CodeAnalysis.Options; | ||
|
|
||
|
|
@@ -133,7 +135,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; } = OptionUtilities.ConvertEnumOption<BinaryOperatorSpacingOptionsInternal, BinaryOperatorSpacingOptions, int>(CSharpFormattingOptions2.SpacingAroundBinaryOperator.ToPublicOption()); | ||
|
|
||
| /// <inheritdoc cref="CSharpFormattingOptions2.IndentBraces"/> | ||
| public static Option<bool> IndentBraces { get; } = CSharpFormattingOptions2.IndentBraces.ToPublicOption(); | ||
|
|
@@ -151,7 +153,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; } = OptionUtilities.ConvertEnumOption<LabelPositionOptionsInternal, LabelPositionOptions, int>(CSharpFormattingOptions2.LabelPositioning.ToPublicOption()); | ||
|
|
||
| /// <inheritdoc cref="CSharpFormattingOptions2.WrappingPreserveSingleLine"/> | ||
| public static Option<bool> WrappingPreserveSingleLine { get; } = CSharpFormattingOptions2.WrappingPreserveSingleLine.ToPublicOption(); | ||
|
|
@@ -187,3 +189,27 @@ public SpacePlacementInternalStorageMapping(IOption2 internalOption, SpacePlacem | |
| /// <inheritdoc cref="CSharpFormattingOptions2.NewLineForClausesInQuery"/> | ||
| public static Option<bool> NewLineForClausesInQuery { get; } = CSharpFormattingOptions2.NewLineForClausesInQuery.ToPublicOption(); | ||
| } | ||
|
|
||
| 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, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // 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. | ||
|
|
||
| #pragma warning disable RS0030 // Do not used banned APIs: Option<T> | ||
|
|
||
| using System; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.Options; | ||
|
|
||
| internal static class OptionUtilities | ||
| { | ||
| /// <summary> | ||
| /// Allows an option of one enum type to be converted to another enum type, provided that both enums share the same underlying type. | ||
| /// Useful for some cases in Roslyn where we have an existing shipped public option in the Workspace layer, and an internal option | ||
| /// in the CodeStyle layer, and we want to map between them. | ||
| /// </summary> | ||
| public static Option<TToEnum> ConvertEnumOption<TFromEnum, TToEnum, TUnderlyingEnumType>(Option<TFromEnum> option) | ||
| where TFromEnum : struct, Enum | ||
| where TToEnum : struct, Enum | ||
| where TUnderlyingEnumType : struct | ||
| { | ||
| // Ensure that this is only called for enums that are actually compatible with each other. | ||
| Contract.ThrowIfTrue(typeof(TFromEnum).GetEnumUnderlyingType() != typeof(TUnderlyingEnumType)); | ||
| Contract.ThrowIfTrue(typeof(TToEnum).GetEnumUnderlyingType() != typeof(TUnderlyingEnumType)); | ||
|
|
||
| var definition = option.OptionDefinition; | ||
| var newDefaultValue = ConvertEnum<TFromEnum, TToEnum, TUnderlyingEnumType>(definition.DefaultValue); | ||
| var newSerializer = ConvertEnumSerializer<TFromEnum, TToEnum, TUnderlyingEnumType>(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>? ConvertEnumSerializer<TFromEnum, TToEnum, TUnderlyingEnumType>(EditorConfigValueSerializer<TFromEnum> serializer) | ||
CyrusNajmabadi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| where TFromEnum : struct | ||
| where TToEnum : struct | ||
| where TUnderlyingEnumType : struct | ||
| { | ||
| return new( | ||
| value => ConvertEnum<TFromEnum, TToEnum, TUnderlyingEnumType>(serializer.ParseValue(value)), | ||
| value => serializer.SerializeValue(ConvertEnum<TToEnum, TFromEnum, TUnderlyingEnumType>(value))); | ||
| } | ||
|
|
||
| private static Optional<TToEnum> ConvertEnum<TFromEnum, TToEnum, TUnderlyingEnumType>(Optional<TFromEnum> optional) | ||
| where TFromEnum : struct | ||
| where TToEnum : struct | ||
| where TUnderlyingEnumType : struct | ||
| { | ||
| if (!optional.HasValue) | ||
| return default; | ||
|
|
||
| return ConvertEnum<TFromEnum, TToEnum, TUnderlyingEnumType>(optional.Value); | ||
| } | ||
|
|
||
| private static TToEnum ConvertEnum<TFromEnum, TToEnum, TUnderlyingEnumType>(TFromEnum value) | ||
| where TFromEnum : struct | ||
| where TToEnum : struct | ||
| where TUnderlyingEnumType : struct | ||
| { | ||
| return Unsafe.As<TUnderlyingEnumType, TToEnum>(ref Unsafe.As<TFromEnum, TUnderlyingEnumType>(ref value)); | ||
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.