提交 6d51512c 编写于 作者: S Sam Harwell

Simplify and share additional code

上级 754dee89
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.Formatting
......@@ -26,26 +21,7 @@ internal static bool DetermineIfSpaceOptionIsSet(string value, SpacingWithinPare
private static string GetSpacingWithParenthesesEditorConfigString(OptionSet optionSet)
{
var editorConfigStringBuilder = new List<string>();
foreach (var kvp in SpacingWithinParenthesisOptionsMap)
{
var value = optionSet.GetOption(kvp.Key);
if (value)
{
Debug.Assert(s_spacingWithinParenthesisOptionsEditorConfigMap.ContainsValue(kvp.Value));
editorConfigStringBuilder.Add(s_spacingWithinParenthesisOptionsEditorConfigMap.GetKeyOrDefault(kvp.Value));
}
}
if (editorConfigStringBuilder.Count == 0)
{
// No spacing within parenthesis option set.
return "false";
}
else
{
return string.Join(",", editorConfigStringBuilder.Order());
}
throw ExceptionUtilities.Unreachable;
}
internal static BinaryOperatorSpacingOptions ParseEditorConfigSpacingAroundBinaryOperator(string binaryOperatorSpacingValue)
......@@ -95,31 +71,7 @@ internal static bool DetermineIfNewLineOptionIsSet(string value, NewLineOption o
private static string GetNewLineOptionEditorConfigString(OptionSet optionSet)
{
var editorConfigStringBuilder = new List<string>(NewLineOptionsMap.Count);
foreach (var kvp in NewLineOptionsMap)
{
var value = optionSet.GetOption(kvp.Key);
if (value)
{
Debug.Assert(s_newLineOptionsEditorConfigMap.ContainsValue(kvp.Value));
editorConfigStringBuilder.Add(s_newLineOptionsEditorConfigMap.GetKeyOrDefault(kvp.Value));
}
}
if (editorConfigStringBuilder.Count == 0)
{
// No NewLine option set.
return "none";
}
else if (editorConfigStringBuilder.Count == s_newLineOptionsMapBuilder.Count)
{
// All NewLine options set.
return "all";
}
else
{
return string.Join(",", editorConfigStringBuilder.Order());
}
throw ExceptionUtilities.Unreachable;
}
internal static bool DetermineIfIgnoreSpacesAroundVariableDeclarationIsSet(string value)
......
......@@ -9,14 +9,6 @@ namespace Microsoft.CodeAnalysis.CSharp.Formatting
{
internal static partial class CSharpFormattingOptions
{
private static readonly ImmutableArray<IOption>.Builder s_allOptionsBuilder = ImmutableArray.CreateBuilder<IOption>();
// Maps to store mapping between special option kinds and the corresponding options.
private static readonly ImmutableDictionary<Option<bool>, SpacingWithinParenthesesOption>.Builder s_spacingWithinParenthesisOptionsMapBuilder
= ImmutableDictionary.CreateBuilder<Option<bool>, SpacingWithinParenthesesOption>();
private static readonly ImmutableDictionary<Option<bool>, NewLineOption>.Builder s_newLineOptionsMapBuilder =
ImmutableDictionary.CreateBuilder<Option<bool>, NewLineOption>();
// Maps to store mapping between special option kinds and the corresponding editor config string representations.
#region Editor Config maps
private static readonly BidirectionalMap<string, SpacingWithinParenthesesOption> s_spacingWithinParenthesisOptionsEditorConfigMap =
......@@ -63,20 +55,14 @@ internal static partial class CSharpFormattingOptions
});
#endregion
internal static ImmutableArray<IOption> AllOptions { get; }
private static ImmutableDictionary<Option<bool>, SpacingWithinParenthesesOption> SpacingWithinParenthesisOptionsMap { get; }
private static ImmutableDictionary<Option<bool>, NewLineOption> NewLineOptionsMap { get; }
private static Option<T> CreateOption<T>(string name, T defaultValue, params OptionStorageLocation[] storageLocations)
{
var option = new Option<T>(nameof(CSharpFormattingOptions), name, defaultValue, storageLocations);
s_allOptionsBuilder.Add(option);
return option;
return new Option<T>(nameof(CSharpFormattingOptions), name, defaultValue, storageLocations);
}
private static Option<bool> CreateSpaceWithinParenthesesOption(SpacingWithinParenthesesOption parenthesesOption, string name)
{
var option = CreateOption(
return CreateOption(
name,
defaultValue: false,
storageLocations: new OptionStorageLocation[] {
......@@ -85,16 +71,11 @@ private static Option<bool> CreateSpaceWithinParenthesesOption(SpacingWithinPare
s => DetermineIfSpaceOptionIsSet(s, parenthesesOption),
GetSpacingWithParenthesesEditorConfigString),
new RoamingProfileStorageLocation($"TextEditor.CSharp.Specific.{name}")});
Debug.Assert(s_spacingWithinParenthesisOptionsEditorConfigMap.ContainsValue(parenthesesOption));
s_spacingWithinParenthesisOptionsMapBuilder.Add(option, parenthesesOption);
return option;
}
private static Option<bool> CreateNewLineForBracesOption(NewLineOption newLineOption, string name)
{
var option = CreateOption(
return CreateOption(
name,
defaultValue: true,
storageLocations: new OptionStorageLocation[] {
......@@ -103,11 +84,6 @@ private static Option<bool> CreateNewLineForBracesOption(NewLineOption newLineOp
value => DetermineIfNewLineOptionIsSet(value, newLineOption),
GetNewLineOptionEditorConfigString),
new RoamingProfileStorageLocation($"TextEditor.CSharp.Specific.{name}")});
Debug.Assert(s_newLineOptionsEditorConfigMap.ContainsValue(newLineOption));
s_newLineOptionsMapBuilder.Add(option, newLineOption);
return option;
}
public static Option<bool> SpacingAfterMethodDeclarationName { get; } = CreateOption(
......@@ -399,15 +375,6 @@ private static Option<bool> CreateNewLineForBracesOption(NewLineOption newLineOp
storageLocations: new OptionStorageLocation[] {
EditorConfigStorageLocation.ForBoolOption("csharp_new_line_between_query_expression_clauses"),
new RoamingProfileStorageLocation("TextEditor.CSharp.Specific.NewLineForClausesInQuery")});
static CSharpFormattingOptions()
{
// Note that the static constructor executes after all the static field initializers for the options have executed,
// and each field initializer adds the created option to the following builders.
AllOptions = s_allOptionsBuilder.ToImmutable();
SpacingWithinParenthesisOptionsMap = s_spacingWithinParenthesisOptionsMapBuilder.ToImmutable();
NewLineOptionsMap = s_newLineOptionsMapBuilder.ToImmutable();
}
}
internal enum LabelPositionOptions
......
......@@ -7,6 +7,7 @@
<OutputType>Library</OutputType>
<RootNamespace>Microsoft.CodeAnalysis</RootNamespace>
<TargetFramework>netstandard1.3</TargetFramework>
<DefineConstants>$(DefineConstants),CODE_STYLE</DefineConstants>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
......@@ -31,7 +32,6 @@
<ItemGroup>
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\EnumerableExtensions.cs" Link="Formatting\EnumerableExtensions.cs" />
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\ExceptionUtilities.cs" Link="ExceptionUtilities.cs" />
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\Hash.cs" Link="Hash.cs" />
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\IReadOnlySet.cs" Link="InternalUtilities\IReadOnlySet.cs" />
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\KeyValuePairUtil.cs" Link="InternalUtilities\KeyValuePairUtil.cs" />
<Compile Include="..\..\..\Compilers\Core\Portable\InternalUtilities\SpecializedCollections.cs" Link="InternalUtilities\SpecializedCollections.cs" />
......@@ -125,6 +125,10 @@
<Compile Include="..\..\..\Workspaces\Core\Portable\Log\Logger.LogBlock.cs" Link="Formatting\Logger.LogBlock.cs" />
<Compile Include="..\..\..\Workspaces\Core\Portable\Log\LogMessage.cs" Link="Formatting\LogMessage.cs" />
<Compile Include="..\..\..\Workspaces\Core\Portable\NamingStyles\EditorConfig\EditorConfigSeverityStrings.cs" Link="Options\EditorConfigSeverityStrings.cs" />
<Compile Include="..\..\..\Workspaces\Core\Portable\Options\EditorConfig\EditorConfigStorageLocation.cs" Link="Options\EditorConfigStorageLocation.cs" />
<Compile Include="..\..\..\Workspaces\Core\Portable\Options\EditorConfig\EditorConfigStorageLocation`1.cs" Link="Options\EditorConfigStorageLocation`1.cs" />
<Compile Include="..\..\..\Workspaces\Core\Portable\Options\EditorConfig\IEditorConfigStorageLocation.cs" Link="Options\IEditorConfigStorageLocation.cs" />
<Compile Include="..\..\..\Workspaces\Core\Portable\Options\EditorConfig\IEditorConfigStorageLocation2.cs" Link="Options\IEditorConfigStorageLocation2.cs" />
<Compile Include="..\..\..\Workspaces\Core\Portable\Options\LocalUserProfileStorageLocation.cs" Link="Options\LocalUserProfileStorageLocation.cs" />
<Compile Include="..\..\..\Workspaces\Core\Portable\Options\RoamingProfileStorageLocation.cs" Link="Options\RoamingProfileStorageLocation.cs" />
<Compile Include="..\..\..\Workspaces\Core\Portable\Shared\Collections\IIntervalIntrospector.cs" Link="Formatting\Context\IIntervalIntrospector.cs" />
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
namespace Microsoft.CodeAnalysis.Options
{
internal static class EditorConfigStorageLocation
{
public static EditorConfigStorageLocation<bool> ForBoolOption(string keyName)
=> new EditorConfigStorageLocation<bool>(keyName, s_parseBool, s_getBoolEditorConfigStringForValue);
public static EditorConfigStorageLocation<int> ForInt32Option(string keyName)
=> new EditorConfigStorageLocation<int>(keyName, s_parseInt32, s_getInt32EditorConfigStringForValue);
private static readonly Func<string, Optional<bool>> s_parseBool = ParseBool;
private static Optional<bool> ParseBool(string str)
=> bool.TryParse(str, out var result) ? result : new Optional<bool>();
private static readonly Func<bool, string> s_getBoolEditorConfigStringForValue = GetBoolEditorConfigStringForValue;
private static string GetBoolEditorConfigStringForValue(bool value) => value.ToString().ToLowerInvariant();
private static readonly Func<string, Optional<int>> s_parseInt32 = ParseInt32;
private static Optional<int> ParseInt32(string str)
=> int.TryParse(str, out var result) ? result : new Optional<int>();
private static readonly Func<int, string> s_getInt32EditorConfigStringForValue = GetInt32EditorConfigStringForValue;
private static string GetInt32EditorConfigStringForValue(int value) => value.ToString().ToLowerInvariant();
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using Microsoft.CodeAnalysis.Diagnostics;
namespace Microsoft.CodeAnalysis.Options
{
/// <summary>
/// Specifies that an option should be read from an .editorconfig file.
/// </summary>
internal sealed class EditorConfigStorageLocation<T> : OptionStorageLocation
{
public string KeyName { get; }
private readonly Func<string, Type, Optional<T>> _parseValue;
private readonly Func<T, OptionSet, string> _getEditorConfigStringForValue;
public EditorConfigStorageLocation(string keyName, Func<string, Optional<T>> parseValue, Func<T, string> getEditorConfigStringForValue)
: this (keyName, parseValue, (value, optionSet) => getEditorConfigStringForValue(value))
{
if (getEditorConfigStringForValue == null)
{
throw new ArgumentNullException(nameof(getEditorConfigStringForValue));
}
}
public EditorConfigStorageLocation(string keyName, Func<string, Optional<T>> parseValue, Func<OptionSet, string> getEditorConfigStringForValue)
: this(keyName, parseValue, (value, optionSet) => getEditorConfigStringForValue(optionSet))
{
if (getEditorConfigStringForValue == null)
{
throw new ArgumentNullException(nameof(getEditorConfigStringForValue));
}
}
public EditorConfigStorageLocation(string keyName, Func<string, Optional<T>> parseValue, Func<T, OptionSet, string> getEditorConfigStringForValue)
{
if (parseValue == null)
{
throw new ArgumentNullException(nameof(parseValue));
}
KeyName = keyName ?? throw new ArgumentNullException(nameof(keyName));
// If we're explicitly given a parsing function we can throw away the type when parsing
_parseValue = (s, type) => parseValue(s);
_getEditorConfigStringForValue = getEditorConfigStringForValue ?? throw new ArgumentNullException(nameof(getEditorConfigStringForValue));
}
public bool TryGetOption(object underlyingOption, IReadOnlyDictionary<string, object> allRawConventions, Type type, out object result)
{
if (allRawConventions.TryGetValue(KeyName, out object value))
{
var optionalValue = _parseValue(value.ToString(), type);
if (optionalValue.HasValue)
{
result = optionalValue.Value;
}
else
{
result = null;
}
return result != null;
}
result = null;
return false;
}
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
namespace Microsoft.CodeAnalysis.Options
{
internal interface IGlobalOptionService
{
/// <summary>
/// Gets the current value of the specific option.
/// </summary>
T GetOption<T>(Option<T> option);
}
}
......@@ -340,6 +340,7 @@ public static IDisposable LogBlock(FunctionId functionId, LogMessage logMessage,
return CreateLogBlock(functionId, logMessage, GetNextUniqueBlockId(), token);
}
#if !CODE_STYLE
public static Func<FunctionId, bool> GetLoggingChecker(IGlobalOptionService optionService)
{
var functionIds = Enum.GetValues(typeof(FunctionId)).Cast<FunctionId>();
......@@ -347,5 +348,6 @@ public static IDisposable LogBlock(FunctionId functionId, LogMessage logMessage,
return functionId => functionIdOptions[functionId];
}
#endif
}
}
......@@ -13,11 +13,13 @@ public static EditorConfigStorageLocation<bool> ForBoolOption(string keyName)
public static EditorConfigStorageLocation<int> ForInt32Option(string keyName)
=> new EditorConfigStorageLocation<int>(keyName, s_parseInt32, s_getInt32EditorConfigStringForValue);
#if !CODE_STYLE
public static EditorConfigStorageLocation<CodeStyleOption<bool>> ForBoolCodeStyleOption(string keyName)
=> new EditorConfigStorageLocation<CodeStyleOption<bool>>(keyName, s_parseBoolCodeStyleOption, s_getBoolCodeStyleOptionEditorConfigStringForValue);
public static EditorConfigStorageLocation<CodeStyleOption<string>> ForStringCodeStyleOption(string keyName)
=> new EditorConfigStorageLocation<CodeStyleOption<string>>(keyName, s_parseStringCodeStyleOption, s_getStringCodeStyleOptionEditorConfigStringForValue);
#endif
private static readonly Func<string, Optional<bool>> s_parseBool = ParseBool;
private static Optional<bool> ParseBool(string str)
......@@ -31,6 +33,7 @@ private static Optional<int> ParseInt32(string str)
private static readonly Func<int, string> s_getInt32EditorConfigStringForValue = GetInt32EditorConfigStringForValue;
private static string GetInt32EditorConfigStringForValue(int value) => value.ToString().ToLowerInvariant();
#if !CODE_STYLE
private static readonly Func<string, Optional<CodeStyleOption<bool>>> s_parseBoolCodeStyleOption = ParseBoolCodeStyleOption;
private static Optional<CodeStyleOption<bool>> ParseBoolCodeStyleOption(string str)
=> CodeStyleHelpers.TryParseBoolEditorConfigCodeStyleOption(str, out var result) ? result : new Optional<CodeStyleOption<bool>>();
......@@ -45,5 +48,6 @@ private static Optional<CodeStyleOption<string>> ParseStringCodeStyleOption(stri
=> CodeStyleHelpers.TryParseStringEditorConfigCodeStyleOption(str, out var result) ? result : new Optional<CodeStyleOption<string>>();
private static readonly Func<CodeStyleOption<string>, string> s_getStringCodeStyleOptionEditorConfigStringForValue = GetStringCodeStyleOptionEditorConfigStringForValue;
private static string GetStringCodeStyleOptionEditorConfigStringForValue(CodeStyleOption<string> value) => value.Value.ToLowerInvariant();
#endif
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册