From 56e10d63fa4c2a5f0aba4868d5b7df4f20b081fe Mon Sep 17 00:00:00 2001 From: Isaac Abraham Date: Fri, 17 May 2024 13:37:16 +0100 Subject: [PATCH 1/2] Add configuration flag. --- src/Fantomas.Core/FormatConfig.fs | 24 +++++++++++++++++-- .../EditorConfigurationTests.fs | 19 +++++++++++++++ src/Fantomas/EditorConfig.fs | 7 ++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/Fantomas.Core/FormatConfig.fs b/src/Fantomas.Core/FormatConfig.fs index e2abad7247..f1ebed65f1 100644 --- a/src/Fantomas.Core/FormatConfig.fs +++ b/src/Fantomas.Core/FormatConfig.fs @@ -26,6 +26,19 @@ type MultilineFormatterType = | "number_of_items" -> Some MultilineFormatterType.NumberOfItems | _ -> None +type PatternMatchStyle = + | LineSpecific + | Consistent + static member ToConfigString(cfg: PatternMatchStyle) = + match cfg with + | LineSpecific -> "line_specific" + | Consistent -> "consistent" + static member OfConfigString(cfgString: string) = + match cfgString with + | "line_specific" -> Some LineSpecific + | "consistent" -> Some Consistent + | _ -> None + type MultilineBracketStyle = | Cramped | Aligned @@ -227,7 +240,13 @@ type FormatConfig = [] [] - ExperimentalElmish: bool } + ExperimentalElmish: bool + + [] + [] + [] + ExperimentalPatternMatchStyle : PatternMatchStyle + } member x.IsStroustrupStyle = x.MultilineBracketStyle = Stroustrup @@ -268,4 +287,5 @@ type FormatConfig = MultilineBracketStyle = Cramped KeepMaxNumberOfBlankLines = 100 NewlineBeforeMultilineComputationExpression = true - ExperimentalElmish = false } + ExperimentalElmish = false + ExperimentalPatternMatchStyle = LineSpecific } diff --git a/src/Fantomas.Tests/EditorConfigurationTests.fs b/src/Fantomas.Tests/EditorConfigurationTests.fs index 6c00fdc2a3..c890f5529a 100644 --- a/src/Fantomas.Tests/EditorConfigurationTests.fs +++ b/src/Fantomas.Tests/EditorConfigurationTests.fs @@ -543,3 +543,22 @@ fsharp_experimental_elmish = true let config = EditorConfig.readConfiguration fsharpFile.FSharpFile Assert.That(config.ExperimentalElmish, Is.True) + +[] +let fsharp_consistent_pattern_matching_style () = + let rootDir = tempName () + + let editorConfig = + """ +[*.fs] +fsharp_experimental_pattern_match_style = consistent +""" + + use configFixture = + new ConfigurationFile(defaultConfig, rootDir, content = editorConfig) + + use fsharpFile = new FSharpFile(rootDir) + + let config = EditorConfig.readConfiguration fsharpFile.FSharpFile + + Assert.That(config.ExperimentalPatternMatchStyle, Is.EqualTo Consistent) diff --git a/src/Fantomas/EditorConfig.fs b/src/Fantomas/EditorConfig.fs index f362fe4ba6..ad015b8a66 100644 --- a/src/Fantomas/EditorConfig.fs +++ b/src/Fantomas/EditorConfig.fs @@ -64,6 +64,9 @@ let (|Number|_|) (d: string) = | true, d -> Some(box d) | _ -> None +let (|PatternMatchStyle|_|) pms = + PatternMatchStyle.OfConfigString pms + let (|MultilineFormatterType|_|) mft = MultilineFormatterType.OfConfigString mft @@ -86,6 +89,7 @@ let parseOptionsFromEditorConfig | true, Number n -> n | true, Boolean b -> b | true, MultilineFormatterType mft -> box mft + | true, PatternMatchStyle pms -> box pms | true, EndOfLineStyle eol -> box eol | true, BracketStyle bs -> box bs | _ -> defaultValue) @@ -105,6 +109,9 @@ let configToEditorConfig (config: FormatConfig) : string = | :? MultilineFormatterType as mft -> $"%s{toEditorConfigName recordField.PropertyName}=%s{MultilineFormatterType.ToConfigString mft}" |> Some + | :? PatternMatchStyle as pms -> + $"%s{toEditorConfigName recordField.PropertyName}=%s{PatternMatchStyle.ToConfigString pms}" + |> Some | :? EndOfLineStyle as eols -> $"%s{toEditorConfigName recordField.PropertyName}=%s{EndOfLineStyle.ToConfigString eols}" |> Some From 6187cf6ee54044b92365ce372bbd0832ab32f6ae Mon Sep 17 00:00:00 2001 From: Isaac Abraham Date: Mon, 20 May 2024 14:26:24 +0100 Subject: [PATCH 2/2] Fotmatting --- fake-sample/script.fsx | 46 ++++++++++++++----------------- src/Fantomas.Core/FormatConfig.fs | 5 ++-- src/Fantomas/EditorConfig.fs | 3 +- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/fake-sample/script.fsx b/fake-sample/script.fsx index 440903232c..9a08fc32d0 100644 --- a/fake-sample/script.fsx +++ b/fake-sample/script.fsx @@ -11,33 +11,29 @@ open Fake.DotNet let sourceFiles = !! "*.fs" -Target.create - "CheckFormat" - (fun _ -> - let result = - sourceFiles - |> Seq.map (sprintf "\"%s\"") - |> String.concat " " - |> sprintf "%s --check" - |> DotNet.exec id "fantomas" +Target.create "CheckFormat" (fun _ -> + let result = + sourceFiles + |> Seq.map (sprintf "\"%s\"") + |> String.concat " " + |> sprintf "%s --check" + |> DotNet.exec id "fantomas" - if result.ExitCode = 0 then - Trace.log "No files need formatting" - elif result.ExitCode = 99 then - failwith "Some files need formatting, check output for more info" - else - Trace.logf "Errors while formatting: %A" result.Errors) + if result.ExitCode = 0 then + Trace.log "No files need formatting" + elif result.ExitCode = 99 then + failwith "Some files need formatting, check output for more info" + else + Trace.logf "Errors while formatting: %A" result.Errors) -Target.create - "Format" - (fun _ -> - let result = - sourceFiles - |> Seq.map (sprintf "\"%s\"") - |> String.concat " " - |> DotNet.exec id "fantomas" +Target.create "Format" (fun _ -> + let result = + sourceFiles + |> Seq.map (sprintf "\"%s\"") + |> String.concat " " + |> DotNet.exec id "fantomas" - if not result.OK then - printfn "Errors while formatting all files: %A" result.Messages) + if not result.OK then + printfn "Errors while formatting all files: %A" result.Messages) Target.runOrList () diff --git a/src/Fantomas.Core/FormatConfig.fs b/src/Fantomas.Core/FormatConfig.fs index f1ebed65f1..5c41e92ad3 100644 --- a/src/Fantomas.Core/FormatConfig.fs +++ b/src/Fantomas.Core/FormatConfig.fs @@ -29,10 +29,12 @@ type MultilineFormatterType = type PatternMatchStyle = | LineSpecific | Consistent + static member ToConfigString(cfg: PatternMatchStyle) = match cfg with | LineSpecific -> "line_specific" | Consistent -> "consistent" + static member OfConfigString(cfgString: string) = match cfgString with | "line_specific" -> Some LineSpecific @@ -245,8 +247,7 @@ type FormatConfig = [] [] [] - ExperimentalPatternMatchStyle : PatternMatchStyle - } + ExperimentalPatternMatchStyle: PatternMatchStyle } member x.IsStroustrupStyle = x.MultilineBracketStyle = Stroustrup diff --git a/src/Fantomas/EditorConfig.fs b/src/Fantomas/EditorConfig.fs index ad015b8a66..04ca628612 100644 --- a/src/Fantomas/EditorConfig.fs +++ b/src/Fantomas/EditorConfig.fs @@ -64,8 +64,7 @@ let (|Number|_|) (d: string) = | true, d -> Some(box d) | _ -> None -let (|PatternMatchStyle|_|) pms = - PatternMatchStyle.OfConfigString pms +let (|PatternMatchStyle|_|) pms = PatternMatchStyle.OfConfigString pms let (|MultilineFormatterType|_|) mft = MultilineFormatterType.OfConfigString mft