Skip to content

Commit

Permalink
Read editorconfig settings before overwriting settings with request c…
Browse files Browse the repository at this point in the history
…onfiguration. (#2006)
  • Loading branch information
nojaf committed Dec 29, 2021
1 parent 81ebd61 commit 0349c29
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
34 changes: 34 additions & 0 deletions src/Fantomas.CoreGlobalTool.Tests/DaemonTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,37 @@ val y : string
| FormatSelectionResponse.Error _ -> Assert.Pass()
| otherResponse -> Assert.Fail $"Unexpected response %A{otherResponse}"
})

[<Test>]
let ``format document with both .editorconfig file and custom config`` () =
runWithDaemon
(fun client ->
async {
let sourceCode =
"module Foo\n\nlet add (a:int) (b:int) = //\n a + b"

use codeFile = new TemporaryFileCodeSample(sourceCode)

use _config =
new ConfigurationFile("[*.fs]\nindent_size=2")

let request =
{ SourceCode = sourceCode
FilePath = codeFile.Filename
Config = Some(readOnlyDict [ "fsharp_space_before_colon", "true" ]) }

let! response =
client.InvokeAsync<FormatDocumentResponse>(Methods.FormatDocument, request)
|> Async.AwaitTask

match response with
| FormatDocumentResponse.Formatted (_, formatted) ->
assertFormatted
formatted
"module Foo
let add (a : int) (b : int) = //
a + b
"
| otherResponse -> Assert.Fail $"Unexpected response %A{otherResponse}"
})
8 changes: 6 additions & 2 deletions src/Fantomas.CoreGlobalTool/Daemon.fs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ type FantomasDaemon(sender: Stream, reader: Stream) as this =
else
let config =
match request.Config with
| Some configProperties -> parseOptionsFromEditorConfig configProperties
| Some configProperties ->
let config = readConfiguration request.FilePath
parseOptionsFromEditorConfig config configProperties
| None -> readConfiguration request.FilePath

try
Expand All @@ -76,7 +78,9 @@ type FantomasDaemon(sender: Stream, reader: Stream) as this =
async {
let config =
match request.Config with
| Some configProperties -> parseOptionsFromEditorConfig configProperties
| Some configProperties ->
let config = readConfiguration request.FilePath
parseOptionsFromEditorConfig config configProperties
| None -> readConfiguration request.FilePath

let range =
Expand Down
13 changes: 8 additions & 5 deletions src/Fantomas.Extras/EditorConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ let toEditorConfigName value =
else
sprintf "fsharp_%s" name

let private fantomasFields =
Reflection.getRecordFields FormatConfig.Default
let private getFantomasFields (fallbackConfig: FormatConfig) =
Reflection.getRecordFields fallbackConfig
|> Array.map
(fun (recordField, defaultValue) ->
let editorConfigName =
Expand All @@ -81,8 +81,11 @@ let private (|Boolean|_|) b =
elif b = "false" then Some(box false)
else None

let parseOptionsFromEditorConfig (editorConfigProperties: IReadOnlyDictionary<string, string>) : FormatConfig =
fantomasFields
let parseOptionsFromEditorConfig
(fallbackConfig: FormatConfig)
(editorConfigProperties: IReadOnlyDictionary<string, string>)
: FormatConfig =
getFantomasFields fallbackConfig
|> Array.map
(fun (ecn, dv) ->
match editorConfigProperties.TryGetValue(ecn) with
Expand Down Expand Up @@ -124,7 +127,7 @@ let tryReadConfiguration (fsharpFile: string) : FormatConfig option =
if editorConfigSettings.Properties.Count = 0 then
None
else
Some(parseOptionsFromEditorConfig editorConfigSettings.Properties)
Some(parseOptionsFromEditorConfig FormatConfig.Default editorConfigSettings.Properties)

let readConfiguration (fsharpFile: string) : FormatConfig =
tryReadConfiguration fsharpFile
Expand Down

0 comments on commit 0349c29

Please sign in to comment.