diff --git a/paket.lock b/paket.lock index ed29ed8a0..38477eb94 100644 --- a/paket.lock +++ b/paket.lock @@ -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) @@ -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)) @@ -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) @@ -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) diff --git a/src/FsAutoComplete.Core/Commands.fs b/src/FsAutoComplete.Core/Commands.fs index f57b25198..a0bfab16c 100644 --- a/src/FsAutoComplete.Core/Commands.fs +++ b/src/FsAutoComplete.Core/Commands.fs @@ -58,6 +58,7 @@ type Commands (serialize : Serializer, backgroundServiceEnabled) = let fileStateSet = Event() let commandsLogger = LogProvider.getLoggerByName "Commands" let checkerLogger = LogProvider.getLoggerByName "CheckerEvents" + let fantomasLogger = LogProvider.getLoggerByName "Fantomas" do state.ProjectController.NotifyWorkspace.Add (NotificationEvent.Workspace >> notify.Trigger) @@ -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 @@ -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 + } diff --git a/src/FsAutoComplete.Core/paket.references b/src/FsAutoComplete.Core/paket.references index 4e4249aa7..99e8123e3 100644 --- a/src/FsAutoComplete.Core/paket.references +++ b/src/FsAutoComplete.Core/paket.references @@ -13,5 +13,6 @@ FSharp.Data Dapper Microsoft.Data.Sqlite System.Configuration.ConfigurationManager +Fantomas Microsoft.NETFramework.ReferenceAssemblies diff --git a/src/FsAutoComplete/FsAutoComplete.Lsp.fs b/src/FsAutoComplete/FsAutoComplete.Lsp.fs index 9e975c6d0..166d57b74 100644 --- a/src/FsAutoComplete/FsAutoComplete.Lsp.fs +++ b/src/FsAutoComplete/FsAutoComplete.Lsp.fs @@ -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" @@ -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 @@ -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 }