Skip to content

Commit

Permalink
Use the file contents from the LSP request (#3776)
Browse files Browse the repository at this point in the history
  • Loading branch information
fendor authored Aug 25, 2023
1 parent e4234a3 commit 663c0e7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion plugins/hls-cabal-fmt-plugin/hls-cabal-fmt-plugin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ library
, lens
, lsp-types
, mtl
, process
, process-extras
, text
, transformers

Expand Down
19 changes: 10 additions & 9 deletions plugins/hls-cabal-fmt-plugin/src/Ide/Plugin/CabalFmt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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'"

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plugins/hls-cabal-fmt-plugin/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 663c0e7

Please sign in to comment.