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

Add button to import settings from .editorconfig file #615

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
12 changes: 11 additions & 1 deletion fantomas-tools.sln
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
Expand Down Expand Up @@ -37,6 +37,16 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FantomasOnlinePreview", "sr
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FantomasOnlineV6", "src\server\FantomasOnlineV6\FantomasOnlineV6.fsproj", "{2F8EF5DD-8003-4911-B90A-0BFB970DA1C2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A76D9559-8637-413C-9109-677EC3D63578}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
build.fsx = build.fsx
Directory.Build.props = Directory.Build.props
global.json = global.json
README.md = README.md
.config\dotnet-tools.json = .config\dotnet-tools.json
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
65 changes: 65 additions & 0 deletions src/client/fsharp/FantomasOnline/Decoders.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module FantomasTools.Client.FantomasOnline.Decoders

open System
open FantomasTools.Client
open Thoth.Json
open FantomasOnline.Shared
Expand Down Expand Up @@ -33,6 +34,70 @@ let decodeOptions json =
let decodeOptionsFromUrl: Decoder<FantomasOption list> =
Decode.object (fun get -> get.Required.Field "settings" (Decode.list optionDecoder))

let rec decodeEditorConfigLine (currentSettings: Map<string, FantomasOption>) i (line: string) =
let parts =
line.Split([| '='; ' ' |], StringSplitOptions.RemoveEmptyEntries)
|> Array.toList

let toPascalCase (str: string) =
let rec buildStr acc (split: string list) =
match split with
| [] -> acc
| h :: t ->
let first = h[0] |> Char.ToUpper |> string
buildStr (acc + first + h[1..]) t

str.Split([| '_' |], StringSplitOptions.RemoveEmptyEntries)
|> Array.toList
|> buildStr ""

match parts with
| name :: value :: _ ->
let name = name.Trim().Replace("fsharp_", "")
let value = value.Trim()
let isInt, intValue = Int32.TryParse value
let isBool, boolValue = Boolean.TryParse value

let pascalName = toPascalCase name
let existing = currentSettings.TryFind pascalName

let optionValue =
match existing with
| Some(FantomasOption.IntOption(order, pascalName, _)) when isInt ->
FantomasOption.IntOption(order, pascalName, intValue)
| Some(FantomasOption.BoolOption(order, pascalName, _)) when isBool ->
FantomasOption.BoolOption(order, pascalName, boolValue)
| Some(FantomasOption.MultilineFormatterTypeOption(order, pascalName, _)) ->
FantomasOption.MultilineFormatterTypeOption(order, pascalName, value)
| Some(FantomasOption.EndOfLineStyleOption(order, pascalName, _)) ->
FantomasOption.EndOfLineStyleOption(order, pascalName, value)
| Some(FantomasOption.MultilineBracketStyleOption(order, pascalName, _)) ->
FantomasOption.MultilineBracketStyleOption(order, pascalName, value)
| _ ->
let pascalName = toPascalCase name

match name with
| _ when isInt -> FantomasOption.IntOption(i, pascalName, intValue)
| _ when isBool -> FantomasOption.BoolOption(i, pascalName, boolValue)
| "record_multiline_formatter"
| "array_or_list_multiline_formatter" ->
FantomasOption.MultilineFormatterTypeOption(i, pascalName, value)
| "end_of_line" -> FantomasOption.EndOfLineStyleOption(i, pascalName, value)
| "multiline_bracket_style" -> FantomasOption.MultilineBracketStyleOption(i, pascalName, value)
| name -> failwithf $"Cannot decode %s{name}"

pascalName, optionValue
| _ -> failwithf $"Cannot decode `{line}`"

let decodeOptionsFromEditorConfigFile (currentSettings: Map<string, FantomasOption>) (fileText: string) =
fileText.Split([| Environment.NewLine |], StringSplitOptions.RemoveEmptyEntries)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't quite work when there are multiple sections.
This will assume all settings found will apply.
Might be confusing for the users.

|> Array.filter (fun line ->
line.StartsWith("fsharp_", StringComparison.OrdinalIgnoreCase)
|| [ "max_line_length"; "indent_size"; "end_of_line"; "insert_final_newline" ]
|> List.exists (fun prop -> line.StartsWith(prop, StringComparison.OrdinalIgnoreCase)))
|> Array.mapi (decodeEditorConfigLine currentSettings)
|> Array.toList

let decodeFormatResponse: Decoder<FormatResponse> =
Decode.object (fun get ->
{ FirstFormat = get.Required.Field "firstFormat" Decode.string
Expand Down
29 changes: 29 additions & 0 deletions src/client/fsharp/FantomasOnline/View.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module FantomasTools.Client.FantomasOnline.View

open System.Text.RegularExpressions

open Fable.Core.JsInterop
open Fable.React
open Fable.React.Props
open FantomasOnline.Shared
Expand Down Expand Up @@ -339,6 +341,33 @@ let settings isFsi model dispatch =
Placeholder "Filter settings"
OnChange(fun (ev: Browser.Types.Event) -> ev.Value |> UpdateSettingsFilter |> dispatch)
]
div [ ClassName "form-group" ] [
label [ ClassName "d-block" ] [
strong [ ClassName "h4 text-center d-block mb-2" ] [ str "Upload .editorconfig" ]
]
input [
Type "file"
Multiple false
Accept ".editorconfig"
OnInput(fun ev ->
let file = ev.target?files?(0)
let reader = Browser.Dom.FileReader.Create()

reader.onload <-
fun evt ->
let fileContents: string = evt.target?result

let settings =
Decoders.decodeOptionsFromEditorConfigFile model.UserOptions fileContents

Fable.Core.JS.console.log $"%A{settings}"
settings |> List.iter (fun setting -> dispatch (UpdateOption setting))

reader.onerror <- fun evt -> Fable.Core.JS.console.error $"%A{evt.target?result}"

reader.readAsText (file))
]
]
]

fragment [] [
Expand Down
53 changes: 53 additions & 0 deletions src/client/fsharp/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,59 @@
"Fable.Browser.Dom": "2.4.4",
"Fable.Core": "3.2.7"
}
},
"Microsoft.NETCore.Platforms": {
"type": "Transitive",
"resolved": "1.1.1",
"contentHash": "TMBuzAHpTenGbGgk0SMTwyEkyijY/Eae4ZGsFNYJvAr/LDn1ku3Etp3FPxChmDp5HHF3kzJuoaa08N0xjqAJfQ=="
},
"Microsoft.NETCore.Targets": {
"type": "Transitive",
"resolved": "1.1.3",
"contentHash": "3Wrmi0kJDzClwAC+iBdUBpEKmEle8FQNsCs77fkiOIw/9oYA07bL1EZNX0kQ2OMN3xpwvl0vAtOCYY3ndDNlhQ=="
},
"System.Diagnostics.DiagnosticSource": {
"type": "Transitive",
"resolved": "7.0.0",
"contentHash": "9W0ewWDuAyDqS2PigdTxk6jDKonfgscY/hP8hm7VpxYhNHZHKvZTdRckberlFk3VnCmr3xBUyMBut12Q+T2aOw==",
"dependencies": {
"System.Runtime.CompilerServices.Unsafe": "6.0.0"
}
},
"System.Memory": {
"type": "Transitive",
"resolved": "4.5.5",
"contentHash": "XIWiDvKPXaTveaB7HVganDlOCRoj03l+jrwNvcge/t8vhGYKvqV+dMv6G4SAX2NoNmN0wZfVPTAlFwZcZvVOUw=="
},
"System.Runtime": {
"type": "Transitive",
"resolved": "4.3.1",
"contentHash": "abhfv1dTK6NXOmu4bgHIONxHyEqFjW8HwXPmpY9gmll+ix9UNo4XDcmzJn6oLooftxNssVHdJC1pGT9jkSynQg==",
"dependencies": {
"Microsoft.NETCore.Platforms": "1.1.1",
"Microsoft.NETCore.Targets": "1.1.3"
}
},
"System.Runtime.CompilerServices.Unsafe": {
"type": "Transitive",
"resolved": "6.0.0",
"contentHash": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg=="
},
"fantomas.core": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this required?

"type": "Project",
"dependencies": {
"FSharp.Core": "[6.0.1, )",
"Fantomas.FCS": "[1.0.0, )"
}
},
"fantomas.fcs": {
"type": "Project",
"dependencies": {
"FSharp.Core": "[6.0.1, )",
"System.Diagnostics.DiagnosticSource": "[7.0.0, )",
"System.Memory": "[4.5.5, )",
"System.Runtime": "[4.3.1, )"
}
}
}
}
Expand Down