From 3a89eadca3697535382ba38aa461a886f56bd34d Mon Sep 17 00:00:00 2001 From: Josh DeGraw Date: Mon, 19 Sep 2022 09:35:50 -0600 Subject: [PATCH] Update editorconfig parsing to remain backwards-compatible --- src/Fantomas.Core.Tests/RecordTests.fs | 3 +- src/Fantomas.Core/FormatConfig.fs | 6 +-- .../EditorConfigurationTests.fs | 44 ++++++++++++++-- src/Fantomas/EditorConfig.fs | 50 +++++++++++-------- 4 files changed, 73 insertions(+), 30 deletions(-) diff --git a/src/Fantomas.Core.Tests/RecordTests.fs b/src/Fantomas.Core.Tests/RecordTests.fs index 47c7eb71ad..7e32c5c2cb 100644 --- a/src/Fantomas.Core.Tests/RecordTests.fs +++ b/src/Fantomas.Core.Tests/RecordTests.fs @@ -3,6 +3,7 @@ module Fantomas.Core.Tests.RecordTests open NUnit.Framework open FsUnit open Fantomas.Core.Tests.TestHelper +open Fantomas.Core.FormatConfig [] let ``record declaration`` () = @@ -2127,7 +2128,7 @@ let compareThings (first: Thing) (second: Thing) = Bar = first.Bar } """ - { config with MultilineBlockBracketsOnSameColumn = true } + { config with MultilineBracketStyle = Aligned } |> prepend newline |> should equal diff --git a/src/Fantomas.Core/FormatConfig.fs b/src/Fantomas.Core/FormatConfig.fs index e92835b2aa..da78a5d8dd 100644 --- a/src/Fantomas.Core/FormatConfig.fs +++ b/src/Fantomas.Core/FormatConfig.fs @@ -38,9 +38,9 @@ type MultilineBracketStyle = static member OfConfigString(cfgString: string) = match cfgString with - | "classic" -> Some Classic - | "aligned" -> Some Aligned - | "experimental_stroustrup" -> Some ExperimentalStroustrup + | "classic" -> Some(box Classic) + | "aligned" -> Some(box Aligned) + | "experimental_stroustrup" -> Some(box ExperimentalStroustrup) | _ -> None [] diff --git a/src/Fantomas.Tests/EditorConfigurationTests.fs b/src/Fantomas.Tests/EditorConfigurationTests.fs index d82ee52d13..4ca80a0451 100644 --- a/src/Fantomas.Tests/EditorConfigurationTests.fs +++ b/src/Fantomas.Tests/EditorConfigurationTests.fs @@ -447,7 +447,7 @@ insert_final_newline = false Assert.IsFalse config.InsertFinalNewline [] -let ``Stroustrup style`` () = +let fsharp_experimental_stroustrup_style () = let rootDir = tempName () let editorConfig = @@ -468,14 +468,51 @@ fsharp_experimental_stroustrup_style = true Assert.IsTrue config.ExperimentalStroustrupStyle [] -let ``Aligned style`` () = +let ``fsharp_multiline_bracket_style = experimental_stroustrup`` () = + let rootDir = tempName () + + let editorConfig = + """ +[*.fs] +fsharp_multiline_bracket_style = experimental_stroustrup +""" + + use configFixture = + new ConfigurationFile(defaultConfig, rootDir, content = editorConfig) + + use fsharpFile = new FSharpFile(rootDir) + + let config = EditorConfig.readConfiguration fsharpFile.FSharpFile + + Assert.AreEqual(ExperimentalStroustrup, config.MultilineBracketStyle) + +[] +let ``fsharp_multiline_bracket_style = aligned`` () = + let rootDir = tempName () + + let editorConfig = + """ +[*.fs] +fsharp_multiline_bracket_style = aligned +""" + + use configFixture = + new ConfigurationFile(defaultConfig, rootDir, content = editorConfig) + + use fsharpFile = new FSharpFile(rootDir) + + let config = EditorConfig.readConfiguration fsharpFile.FSharpFile + + Assert.AreEqual(Aligned, config.MultilineBracketStyle) + +[] +let fsharp_multiline_block_brackets_on_same_column () = let rootDir = tempName () let editorConfig = """ [*.fs] fsharp_multiline_block_brackets_on_same_column = true -fsharp_experimental_stroustrup_style = true """ use configFixture = @@ -486,4 +523,3 @@ fsharp_experimental_stroustrup_style = true let config = EditorConfig.readConfiguration fsharpFile.FSharpFile Assert.AreEqual(Aligned, config.MultilineBracketStyle) - Assert.IsTrue config.ExperimentalStroustrupStyle diff --git a/src/Fantomas/EditorConfig.fs b/src/Fantomas/EditorConfig.fs index 7cc22eda4f..77be04694a 100644 --- a/src/Fantomas/EditorConfig.fs +++ b/src/Fantomas/EditorConfig.fs @@ -68,12 +68,6 @@ let private (|MultilineFormatterType|_|) mft = let private (|BracketStyle|_|) bs = MultilineBracketStyle.OfConfigString bs -let private (|OldBracketStyle|_|) input = - match input with - | "fsharp_experimental_stroustrup_style" -> Some input - | "fsharp_multiline_block_brackets_on_same_column" -> Some input - | _ -> None - let private (|EndOfLineStyle|_|) eol = EndOfLineStyle.OfConfigString eol let private (|Boolean|_|) b = @@ -81,36 +75,48 @@ let private (|Boolean|_|) b = elif b = "false" then Some(box false) else None -// TODO: Make this backwards compatible with old alignment style -// let private updateOldBracketStyle (values: obj[]) = -// let oldStrous = values |> Array.tryFindIndex (fun v -> v = box "fsharp_experimental_stroustrup_style") -// let oldAlign = values |> Array.tryFindIndex (fun v -> v = box "fsharp_multiline_block_brackets_on_same_column") -// -// match oldAlign, oldStrous with -// | None, None -> -// values -// | Some i, None -> -// values |> Array.set i (box Aligned) -// +let private (|OldStroustrup|OldAligned|Unspecified|) (input: IReadOnlyDictionary) = + let toOption = + function + | true, "true" -> Some true + | true, "false" -> Some false + | _ -> None + + let hasStroustrup = + input.TryGetValue("fsharp_experimental_stroustrup_style") |> toOption + + let hasAligned = + input.TryGetValue("fsharp_multiline_block_brackets_on_same_column") |> toOption + + match hasAligned, hasStroustrup with + | Some true, Some true -> OldStroustrup + | Some true, _ -> OldAligned + | _ -> Unspecified let parseOptionsFromEditorConfig (fallbackConfig: FormatConfig) (editorConfigProperties: IReadOnlyDictionary) : FormatConfig = getFantomasFields fallbackConfig - |> Array.map (fun (ecn, dv) -> - match editorConfigProperties.TryGetValue(ecn) with + |> Array.map (fun (editorConfigName, defaultValue) -> + match editorConfigProperties.TryGetValue(editorConfigName) with | true, Number n -> n | true, Boolean b -> b | true, MultilineFormatterType mft -> mft | true, EndOfLineStyle eol -> box eol | true, BracketStyle bs -> box bs - | true, OldBracketStyle s -> s - | _ -> dv) + | _ -> defaultValue) |> fun newValues -> let formatConfigType = FormatConfig.Default.GetType() - Microsoft.FSharp.Reflection.FSharpValue.MakeRecord(formatConfigType, newValues) :?> FormatConfig + + let config = + Microsoft.FSharp.Reflection.FSharpValue.MakeRecord(formatConfigType, newValues) :?> FormatConfig + + match editorConfigProperties with + | Unspecified -> config + | OldStroustrup -> { config with MultilineBracketStyle = ExperimentalStroustrup } + | OldAligned -> { config with MultilineBracketStyle = Aligned } let configToEditorConfig (config: FormatConfig) : string = Reflection.getRecordFields config