Skip to content

hls-cabal-fmt-plugin: Use the file contents of the LSP request #3776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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