From c73558ab917b291a7c41fd0357b3de9f8e273929 Mon Sep 17 00:00:00 2001 From: Mikayla Hutchinson Date: Wed, 18 Sep 2024 00:01:29 -0400 Subject: [PATCH] Some fixes to the options API --- Core/Options/IOptionsReader.cs | 2 +- Core/Options/OptionReaderExtensions.cs | 4 +- Core/Options/TextFormattingOptionValues.cs | 6 +-- Core/Options/TextFormattingOptions.cs | 43 ++++++++++++---------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Core/Options/IOptionsReader.cs b/Core/Options/IOptionsReader.cs index 02e6563..b59083f 100644 --- a/Core/Options/IOptionsReader.cs +++ b/Core/Options/IOptionsReader.cs @@ -5,5 +5,5 @@ namespace MonoDevelop.Xml.Options; public interface IOptionsReader { - bool TryGetOption (Option option, out T value); + bool TryGetOption (Option option, out T? value); } diff --git a/Core/Options/OptionReaderExtensions.cs b/Core/Options/OptionReaderExtensions.cs index 9cb14cd..1ff808f 100644 --- a/Core/Options/OptionReaderExtensions.cs +++ b/Core/Options/OptionReaderExtensions.cs @@ -7,8 +7,8 @@ public static class OptionReaderExtensions { public static T GetOption(this IOptionsReader options, Option option) { - if (options.TryGetOption (option, out T value)) { - return value; + if (options.TryGetOption (option, out T? value)) { + return value!; } return option.DefaultValue; } diff --git a/Core/Options/TextFormattingOptionValues.cs b/Core/Options/TextFormattingOptionValues.cs index 48d5b3e..71413db 100644 --- a/Core/Options/TextFormattingOptionValues.cs +++ b/Core/Options/TextFormattingOptionValues.cs @@ -13,9 +13,9 @@ namespace MonoDevelop.Xml.Options; /// public sealed record class TextFormattingOptionValues () { - public readonly TextFormattingOptionValues Default = new (); + public static readonly TextFormattingOptionValues Default = new (); - public bool UseTabs { get; init; } = false; + public bool ConvertTabsToSpaces { get; init; } = false; public int TabSize { get; init; } = 4; public int IndentSize { get; init; } = 4; public string NewLine { get; init; } = Environment.NewLine; @@ -24,7 +24,7 @@ public sealed record class TextFormattingOptionValues () public TextFormattingOptionValues (IOptionsReader options) : this () { - UseTabs = options.GetOption (TextFormattingOptions.UseTabs); + ConvertTabsToSpaces = options.GetOption (TextFormattingOptions.ConvertTabsToSpaces); TabSize = options.GetOption (TextFormattingOptions.TabSize); IndentSize = options.GetOption (TextFormattingOptions.IndentSize); NewLine = options.GetOption (TextFormattingOptions.NewLine); diff --git a/Core/Options/TextFormattingOptions.cs b/Core/Options/TextFormattingOptions.cs index bcfa527..e6718bc 100644 --- a/Core/Options/TextFormattingOptions.cs +++ b/Core/Options/TextFormattingOptions.cs @@ -12,32 +12,37 @@ namespace MonoDevelop.Xml.Options; /// public class TextFormattingOptions { - public static readonly Option UseTabs = new ("indent_style", false, - new EditorConfigSerializer (str => str == "tab", value => value ? "tab" : "space") + public static readonly Option ConvertTabsToSpaces = new ( + "indent_style", + TextFormattingOptionValues.Default.ConvertTabsToSpaces, + new EditorConfigSerializer (str => str != "tab", value => value ? "space" : "tab") ); - public static readonly Option TabSize = new ("tab_size", 4, true); - - public static readonly Option IndentSize = new ("indent_size", 4, true); - - public static readonly Option NewLine = new ("end_of_line", Environment.NewLine, new EditorConfigSerializer ( - str => str switch { - "lf" => "\n", - "cr" => "\r", - "crlf" => "\r\n", - _ => Environment.NewLine - }, - value => value switch { - "\n" => "lf", - "\r" => "cr", - "\r\n" => "crlf", - _ => "unset" + public static readonly Option TabSize = new ("tab_size", TextFormattingOptionValues.Default.TabSize, true); + + public static readonly Option IndentSize = new ("indent_size", TextFormattingOptionValues.Default.IndentSize, true); + + public static readonly Option NewLine = new ( + "end_of_line", + TextFormattingOptionValues.Default.NewLine, + new EditorConfigSerializer ( + str => str switch { + "lf" => "\n", + "cr" => "\r", + "crlf" => "\r\n", + _ => Environment.NewLine + }, + value => value switch { + "\n" => "lf", + "\r" => "cr", + "\r\n" => "crlf", + _ => "unset" })); public static readonly Option InsertFinalNewline = new ("insert_final_newline", true, true); - public static readonly Option TrimTrailingWhitespace = new ("trim_trailing_whitespace", true, true); + public static readonly Option TrimTrailingWhitespace = new ("trim_trailing_whitespace", TextFormattingOptionValues.Default.TrimTrailingWhitespace, true); public static readonly Option MaxLineLength = new ("max_line_length", null, new EditorConfigSerializer ( str => str != "off" && int.TryParse (str, out var val) && val > 0 ? val : null,