From 3d9f9a2889aca53ea8d477d036002729ec851c89 Mon Sep 17 00:00:00 2001 From: George Thomas Date: Sun, 12 Jun 2022 15:59:49 +0200 Subject: [PATCH 1/3] Support Fourmolu 0.7 in CLI mode Fourmolu 0.7 (due to Ormolu 0.5) removes the `--cabal-default-extensions` flag, instead looking for Cabal files by default. If input is taken from stdin and `--stdin-input-file` isn't specified, it therefore fails. We don't need to make any changes in non-CLI mode because the Cabal file handling logic occurs at a higher level outside the entry point we use to the library (the `ormolu` function). --- plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs index d981c3b09f..c5cc358ba0 100644 --- a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs +++ b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs @@ -66,7 +66,7 @@ provider plId ideState typ contents fp fo = withIndefiniteProgress title Cancell (exitCode, out, err) <- readCreateProcessWithExitCode ( proc "fourmolu" $ - ["-d"] + ["-d", "--no-cabal"] <> catMaybes [ ("--start-line=" <>) . show <$> regionStartLine region , ("--end-line=" <>) . show <$> regionEndLine region From 0bbc545300eb014186e48f879394f9ca6d7ed3c8 Mon Sep 17 00:00:00 2001 From: George Thomas Date: Sun, 12 Jun 2022 16:47:06 +0200 Subject: [PATCH 2/3] Bump index state Allows us to use Fourmolu 0.7. --- cabal.project | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cabal.project b/cabal.project index 6a392d90ef..6ba4800c5a 100644 --- a/cabal.project +++ b/cabal.project @@ -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, From 581686c4729f9ce2dd6ca42a7842701ffa165d9a Mon Sep 17 00:00:00 2001 From: George Thomas Date: Mon, 13 Jun 2022 12:25:21 +0200 Subject: [PATCH 3/3] Only use Fourmolu `--no-cabal` flag on recent versions --- .../src/Ide/Plugin/Fourmolu.hs | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs index c5cc358ba0..d3d8c59cea 100644 --- a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs +++ b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs @@ -2,6 +2,7 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE DisambiguateRecordFields #-} {-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedLabels #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeApplications #-} @@ -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" + pure CLIVersionInfo + { noCabal = True + } + (exitCode, out, err) <- -- run Fourmolu readCreateProcessWithExitCode ( proc "fourmolu" $ - ["-d", "--no-cabal"] + ["-d"] + <> mwhen noCabal ["--no-cabal"] <> catMaybes [ ("--start-line=" <>) . show <$> regionStartLine region , ("--end-line=" <>) . show <$> regionEndLine region @@ -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