From 5bec77b81d346be9e912b3196c31443ceb257b6f Mon Sep 17 00:00:00 2001 From: baronfel Date: Wed, 4 Aug 2021 09:37:54 -0500 Subject: [PATCH] make handling fantomas errors a bit safer by explicitly using the old version of the formatted code --- src/FsAutoComplete.Core/Commands.fs | 10 +++++++--- src/FsAutoComplete/FsAutoComplete.Lsp.fs | 6 +++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/FsAutoComplete.Core/Commands.fs b/src/FsAutoComplete.Core/Commands.fs index 21bdfaa5c..1a0478508 100644 --- a/src/FsAutoComplete.Core/Commands.fs +++ b/src/FsAutoComplete.Core/Commands.fs @@ -1149,10 +1149,14 @@ type Commands (checker: FSharpCompilerServiceChecker, state: State, backgroundSe | None -> fantomasLogger.warn (Log.setMessage "No fantomas configuration found for file '{filePath}' or parent directories. Using the default configuration." >> Log.addContextDestructured "filePath" file) Fantomas.FormatConfig.FormatConfig.Default - let! formatted = Fantomas.CodeFormatter.FormatDocumentAsync(UMX.untag file, Fantomas.SourceOrigin.SourceText text, config, parsingOptions, checker) - return text, formatted + try + let! formatted = Fantomas.CodeFormatter.FormatDocumentAsync(UMX.untag file, Fantomas.SourceOrigin.SourceText text, config, parsingOptions, checker) + return text, formatted + with + | ex -> + fantomasLogger.warn (Log.setMessage "Errors while formatting file, defaulting to previous content. Error message was {message}" >> Log.addContextDestructured "message" ex.Message >> Log.addExn ex) + return! Core.Error ex.Message } - |> AsyncResult.foldResult Some (fun _ -> None) /// gets the semantic classification ranges for a file, optionally filtered by a given range. member x.GetHighlighting (file: string, range: Range option) = diff --git a/src/FsAutoComplete/FsAutoComplete.Lsp.fs b/src/FsAutoComplete/FsAutoComplete.Lsp.fs index ba787e136..ca5197a23 100644 --- a/src/FsAutoComplete/FsAutoComplete.Lsp.fs +++ b/src/FsAutoComplete/FsAutoComplete.Lsp.fs @@ -1186,15 +1186,15 @@ type FSharpLspServer(backgroundServiceEnabled: bool, state: State, lspClient: FS let fileName = doc.GetFilePath() |> Utils.normalizePath let! res = commands.FormatDocument fileName match res with - | Some (lines, formatted) -> + | Ok (lines, formatted) -> let range = let zero = { Line = 0; Character = 0 } let lastPos = lines.GetLastFilePosition() { Start = zero; End = fcsPosToLsp lastPos } return LspResult.success(Some([| { Range = range; NewText = formatted } |])) - | None -> - return LspResult.notImplemented + | Error ex -> + return LspResult.internalError ex } member private x.HandleTypeCheckCodeAction file pos f =