Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add basic support for "Consistent" Pattern Matching #3085

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 21 additions & 25 deletions fake-sample/script.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
25 changes: 23 additions & 2 deletions src/Fantomas.Core/FormatConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ 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
Expand Down Expand Up @@ -227,7 +242,12 @@ type FormatConfig =

[<Category("Convention")>]
[<DisplayName("Applies the Stroustrup style to the final (two) array or list argument(s) in a function application")>]
ExperimentalElmish: bool }
ExperimentalElmish: bool

[<Category("Indentation")>]
[<DisplayName("How to format pattern match expression")>]
[<Description("Possible options include each line judged separately (default), or consistent (either all single line or all multiline)")>]
ExperimentalPatternMatchStyle: PatternMatchStyle }

member x.IsStroustrupStyle = x.MultilineBracketStyle = Stroustrup

Expand Down Expand Up @@ -268,4 +288,5 @@ type FormatConfig =
MultilineBracketStyle = Cramped
KeepMaxNumberOfBlankLines = 100
NewlineBeforeMultilineComputationExpression = true
ExperimentalElmish = false }
ExperimentalElmish = false
ExperimentalPatternMatchStyle = LineSpecific }
19 changes: 19 additions & 0 deletions src/Fantomas.Tests/EditorConfigurationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,22 @@ fsharp_experimental_elmish = true
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile

Assert.That(config.ExperimentalElmish, Is.True)

[<Test>]
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)
6 changes: 6 additions & 0 deletions src/Fantomas/EditorConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ let (|Number|_|) (d: string) =
| true, d -> Some(box d)
| _ -> None

let (|PatternMatchStyle|_|) pms = PatternMatchStyle.OfConfigString pms

let (|MultilineFormatterType|_|) mft =
MultilineFormatterType.OfConfigString mft

Expand All @@ -86,6 +88,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)
Expand All @@ -105,6 +108,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
Expand Down
Loading