From 966fb2ed5cbf399c0e4b9396ed9b89ef594977e0 Mon Sep 17 00:00:00 2001 From: Brandon Chinn Date: Thu, 7 Sep 2023 20:59:04 -0700 Subject: [PATCH 1/5] Remove unneeded support for GHC 8.10 --- plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal | 4 +--- plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs | 5 ----- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal b/plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal index a7b903ccc1..375535e229 100644 --- a/plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal +++ b/plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal @@ -42,9 +42,7 @@ library , text , transformers - if impl(ghc >= 8.10) && impl(ghc < 9.0) - build-depends: fourmolu ^>= 0.9 - elif impl(ghc >= 9.0) && impl(ghc < 9.2) + if impl(ghc >= 9.0) && impl(ghc < 9.2) build-depends: fourmolu ^>= 0.11 elif impl(ghc >= 9.2) && impl(ghc < 9.8) build-depends: fourmolu ^>= 0.13 diff --git a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs index 7ee1178465..39e7edcb83 100644 --- a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs +++ b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs @@ -1,4 +1,3 @@ -{-# LANGUAGE CPP #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DisambiguateRecordFields #-} {-# LANGUAGE LambdaCase #-} @@ -79,11 +78,7 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro logWith recorder Debug $ LogCompiledInVersion VERSION_fourmolu let format fourmoluConfig = ExceptT $ bimap (PluginInternalError . T.pack . show) (InL . makeDiffTextEdit contents) -#if MIN_VERSION_fourmolu(0,11,0) <$> try @OrmoluException (ormolu config fp' contents) -#else - <$> try @OrmoluException (ormolu config fp' (T.unpack contents)) -#endif where printerOpts = cfgFilePrinterOpts fourmoluConfig config = From 53f53948b5f3389c86d962d76d54ccf747fe7c4c Mon Sep 17 00:00:00 2001 From: Brandon Chinn Date: Wed, 6 Sep 2023 21:29:08 -0700 Subject: [PATCH 2/5] Add support for Fourmolu 0.14.0.0 --- cabal.project | 2 +- plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal | 2 +- .../hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs | 11 +++++++---- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cabal.project b/cabal.project index 0c6bfba565..ce0bd46ef6 100644 --- a/cabal.project +++ b/cabal.project @@ -52,7 +52,7 @@ package * write-ghc-environment-files: never -index-state: 2023-08-25T00:00:00Z +index-state: 2023-09-08T00:00:00Z constraints: -- For GHC 9.4, older versions of entropy fail to build on Windows diff --git a/plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal b/plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal index 375535e229..53ce82fa26 100644 --- a/plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal +++ b/plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal @@ -45,7 +45,7 @@ library if impl(ghc >= 9.0) && impl(ghc < 9.2) build-depends: fourmolu ^>= 0.11 elif impl(ghc >= 9.2) && impl(ghc < 9.8) - build-depends: fourmolu ^>= 0.13 + build-depends: fourmolu ^>= 0.14 else buildable: false diff --git a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs index 39e7edcb83..e1b1cc5262 100644 --- a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs +++ b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DisambiguateRecordFields #-} {-# LANGUAGE LambdaCase #-} @@ -87,10 +88,7 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro , cfgFixityOverrides = cfgFileFixities fourmoluConfig , cfgRegion = region , cfgDebug = False - , cfgPrinterOpts = - fillMissingPrinterOpts - (printerOpts <> lspPrinterOpts) - defaultPrinterOpts + , cfgPrinterOpts = resolvePrinterOpts [lspPrinterOpts, printerOpts] } in liftIO (loadConfigFile fp') >>= \case ConfigLoaded file opts -> do @@ -195,3 +193,8 @@ newtype CLIVersionInfo = CLIVersionInfo mwhen :: Monoid a => Bool -> a -> a mwhen b x = if b then x else mempty + +#if !MIN_VERSION_fourmolu(0,14,0) +resolvePrinterOpts :: [PrinterOptsPartial] -> PrinterOptsTotal +resolvePrinterOpts = foldr fillMissingPrinterOpts defaultPrinterOpts +#endif From c80d5162013cacf68500a51165cb0b84b938c2b5 Mon Sep 17 00:00:00 2001 From: Brandon Chinn Date: Wed, 6 Sep 2023 21:44:21 -0700 Subject: [PATCH 3/5] Break out format call --- plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs index e1b1cc5262..574afe7d70 100644 --- a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs +++ b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs @@ -90,13 +90,14 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro , cfgDebug = False , cfgPrinterOpts = resolvePrinterOpts [lspPrinterOpts, printerOpts] } - in liftIO (loadConfigFile fp') >>= \case + cfg <- + liftIO (loadConfigFile fp') >>= \case ConfigLoaded file opts -> do logWith recorder Info $ ConfigPath file - mapExceptT liftIO $ format opts + pure opts ConfigNotFound searchDirs -> do logWith recorder Info $ NoConfigPath searchDirs - mapExceptT liftIO $ format emptyConfig + pure emptyConfig ConfigParseError f err -> do lift $ sendNotification SMethod_WindowShowMessage $ ShowMessageParams @@ -106,6 +107,8 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro throwError $ PluginInternalError errorMessage where errorMessage = "Failed to load " <> T.pack f <> ": " <> T.pack (show err) + + mapExceptT liftIO $ format cfg where fp' = fromNormalizedFilePath fp title = "Formatting " <> T.pack (takeFileName fp') From 817708f6531beb490d12a8a5f79e900eb80f80d8 Mon Sep 17 00:00:00 2001 From: Brandon Chinn Date: Wed, 6 Sep 2023 21:47:23 -0700 Subject: [PATCH 4/5] Inline format call --- .../src/Ide/Plugin/Fourmolu.hs | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs index 574afe7d70..1737673bf4 100644 --- a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs +++ b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs @@ -5,6 +5,7 @@ {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedLabels #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeOperators #-} @@ -77,20 +78,7 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro $ runExceptT $ cliHandler fileOpts else do logWith recorder Debug $ LogCompiledInVersion VERSION_fourmolu - let format fourmoluConfig = ExceptT $ - bimap (PluginInternalError . T.pack . show) (InL . makeDiffTextEdit contents) - <$> try @OrmoluException (ormolu config fp' contents) - where - printerOpts = cfgFilePrinterOpts fourmoluConfig - config = - defaultConfig - { cfgDynOptions = map DynOption fileOpts - , cfgFixityOverrides = cfgFileFixities fourmoluConfig - , cfgRegion = region - , cfgDebug = False - , cfgPrinterOpts = resolvePrinterOpts [lspPrinterOpts, printerOpts] - } - cfg <- + FourmoluConfig{..} <- liftIO (loadConfigFile fp') >>= \case ConfigLoaded file opts -> do logWith recorder Info $ ConfigPath file @@ -108,7 +96,17 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro where errorMessage = "Failed to load " <> T.pack f <> ": " <> T.pack (show err) - mapExceptT liftIO $ format cfg + let config = + defaultConfig + { cfgDynOptions = map DynOption fileOpts + , cfgFixityOverrides = cfgFileFixities + , cfgRegion = region + , cfgDebug = False + , cfgPrinterOpts = resolvePrinterOpts [lspPrinterOpts, cfgFilePrinterOpts] + } + mapExceptT liftIO $ ExceptT $ + bimap (PluginInternalError . T.pack . show) (InL . makeDiffTextEdit contents) + <$> try @OrmoluException (ormolu config fp' contents) where fp' = fromNormalizedFilePath fp title = "Formatting " <> T.pack (takeFileName fp') From c655372d7367a2df1f4c7cb8b9756b14e91814ef Mon Sep 17 00:00:00 2001 From: Brandon Chinn Date: Wed, 6 Sep 2023 21:50:24 -0700 Subject: [PATCH 5/5] mapExceptT liftIO . ExceptT => ExceptT . liftIO --- plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs index 1737673bf4..68fd1b2017 100644 --- a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs +++ b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs @@ -72,10 +72,9 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro <$> liftIO (runAction "Fourmolu" ideState $ use GhcSession fp) useCLI <- liftIO $ runAction "Fourmolu" ideState $ usePropertyAction #external plId properties if useCLI - then mapExceptT liftIO $ ExceptT - $ handle @IOException - (pure . Left . PluginInternalError . T.pack . show) - $ runExceptT $ cliHandler fileOpts + then ExceptT . liftIO $ + handle @IOException (pure . Left . PluginInternalError . T.pack . show) $ + runExceptT (cliHandler fileOpts) else do logWith recorder Debug $ LogCompiledInVersion VERSION_fourmolu FourmoluConfig{..} <- @@ -104,7 +103,7 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro , cfgDebug = False , cfgPrinterOpts = resolvePrinterOpts [lspPrinterOpts, cfgFilePrinterOpts] } - mapExceptT liftIO $ ExceptT $ + ExceptT . liftIO $ bimap (PluginInternalError . T.pack . show) (InL . makeDiffTextEdit contents) <$> try @OrmoluException (ormolu config fp' contents) where