Skip to content

Commit

Permalink
Update editorconfig parsing to remain backwards-compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh DeGraw committed Dec 21, 2022
1 parent e8336a6 commit 3a89ead
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/Fantomas.Core.Tests/RecordTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module Fantomas.Core.Tests.RecordTests
open NUnit.Framework
open FsUnit
open Fantomas.Core.Tests.TestHelper
open Fantomas.Core.FormatConfig

[<Test>]
let ``record declaration`` () =
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Fantomas.Core/FormatConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

[<RequireQualifiedAccess>]
Expand Down
44 changes: 40 additions & 4 deletions src/Fantomas.Tests/EditorConfigurationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ insert_final_newline = false
Assert.IsFalse config.InsertFinalNewline

[<Test>]
let ``Stroustrup style`` () =
let fsharp_experimental_stroustrup_style () =
let rootDir = tempName ()

let editorConfig =
Expand All @@ -468,14 +468,51 @@ fsharp_experimental_stroustrup_style = true
Assert.IsTrue config.ExperimentalStroustrupStyle

[<Test>]
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)

[<Test>]
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)

[<Test>]
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 =
Expand All @@ -486,4 +523,3 @@ fsharp_experimental_stroustrup_style = true
let config = EditorConfig.readConfiguration fsharpFile.FSharpFile

Assert.AreEqual(Aligned, config.MultilineBracketStyle)
Assert.IsTrue config.ExperimentalStroustrupStyle
50 changes: 28 additions & 22 deletions src/Fantomas/EditorConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -68,49 +68,55 @@ 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 =
if b = "true" then Some(box true)
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<string, string>) =
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<string, string>)
: 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
Expand Down

0 comments on commit 3a89ead

Please sign in to comment.