Skip to content

Commit

Permalink
Some fixes to the options API
Browse files Browse the repository at this point in the history
  • Loading branch information
mhutch committed Sep 18, 2024
1 parent 95aafd2 commit c73558a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Core/Options/IOptionsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace MonoDevelop.Xml.Options;

public interface IOptionsReader
{
bool TryGetOption<T> (Option<T> option, out T value);
bool TryGetOption<T> (Option<T> option, out T? value);
}
4 changes: 2 additions & 2 deletions Core/Options/OptionReaderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public static class OptionReaderExtensions
{
public static T GetOption<T>(this IOptionsReader options, Option<T> option)
{
if (options.TryGetOption<T> (option, out T value)) {
return value;
if (options.TryGetOption<T> (option, out T? value)) {
return value!;
}
return option.DefaultValue;
}
Expand Down
6 changes: 3 additions & 3 deletions Core/Options/TextFormattingOptionValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace MonoDevelop.Xml.Options;
/// </summary>
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;
Expand All @@ -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);
Expand Down
43 changes: 24 additions & 19 deletions Core/Options/TextFormattingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,37 @@ namespace MonoDevelop.Xml.Options;
/// </summary>
public class TextFormattingOptions
{
public static readonly Option<bool> UseTabs = new ("indent_style", false,
new EditorConfigSerializer<bool> (str => str == "tab", value => value ? "tab" : "space")
public static readonly Option<bool> ConvertTabsToSpaces = new (
"indent_style",
TextFormattingOptionValues.Default.ConvertTabsToSpaces,
new EditorConfigSerializer<bool> (str => str != "tab", value => value ? "space" : "tab")
);

public static readonly Option<int> TabSize = new ("tab_size", 4, true);

public static readonly Option<int> IndentSize = new ("indent_size", 4, true);

public static readonly Option<string> NewLine = new ("end_of_line", Environment.NewLine, new EditorConfigSerializer<string> (
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<int> TabSize = new ("tab_size", TextFormattingOptionValues.Default.TabSize, true);

public static readonly Option<int> IndentSize = new ("indent_size", TextFormattingOptionValues.Default.IndentSize, true);

public static readonly Option<string> NewLine = new (
"end_of_line",
TextFormattingOptionValues.Default.NewLine,
new EditorConfigSerializer<string> (
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<bool> InsertFinalNewline = new ("insert_final_newline", true, true);

public static readonly Option<bool> TrimTrailingWhitespace = new ("trim_trailing_whitespace", true, true);
public static readonly Option<bool> TrimTrailingWhitespace = new ("trim_trailing_whitespace", TextFormattingOptionValues.Default.TrimTrailingWhitespace, true);

public static readonly Option<int?> MaxLineLength = new ("max_line_length", null, new EditorConfigSerializer<int?> (
str => str != "off" && int.TryParse (str, out var val) && val > 0 ? val : null,
Expand Down

0 comments on commit c73558a

Please sign in to comment.