Skip to content

Commit

Permalink
Update editorconfig package (#2895)
Browse files Browse the repository at this point in the history
* Use EditorConfigCache.

* Clean up EditorConfig.fs(i)
  • Loading branch information
nojaf committed Jun 19, 2023
1 parent cca528d commit 12e2c6d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/Fantomas.Tests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
},
"editorconfig": {
"type": "Transitive",
"resolved": "0.13.0",
"contentHash": "1IAdbakbxaKyVv/gr3VTHkbFMFgfOSpP4VRSvOg50Ee9qAfXDr4F8FvOtwq7Pi6fcoxxXYfOYceT1U3oODdpFg=="
"resolved": "0.14.0",
"contentHash": "emt1KlBtTsTSHWeLnlD1grQYN991wlzIh8t0/HF8ambYTLE8v25sPU9q2eu3sNj/Za7Ij6a0MW00lDGTsphEhQ=="
},
"Fable.Core": {
"type": "Transitive",
Expand Down Expand Up @@ -922,7 +922,7 @@
"StreamJsonRpc": "[2.8.28, )",
"System.IO.Abstractions": "[17.2.3, )",
"Thoth.Json.Net": "[8.0.0, )",
"editorconfig": "[0.13.0, )"
"editorconfig": "[0.14.0, )"
}
},
"fantomas.client": {
Expand Down
27 changes: 14 additions & 13 deletions src/Fantomas/EditorConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Fantomas.EditorConfig

open System.Collections.Generic
open System.ComponentModel
open EditorConfig.Core
open Fantomas.Core

module Reflection =
Expand All @@ -15,7 +16,7 @@ module Reflection =
DisplayName: string option
Description: string option }

let inline private getCustomAttribute<'t, 'v when 't :> Attribute and 't: null>
let inline getCustomAttribute<'t, 'v when 't :> Attribute and 't: null>
(projection: 't -> 'v)
(property: PropertyInfo)
: 'v option =
Expand All @@ -40,7 +41,7 @@ let toEditorConfigName value =
value
|> Seq.map (fun c ->
if System.Char.IsUpper(c) then
sprintf "_%s" (c.ToString().ToLower())
$"_%s{c.ToString().ToLower()}"
else
c.ToString())
|> String.concat ""
Expand All @@ -49,28 +50,28 @@ let toEditorConfigName value =
if List.contains name supportedProperties then
name
else
sprintf "fsharp_%s" name
$"fsharp_%s{name}"

let private getFantomasFields (fallbackConfig: FormatConfig) =
let getFantomasFields (fallbackConfig: FormatConfig) =
Reflection.getRecordFields fallbackConfig
|> Array.map (fun (recordField, defaultValue) ->
let editorConfigName = toEditorConfigName recordField.PropertyName

(editorConfigName, defaultValue))

let private (|Number|_|) (d: string) =
let (|Number|_|) (d: string) =
match System.Int32.TryParse(d) with
| true, d -> Some(box d)
| _ -> None

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

let private (|BracketStyle|_|) bs = MultilineBracketStyle.OfConfigString bs
let (|BracketStyle|_|) bs = MultilineBracketStyle.OfConfigString bs

let private (|EndOfLineStyle|_|) eol = EndOfLineStyle.OfConfigString eol
let (|EndOfLineStyle|_|) eol = EndOfLineStyle.OfConfigString eol

let private (|Boolean|_|) b =
let (|Boolean|_|) b =
if b = "true" then Some(box true)
elif b = "false" then Some(box false)
else None
Expand Down Expand Up @@ -102,18 +103,18 @@ let configToEditorConfig (config: FormatConfig) : string =
|> Some
| :? System.Int32 as i -> $"%s{toEditorConfigName recordField.PropertyName}=%d{i}" |> Some
| :? MultilineFormatterType as mft ->
sprintf "%s=%s" (toEditorConfigName recordField.PropertyName) (MultilineFormatterType.ToConfigString mft)
$"%s{toEditorConfigName recordField.PropertyName}=%s{MultilineFormatterType.ToConfigString mft}"
|> Some
| :? EndOfLineStyle as eols ->
sprintf "%s=%s" (toEditorConfigName recordField.PropertyName) (EndOfLineStyle.ToConfigString eols)
$"%s{toEditorConfigName recordField.PropertyName}=%s{EndOfLineStyle.ToConfigString eols}"
|> Some
| _ -> None)
|> String.concat "\n"

let private editorConfigParser = EditorConfig.Core.EditorConfigParser()
let editorConfigParser = EditorConfigParser(EditorConfigFileCache.GetOrCreate)

let tryReadConfiguration (fsharpFile: string) : FormatConfig option =
let editorConfigSettings: EditorConfig.Core.FileConfiguration =
let editorConfigSettings: FileConfiguration =
editorConfigParser.Parse(fileName = fsharpFile)

if editorConfigSettings.Properties.Count = 0 then
Expand Down
2 changes: 0 additions & 2 deletions src/Fantomas/EditorConfig.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ module Reflection =

val inline getRecordFields: x: 'a -> (FSharpRecordField * obj)[]

val supportedProperties: string list

val toEditorConfigName: value: seq<char> -> string

val parseOptionsFromEditorConfig:
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas/Fantomas.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<PackageReference Include="Argu" Version="6.1.1" />
<PackageReference Include="Thoth.Json.Net" Version="8.0.0" />
<PackageReference Include="SerilogTraceListener" Version="3.2.1-dev-00011" />
<PackageReference Include="editorconfig" Version="0.13.0" />
<PackageReference Include="editorconfig" Version="0.14.0" />
<PackageReference Include="Ignore" Version="0.1.46" />
<PackageReference Include="System.IO.Abstractions" Version="17.2.3" />
<PackageReference Include="Spectre.Console" Version="0.46.1-preview.0.6" />
Expand Down
6 changes: 3 additions & 3 deletions src/Fantomas/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
},
"editorconfig": {
"type": "Direct",
"requested": "[0.13.0, )",
"resolved": "0.13.0",
"contentHash": "1IAdbakbxaKyVv/gr3VTHkbFMFgfOSpP4VRSvOg50Ee9qAfXDr4F8FvOtwq7Pi6fcoxxXYfOYceT1U3oODdpFg=="
"requested": "[0.14.0, )",
"resolved": "0.14.0",
"contentHash": "emt1KlBtTsTSHWeLnlD1grQYN991wlzIh8t0/HF8ambYTLE8v25sPU9q2eu3sNj/Za7Ij6a0MW00lDGTsphEhQ=="
},
"FSharp.Core": {
"type": "Direct",
Expand Down

0 comments on commit 12e2c6d

Please sign in to comment.