Skip to content

Commit

Permalink
Move Fantomas formatting to Core project (#553)
Browse files Browse the repository at this point in the history
* Move Fantomas formatting to Core project

* Remove commented out code
  • Loading branch information
Krzysztof-Cieslak authored Feb 16, 2020
1 parent 0806de8 commit a0d3cbe
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 62 deletions.
12 changes: 6 additions & 6 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ NUGET
Microsoft.NETCore.Targets (>= 1.1) - restriction: || (&& (== net461) (< net46)) (== netcoreapp2.1) (== netstandard2.0)
System.Runtime (>= 4.3) - restriction: || (&& (== net461) (< net46)) (== netcoreapp2.1) (== netstandard2.0)
Microsoft.Win32.Registry (4.6) - restriction: || (== netcoreapp2.1) (== netstandard2.0)
System.Buffers (>= 4.5) - restriction: || (&& (== net461) (>= monoandroid)) (&& (== net461) (>= monotouch)) (&& (== net461) (< net46) (>= netstandard2.0)) (&& (== net461) (>= xamarinios)) (&& (== net461) (>= xamarinmac)) (&& (== net461) (>= xamarintvos)) (&& (== net461) (>= xamarinwatchos)) (&& (== netcoreapp2.1) (>= monoandroid) (< netstandard2.0)) (&& (== netcoreapp2.1) (>= monotouch)) (&& (== netcoreapp2.1) (< netcoreapp2.0)) (&& (== netcoreapp2.1) (>= xamarinios)) (&& (== netcoreapp2.1) (>= xamarinmac)) (&& (== netcoreapp2.1) (>= xamarintvos)) (&& (== netcoreapp2.1) (>= xamarinwatchos)) (== netstandard2.0)
System.Memory (>= 4.5.3) - restriction: || (&& (== net461) (< net46) (>= netstandard2.0)) (&& (== net461) (>= netcoreapp2.0)) (&& (== net461) (>= uap10.1)) (&& (== netcoreapp2.1) (< netcoreapp2.0)) (&& (== netcoreapp2.1) (>= uap10.1)) (== netstandard2.0)
System.Buffers (>= 4.5) - restriction: || (&& (== net461) (== netcoreapp2.1)) (&& (== net461) (>= monoandroid)) (&& (== net461) (>= monotouch)) (&& (== net461) (< net46) (>= netstandard2.0)) (&& (== net461) (>= xamarinios)) (&& (== net461) (>= xamarinmac)) (&& (== net461) (>= xamarintvos)) (&& (== net461) (>= xamarinwatchos)) (&& (== netcoreapp2.1) (>= monoandroid) (< netstandard2.0)) (&& (== netcoreapp2.1) (>= monotouch)) (&& (== netcoreapp2.1) (< netcoreapp2.0)) (&& (== netcoreapp2.1) (>= xamarinios)) (&& (== netcoreapp2.1) (>= xamarinmac)) (&& (== netcoreapp2.1) (>= xamarintvos)) (&& (== netcoreapp2.1) (>= xamarinwatchos)) (== netstandard2.0)
System.Memory (>= 4.5.3) - restriction: || (&& (== net461) (== netcoreapp2.1)) (&& (== net461) (< net46) (>= netstandard2.0)) (&& (== net461) (>= netcoreapp2.0)) (&& (== net461) (>= uap10.1)) (&& (== netcoreapp2.1) (< netcoreapp2.0)) (&& (== netcoreapp2.1) (>= uap10.1)) (== netstandard2.0)
System.Security.AccessControl (>= 4.6)
System.Security.Principal.Windows (>= 4.6)
Mono.Cecil (0.11)
Expand Down Expand Up @@ -149,7 +149,7 @@ NUGET
System.Threading.Timer (>= 4.0.1) - restriction: || (&& (== net461) (< net45)) (== netcoreapp2.1) (== netstandard2.0)
Sln (0.3)
SQLitePCLRaw.bundle_green (1.1.14)
NETStandard.Library (>= 1.6) - restriction: || (&& (== net461) (< net35)) (&& (== netcoreapp2.1) (< netcoreapp1.0)) (== netstandard2.0)
NETStandard.Library (>= 1.6) - restriction: || (&& (== net461) (== netcoreapp2.1)) (&& (== net461) (< net35)) (&& (== netcoreapp2.1) (< netcoreapp1.0)) (== netstandard2.0)
SQLitePCLRaw.core (>= 1.1.14)
SQLitePCLRaw.lib.e_sqlite3.linux (>= 1.1.14) - restriction: || (== net461) (== netcoreapp2.1) (&& (== netstandard2.0) (>= net35) (< portable-net40+sl5+win8+wp8) (< portable-net45+win8+wp8+wpa81)) (&& (== netstandard2.0) (>= net40) (< portable-net45+win8+wp8+wpa81)) (&& (== netstandard2.0) (>= net45)) (&& (== netstandard2.0) (>= netcoreapp1.0))
SQLitePCLRaw.lib.e_sqlite3.osx (>= 1.1.14) - restriction: || (== net461) (== netcoreapp2.1) (&& (== netstandard2.0) (>= net35) (< portable-net40+sl5+win8+wp8) (< portable-net45+win8+wp8+wpa81)) (&& (== netstandard2.0) (>= net40) (< portable-net45+win8+wp8+wpa81)) (&& (== netstandard2.0) (>= net45)) (&& (== netstandard2.0) (>= netcoreapp1.0)) (&& (== netstandard2.0) (>= xamarinmac))
Expand Down Expand Up @@ -275,10 +275,10 @@ NUGET
System.Reflection.Primitives (>= 4.3) - restriction: || (&& (== net461) (< net45)) (== netcoreapp2.1) (== netstandard2.0)
System.Runtime (>= 4.3) - restriction: || (&& (== net461) (< net45)) (== netcoreapp2.1) (== netstandard2.0)
System.Reflection.Emit (4.6) - restriction: || (== netcoreapp2.1) (== netstandard2.0)
System.Reflection.Emit.ILGeneration (>= 4.6) - restriction: || (&& (== net461) (< net45)) (&& (== net461) (< netstandard1.1)) (&& (== net461) (>= uap10.1)) (&& (== net461) (>= wpa81)) (&& (== netcoreapp2.1) (< netcoreapp2.0)) (&& (== netcoreapp2.1) (< netstandard1.1)) (&& (== netcoreapp2.1) (< netstandard2.0)) (&& (== netcoreapp2.1) (>= uap10.1)) (== netstandard2.0)
System.Reflection.Emit.ILGeneration (>= 4.6) - restriction: || (&& (== net461) (== netcoreapp2.1)) (&& (== net461) (< net45)) (&& (== net461) (< netstandard1.1)) (&& (== net461) (>= uap10.1)) (&& (== net461) (>= wpa81)) (&& (== netcoreapp2.1) (< netcoreapp2.0)) (&& (== netcoreapp2.1) (< netstandard1.1)) (&& (== netcoreapp2.1) (< netstandard2.0)) (&& (== netcoreapp2.1) (>= uap10.1)) (== netstandard2.0)
System.Reflection.Emit.ILGeneration (4.6) - restriction: || (&& (== net461) (== netcoreapp2.1)) (&& (== netcoreapp2.1) (< netcoreapp2.0)) (&& (== netcoreapp2.1) (< netstandard2.0)) (&& (== netcoreapp2.1) (< portable-net45+wp8)) (&& (== netcoreapp2.1) (>= uap10.1)) (== netstandard2.0)
System.Reflection.Emit.Lightweight (4.6) - restriction: || (== netcoreapp2.1) (== netstandard2.0)
System.Reflection.Emit.ILGeneration (>= 4.6) - restriction: || (&& (== net461) (< net45)) (&& (== net461) (< portable-net45+wp8)) (&& (== net461) (>= uap10.1)) (&& (== net461) (>= wpa81)) (&& (== netcoreapp2.1) (< netcoreapp2.0)) (&& (== netcoreapp2.1) (< netstandard2.0)) (&& (== netcoreapp2.1) (< portable-net45+wp8)) (&& (== netcoreapp2.1) (>= uap10.1)) (== netstandard2.0)
System.Reflection.Emit.ILGeneration (>= 4.6) - restriction: || (&& (== net461) (== netcoreapp2.1)) (&& (== net461) (< net45)) (&& (== net461) (< portable-net45+wp8)) (&& (== net461) (>= uap10.1)) (&& (== net461) (>= wpa81)) (&& (== netcoreapp2.1) (< netcoreapp2.0)) (&& (== netcoreapp2.1) (< netstandard2.0)) (&& (== netcoreapp2.1) (< portable-net45+wp8)) (&& (== netcoreapp2.1) (>= uap10.1)) (== netstandard2.0)
System.Reflection.Extensions (4.3) - restriction: || (&& (== net461) (>= netcoreapp1.1)) (== netcoreapp2.1) (&& (== netstandard2.0) (>= netcoreapp1.1))
Microsoft.NETCore.Platforms (>= 1.1) - restriction: || (&& (== net461) (< net45)) (== netcoreapp2.1) (== netstandard2.0)
Microsoft.NETCore.Targets (>= 1.1) - restriction: || (&& (== net461) (< net45)) (== netcoreapp2.1) (== netstandard2.0)
Expand Down Expand Up @@ -373,7 +373,7 @@ NUGET
System.Threading (>= 4.3) - restriction: || (&& (== net461) (< net46)) (== netcoreapp2.1) (== netstandard2.0)
System.Threading.Tasks (>= 4.3) - restriction: || (&& (== net461) (< net46)) (== netcoreapp2.1) (== netstandard2.0)
System.Security.Cryptography.ProtectedData (4.6) - restriction: || (== netcoreapp2.1) (== netstandard2.0)
System.Memory (>= 4.5.3) - restriction: || (&& (== net461) (< net46) (>= netstandard2.0)) (== netstandard2.0)
System.Memory (>= 4.5.3) - restriction: || (&& (== net461) (== netcoreapp2.1)) (&& (== net461) (< net46) (>= netstandard2.0)) (== netstandard2.0)
System.Security.Permissions (4.6)
System.Security.AccessControl (>= 4.6)
System.Security.Principal.Windows (4.6)
Expand Down
39 changes: 37 additions & 2 deletions src/FsAutoComplete.Core/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Commands (serialize : Serializer, backgroundServiceEnabled) =
let fileStateSet = Event<unit>()
let commandsLogger = LogProvider.getLoggerByName "Commands"
let checkerLogger = LogProvider.getLoggerByName "CheckerEvents"
let fantomasLogger = LogProvider.getLoggerByName "Fantomas"

do state.ProjectController.NotifyWorkspace.Add (NotificationEvent.Workspace >> notify.Trigger)

Expand Down Expand Up @@ -930,8 +931,6 @@ type Commands (serialize : Serializer, backgroundServiceEnabled) =
return CoreResponse.Res runtimePath
}

member x.GetChecker () = checker.GetFSharpChecker()

member x.ScopesForFile (file: string) = async {
let file = Path.GetFullPath file
match state.TryGetFileCheckerOptionsWithLines file with
Expand All @@ -949,3 +948,39 @@ type Commands (serialize : Serializer, backgroundServiceEnabled) =

member __.SetDotnetSDKRoot(path) = checker.SetDotnetRoot(path)
member __.SetFSIAdditionalArguments args = checker.SetFSIAdditionalArguments args

member x.FormatDocument (file: SourceFilePath) = async {
let file = Path.GetFullPath file

match x.TryGetFileCheckerOptionsWithLines file with
| Result.Ok (opts, lines) ->
let source = String.concat "\n" lines
let parsingOptions = Utils.projectOptionsToParseOptions opts
let checker : FSharpChecker = checker.GetFSharpChecker()
// ENHANCEMENT: consider caching the Fantomas configuration and reevaluate when the configuration file changes.
let config =
let currentFolder = Path.GetDirectoryName(file)
let result = Fantomas.CodeFormatter.ReadConfiguration currentFolder
match result with
| Fantomas.FormatConfig.Success c -> c
| Fantomas.FormatConfig.PartialSuccess(c,warnings) ->
match warnings with
| [] ->
c
| warnings ->
fantomasLogger.warn (Log.setMessage "Warnings while parsing the configuration file at {path}" >> Log.addContextDestructured "path" currentFolder >> Log.addContextDestructured "warnings" warnings)
c
| Fantomas.FormatConfig.Failure err ->
fantomasLogger.error (Log.setMessage "Error while parsing the configuration files at {path}. Using default configuration" >> Log.addContextDestructured "path" currentFolder >> Log.addExn err)
Fantomas.FormatConfig.FormatConfig.Default

let! formatted =
Fantomas.CodeFormatter.FormatDocumentAsync(file,
Fantomas.SourceOrigin.SourceString source,
config,
parsingOptions,
checker)
return Some (lines, formatted)
| Result.Error er ->
return None
}
1 change: 1 addition & 0 deletions src/FsAutoComplete.Core/paket.references
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ FSharp.Data
Dapper
Microsoft.Data.Sqlite
System.Configuration.ConfigurationManager
Fantomas

Microsoft.NETFramework.ReferenceAssemblies
59 changes: 5 additions & 54 deletions src/FsAutoComplete/FsAutoComplete.Lsp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ type FsharpLspServer(commands: Commands, lspClient: FSharpLspClient) =
LspResult.internalError msg
| CoreResponse.Res(tip, signature, footer, typeDoc) ->
match TipFormatter.formatTipEnhanced tip signature footer typeDoc with
| (sigCommentFooter::_)::_ ->
| (sigCommentFooter::_)::_ ->
let signature, comment, footer = sigCommentFooter
let markStr lang (value:string) = MarkedString.WithLanguage { Language = lang ; Value = value }
let fsharpBlock (lines: string[]) = lines |> String.concat "\n" |> markStr "fsharp"
Expand Down Expand Up @@ -919,8 +919,9 @@ type FsharpLspServer(commands: Commands, lspClient: FSharpLspClient) =
override __.TextDocumentFormatting(p: DocumentFormattingParams) = async {
let doc = p.TextDocument
let fileName = doc.GetFilePath()
match commands.TryGetFileCheckerOptionsWithLines fileName with
| Result.Ok (opts, lines) ->
let! res = commands.FormatDocument fileName
match res with
| Some (lines, formatted) ->
let range =
let zero = { Line = 0; Character = 0 }
let endLine = Array.length lines - 1
Expand All @@ -930,58 +931,8 @@ type FsharpLspServer(commands: Commands, lspClient: FSharpLspClient) =
|> Option.defaultValue 0
{ Start = zero; End = { Line = endLine; Character = endCharacter } }

let source = String.concat "\n" lines
let parsingOptions = Utils.projectOptionsToParseOptions opts
let checker : FSharpChecker = commands.GetChecker()
// ENHANCEMENT: consider caching the Fantomas configuration and reevaluate when the configuration file changes.
let config =
let currentFolder = System.IO.Path.GetDirectoryName(fileName)
let result = Fantomas.CodeFormatter.ReadConfiguration currentFolder
match result with
| Fantomas.FormatConfig.Success c -> c
| Fantomas.FormatConfig.PartialSuccess(c,warnings) ->
match warnings with
| [] ->
c
| warnings ->
fantomasLogger.warn (Log.setMessage "Warnings while parsing the configuration file at {path}" >> Log.addContextDestructured "path" currentFolder >> Log.addContextDestructured "warnings" warnings)
c
| Fantomas.FormatConfig.Failure err ->
fantomasLogger.error (Log.setMessage "Error while parsing the configuration files at {path}. Using default configuration" >> Log.addContextDestructured "path" currentFolder >> Log.addExn err)
Fantomas.FormatConfig.FormatConfig.Default

let! formatted =
Fantomas.CodeFormatter.FormatDocumentAsync(fileName,
Fantomas.SourceOrigin.SourceString source,
config,
parsingOptions,
checker)

return LspResult.success(Some([| { Range = range; NewText = formatted } |]))
| Result.Error er ->
return LspResult.notImplemented
}

override __.TextDocumentRangeFormatting(p) = async {
let doc = p.TextDocument
let fileName = doc.GetFilePath()
match commands.TryGetFileCheckerOptionsWithLines fileName with
| Result.Ok (opts, lines) ->
let range = Fantomas.CodeFormatter.MakeRange(fileName, (p.Range.Start.Line + 1), (p.Range.Start.Character + 1), (p.Range.End.Line + 1), (p.Range.End.Character + 1))

let source = String.concat "\n" lines
let parsingOptions = Utils.projectOptionsToParseOptions opts
let checker : FSharpChecker = commands.GetChecker()
let! formatted =
Fantomas.CodeFormatter.FormatSelectionAsync(fileName,
range,
Fantomas.SourceOrigin.SourceString source,
Fantomas.FormatConfig.FormatConfig.Default,
parsingOptions,
checker)

return LspResult.success(Some([| { Range = p.Range; NewText = formatted } |]))
| Result.Error er ->
| None ->
return LspResult.notImplemented
}

Expand Down

0 comments on commit a0d3cbe

Please sign in to comment.