提交 7182ba08 编写于 作者: J Jason Malinowski

Some tweaks to the EditorConfigStorageLocation surface area

- Don't expose the lambda back out
- Make a default case so you don't have to specify a lambda at all
  for common things like bool or int
上级 57584f7c
......@@ -130,7 +130,7 @@ public bool TryGetDocumentOption(Document document, OptionKey option, out object
if (_codingConventionSnapshot.TryGetConventionValue(editorConfigPersistence.KeyName, out value))
{
value = editorConfigPersistence.ParseFunction(value.ToString());
value = editorConfigPersistence.ParseValue(value.ToString(), option.Option.Type);
return true;
}
else
......
......@@ -12,11 +12,11 @@ public static class FormattingOptions
// This is also serialized by the Visual Studio-specific LanguageSettingsPersister
public static PerLanguageOption<int> TabSize { get; } = new PerLanguageOption<int>(nameof(FormattingOptions), nameof(TabSize), defaultValue: 4,
storageLocations: new EditorConfigStorageLocation("tab_width", s => int.Parse(s)));
storageLocations: new EditorConfigStorageLocation("tab_width"));
// This is also serialized by the Visual Studio-specific LanguageSettingsPersister
public static PerLanguageOption<int> IndentationSize { get; } = new PerLanguageOption<int>(nameof(FormattingOptions), nameof(IndentationSize), defaultValue: 4,
storageLocations: new EditorConfigStorageLocation("indent_size", s => int.Parse(s)));
storageLocations: new EditorConfigStorageLocation("indent_size"));
// This is also serialized by the Visual Studio-specific LanguageSettingsPersister
public static PerLanguageOption<IndentStyle> SmartIndent { get; } = new PerLanguageOption<IndentStyle>(nameof(FormattingOptions), nameof(SmartIndent), defaultValue: IndentStyle.Smart);
......
......@@ -10,12 +10,38 @@ namespace Microsoft.CodeAnalysis.Options
internal sealed class EditorConfigStorageLocation : OptionStorageLocation
{
public string KeyName { get; }
public Func<string, object> ParseFunction { get; }
public EditorConfigStorageLocation(string keyName, Func<string, object> parseFunction)
private Func<string, Type, object> _parseValue;
public object ParseValue(string s, Type type) => _parseValue(s, type);
public EditorConfigStorageLocation(string keyName)
{
KeyName = keyName;
_parseValue = (s, type) =>
{
if (type == typeof(int))
{
return int.Parse(s);
}
else if (type == typeof(bool))
{
return bool.Parse(s);
}
else
{
throw new NotSupportedException(WorkspacesResources.Option_0_has_an_unsupported_type_to_use_with_1_You_should_specify_a_parsing_function);
}
};
}
public EditorConfigStorageLocation(string keyName, Func<string, object> parseValue)
{
KeyName = keyName;
ParseFunction = parseFunction;
// If we're explicitly given a parsing function we can throw away the type when parsing
_parseValue = (s, type) => parseValue(s);
}
}
}
......@@ -818,6 +818,16 @@ internal class WorkspacesResources {
}
}
/// <summary>
/// Looks up a localized string similar to Option &apos;{0}&apos; has an unsupported type to use with {1}. You should specify a parsing function..
/// </summary>
internal static string Option_0_has_an_unsupported_type_to_use_with_1_You_should_specify_a_parsing_function {
get {
return ResourceManager.GetString("Option_0_has_an_unsupported_type_to_use_with_1_You_should_specify_a_parsing_funct" +
"ion", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Options did not come from Workspace.
/// </summary>
......
......@@ -489,4 +489,7 @@
<data name="Stream_is_too_long" xml:space="preserve">
<value>Stream is too long.</value>
</data>
<data name="Option_0_has_an_unsupported_type_to_use_with_1_You_should_specify_a_parsing_function" xml:space="preserve">
<value>Option '{0}' has an unsupported type to use with {1}. You should specify a parsing function.</value>
</data>
</root>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册