From 663c0e7571535330b972609f092cbacfb1e98e77 Mon Sep 17 00:00:00 2001 From: fendor Date: Sat, 26 Aug 2023 00:23:35 +0200 Subject: [PATCH] Use the file contents from the LSP request (#3776) --- .../hls-cabal-fmt-plugin.cabal | 2 +- .../src/Ide/Plugin/CabalFmt.hs | 19 ++++++++++--------- plugins/hls-cabal-fmt-plugin/test/Main.hs | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/plugins/hls-cabal-fmt-plugin/hls-cabal-fmt-plugin.cabal b/plugins/hls-cabal-fmt-plugin/hls-cabal-fmt-plugin.cabal index fe47b12089..132e51d187 100644 --- a/plugins/hls-cabal-fmt-plugin/hls-cabal-fmt-plugin.cabal +++ b/plugins/hls-cabal-fmt-plugin/hls-cabal-fmt-plugin.cabal @@ -38,7 +38,7 @@ library , lens , lsp-types , mtl - , process + , process-extras , text , transformers diff --git a/plugins/hls-cabal-fmt-plugin/src/Ide/Plugin/CabalFmt.hs b/plugins/hls-cabal-fmt-plugin/src/Ide/Plugin/CabalFmt.hs index a3250ba119..d51c25678a 100644 --- a/plugins/hls-cabal-fmt-plugin/src/Ide/Plugin/CabalFmt.hs +++ b/plugins/hls-cabal-fmt-plugin/src/Ide/Plugin/CabalFmt.hs @@ -11,28 +11,29 @@ import Development.IDE hiding (pluginHandlers) import Ide.Plugin.Error (PluginError (PluginInternalError, PluginInvalidParams)) import Ide.PluginUtils import Ide.Types -import Language.LSP.Protocol.Lens as L +import qualified Language.LSP.Protocol.Lens as L import Language.LSP.Protocol.Types import Prelude hiding (log) import System.Directory import System.Exit import System.FilePath -import System.Process +import System.Process.ListLike +import qualified System.Process.Text as Process data Log = LogProcessInvocationFailure Int - | LogReadCreateProcessInfo String [String] + | LogReadCreateProcessInfo T.Text [String] | LogInvalidInvocationInfo | LogCabalFmtNotFound deriving (Show) instance Pretty Log where pretty = \case - LogProcessInvocationFailure code -> "Invocation of cabal-fmt failed with code" <+> pretty code + LogProcessInvocationFailure exitCode -> "Invocation of cabal-fmt failed with code" <+> pretty exitCode LogReadCreateProcessInfo stdErrorOut args -> vcat $ ["Invocation of cabal-fmt with arguments" <+> pretty args] - ++ ["failed with standard error:" <+> pretty stdErrorOut | not (null stdErrorOut)] + ++ ["failed with standard error:" <+> pretty stdErrorOut | not (T.null stdErrorOut)] LogInvalidInvocationInfo -> "Invocation of cabal-fmt with range was called but is not supported." LogCabalFmtNotFound -> "Couldn't find executable 'cabal-fmt'" @@ -50,24 +51,24 @@ provider recorder _ (FormatRange _) _ _ _ = do logWith recorder Info LogInvalidInvocationInfo throwError $ PluginInvalidParams "You cannot format a text-range using cabal-fmt." provider recorder _ide FormatText contents nfp opts = do - let cabalFmtArgs = [fp, "--indent", show tabularSize] + let cabalFmtArgs = [ "--indent", show tabularSize] x <- liftIO $ findExecutable "cabal-fmt" case x of Just _ -> do (exitCode, out, err) <- - liftIO $ readCreateProcessWithExitCode + liftIO $ Process.readCreateProcessWithExitCode ( proc "cabal-fmt" cabalFmtArgs ) { cwd = Just $ takeDirectory fp } - "" + contents log Debug $ LogReadCreateProcessInfo err cabalFmtArgs case exitCode of ExitFailure code -> do log Error $ LogProcessInvocationFailure code throwError (PluginInternalError "Failed to invoke cabal-fmt") ExitSuccess -> do - let fmtDiff = makeDiffTextEdit contents (T.pack out) + let fmtDiff = makeDiffTextEdit contents out pure $ InL fmtDiff Nothing -> do log Error LogCabalFmtNotFound diff --git a/plugins/hls-cabal-fmt-plugin/test/Main.hs b/plugins/hls-cabal-fmt-plugin/test/Main.hs index 4aa567ac4e..d2e0b9c0f1 100644 --- a/plugins/hls-cabal-fmt-plugin/test/Main.hs +++ b/plugins/hls-cabal-fmt-plugin/test/Main.hs @@ -39,7 +39,7 @@ tests found = testGroup "cabal-fmt" cabalFmtGolden found "formats a simple document" "simple_testdata" "formatted_document" $ \doc -> do formatDoc doc (FormattingOptions 2 True Nothing Nothing Nothing) - , knownBrokenOnWindows "expand:src comment bug in cabal-fmt on windows" $ + , expectFailBecause "cabal-fmt can't expand modules if .cabal file is read from stdin. Tracking issue: https://github.com/phadej/cabal-fmt/pull/82" $ cabalFmtGolden found "formats a document with expand:src comment" "commented_testdata" "formatted_document" $ \doc -> do formatDoc doc (FormattingOptions 2 True Nothing Nothing Nothing)