Skip to content

Fix Fourmolu 0.7 support #2950

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 3 commits into from
Jun 13, 2022
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 cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ package *

write-ghc-environment-files: never

index-state: 2022-04-30T21:02:45Z
index-state: 2022-06-12T00:00:00Z

constraints:
hyphenation +embed,
Expand Down
26 changes: 25 additions & 1 deletion plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
Expand Down Expand Up @@ -63,10 +64,26 @@ provider plId ideState typ contents fp fo = withIndefiniteProgress title Cancell
. fmap (join . first (mkError . show))
. try @IOException
$ do
(exitCode, out, err) <-
CLIVersionInfo{noCabal} <- do -- check Fourmolu version so that we know which flags to use
(exitCode, out, _err) <- readCreateProcessWithExitCode ( proc "fourmolu" ["-v"] ) ""
let version = do
guard $ exitCode == ExitSuccess
"fourmolu" : v : _ <- pure $ T.words out
pure $ T.splitOn "." v
case version of
Just v -> pure CLIVersionInfo
{ noCabal = v >= ["0", "7"]
}
Nothing -> do
T.hPutStrLn stderr "couldn't get Fourmolu version"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is bad error reporting, but I'm about to revamp the logging for the whole plugin anyway, since I've been told off by @fendor for printing directly to stdout!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pure CLIVersionInfo
{ noCabal = True
}
(exitCode, out, err) <- -- run Fourmolu
readCreateProcessWithExitCode
( proc "fourmolu" $
["-d"]
<> mwhen noCabal ["--no-cabal"]
<> catMaybes
[ ("--start-line=" <>) . show <$> regionStartLine region
, ("--end-line=" <>) . show <$> regionEndLine region
Expand Down Expand Up @@ -163,3 +180,10 @@ convertDynFlags df =
Cpp -> "-XCPP"
x -> "-X" ++ show x
in pp <> pm <> ex

newtype CLIVersionInfo = CLIVersionInfo
{ noCabal :: Bool
}

mwhen :: Monoid a => Bool -> a -> a
mwhen b x = if b then x else mempty