From 1ac00019f4b651595a5a955985cd6b93b6d1f079 Mon Sep 17 00:00:00 2001 From: Lei Zhu Date: Sat, 5 Aug 2023 14:22:20 +0800 Subject: [PATCH 1/5] Log fourmolu version --- .../src/Ide/Plugin/Fourmolu.hs | 16 +++++++++++++--- 1 file changed, 13 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 b6b46f2426..113c962a3e 100644 --- a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs +++ b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs @@ -24,9 +24,11 @@ import Control.Monad.Trans.Except (ExceptT (..), mapExceptT, import Control.Monad.IO.Class (MonadIO (liftIO)) import Control.Monad.Trans.Class (MonadTrans (lift)) import Data.Bifunctor (bimap) +import Data.List (intercalate) import Data.Maybe (catMaybes) import Data.Text (Text) import qualified Data.Text as T +import Data.Version (Version, showVersion) import Development.IDE hiding (pluginHandlers) import Development.IDE.GHC.Compat as Compat hiding (Cpp, Warning, hang, vcat) @@ -42,6 +44,7 @@ import Language.LSP.Protocol.Message import Language.LSP.Protocol.Types import Language.LSP.Server hiding (defaultConfig) import Ormolu +import qualified Paths_fourmolu as Fourmolu import System.Exit import System.FilePath import System.Process.Run (cwd, proc) @@ -75,6 +78,7 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro (pure . Left . PluginInternalError . T.pack . show) $ runExceptT $ cliHandler fileOpts else do + logWith recorder Info $ LogBuiltinVersion Fourmolu.version let format fourmoluConfig = ExceptT $ bimap (PluginInternalError . T.pack . show) (InL . makeDiffTextEdit contents) #if MIN_VERSION_fourmolu(0,11,0) @@ -129,9 +133,11 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro "fourmolu" : v : _ <- pure $ T.words out traverse (readMaybe @Int . T.unpack) $ T.splitOn "." v case version of - Just v -> pure CLIVersionInfo - { noCabal = v >= [0, 7] - } + Just v -> do + logWith recorder Info $ LogExternalVersion v + pure CLIVersionInfo + { noCabal = v >= [0, 7] + } Nothing -> do logWith recorder Warning $ NoVersion out pure CLIVersionInfo @@ -161,6 +167,8 @@ data LogEvent | ConfigPath FilePath | NoConfigPath [FilePath] | StdErr Text + | LogBuiltinVersion Version + | LogExternalVersion [Int] deriving (Show) instance Pretty LogEvent where @@ -170,6 +178,8 @@ instance Pretty LogEvent where NoConfigPath ps -> "No " <> pretty configFileName <> " found in any of:" <> line <> indent 2 (vsep (map (pretty . show) ps)) StdErr t -> "Fourmolu stderr:" <> line <> indent 2 (pretty t) + LogBuiltinVersion v -> "Using builtin fourmolu-" <> pretty (showVersion v) + LogExternalVersion v -> "Using external fourmolu-" <> pretty (intercalate "." $ map show v) convertDynFlags :: DynFlags -> [String] convertDynFlags df = From 44a8123ca31364b1d7130058c3ab9bc47ab23c8a Mon Sep 17 00:00:00 2001 From: Lei Zhu Date: Sat, 5 Aug 2023 14:34:00 +0800 Subject: [PATCH 2/5] Log fourmolu version --- .../hls-fourmolu-plugin.cabal | 2 +- .../src/Ide/Plugin/Fourmolu.hs | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal b/plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal index b0910a071c..c1b4ed1974 100644 --- a/plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal +++ b/plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal @@ -32,7 +32,7 @@ library build-depends: , base >=4.12 && <5 , filepath - , fourmolu ^>=0.3 || ^>=0.4 || ^>= 0.6 || ^>= 0.7 || ^>= 0.8 || ^>= 0.9 || ^>= 0.10 || ^>= 0.11 || ^>= 0.12 + , fourmolu ^>=0.3 || ^>=0.4 || ^>= 0.6 || ^>= 0.7 || ^>= 0.8 || ^>= 0.9 || ^>= 0.10 || ^>= 0.11 || ^>= 0.12 || ^>= 0.13 , ghc , ghc-boot-th , ghcide == 2.1.0.0 diff --git a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs index 113c962a3e..6197faf303 100644 --- a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs +++ b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs @@ -44,13 +44,16 @@ import Language.LSP.Protocol.Message import Language.LSP.Protocol.Types import Language.LSP.Server hiding (defaultConfig) import Ormolu -import qualified Paths_fourmolu as Fourmolu import System.Exit import System.FilePath import System.Process.Run (cwd, proc) import System.Process.Text (readCreateProcessWithExitCode) import Text.Read (readMaybe) +#if MIN_VERSION_fourmolu(0,12,0) +import qualified Paths_fourmolu as Fourmolu +#endif + descriptor :: Recorder (WithPriority LogEvent) -> PluginId -> PluginDescriptor IdeState descriptor recorder plId = (defaultPluginDescriptor plId) @@ -78,7 +81,11 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro (pure . Left . PluginInternalError . T.pack . show) $ runExceptT $ cliHandler fileOpts else do - logWith recorder Info $ LogBuiltinVersion Fourmolu.version +#if MIN_VERSION_fourmolu(0,12,0) + logWith recorder Info $ LogBuiltinVersion $ Just Fourmolu.version +#else + logWith recorder Info $ LogBuiltinVersion Nothing +#endif let format fourmoluConfig = ExceptT $ bimap (PluginInternalError . T.pack . show) (InL . makeDiffTextEdit contents) #if MIN_VERSION_fourmolu(0,11,0) @@ -139,6 +146,7 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro { noCabal = v >= [0, 7] } Nothing -> do + logWith recorder Info $ LogExternalVersion [] logWith recorder Warning $ NoVersion out pure CLIVersionInfo { noCabal = True @@ -167,7 +175,7 @@ data LogEvent | ConfigPath FilePath | NoConfigPath [FilePath] | StdErr Text - | LogBuiltinVersion Version + | LogBuiltinVersion (Maybe Version) | LogExternalVersion [Int] deriving (Show) @@ -178,8 +186,8 @@ instance Pretty LogEvent where NoConfigPath ps -> "No " <> pretty configFileName <> " found in any of:" <> line <> indent 2 (vsep (map (pretty . show) ps)) StdErr t -> "Fourmolu stderr:" <> line <> indent 2 (pretty t) - LogBuiltinVersion v -> "Using builtin fourmolu-" <> pretty (showVersion v) - LogExternalVersion v -> "Using external fourmolu-" <> pretty (intercalate "." $ map show v) + LogBuiltinVersion v -> "Using builtin fourmolu" <> pretty (maybe "" showVersion v) + LogExternalVersion v -> "Using external fourmolu" <> pretty (intercalate "." $ map show v) convertDynFlags :: DynFlags -> [String] convertDynFlags df = From b6e90d0076e1c6dfdd74553fcf02201b980a9559 Mon Sep 17 00:00:00 2001 From: Lei Zhu Date: Sat, 5 Aug 2023 22:52:31 +0800 Subject: [PATCH 3/5] Remove redundant CPP --- .../src/Ide/Plugin/Fourmolu.hs | 24 +++++++------------ 1 file changed, 9 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 6197faf303..3ae5baf1bc 100644 --- a/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs +++ b/plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs @@ -28,7 +28,6 @@ import Data.List (intercalate) import Data.Maybe (catMaybes) import Data.Text (Text) import qualified Data.Text as T -import Data.Version (Version, showVersion) import Development.IDE hiding (pluginHandlers) import Development.IDE.GHC.Compat as Compat hiding (Cpp, Warning, hang, vcat) @@ -50,10 +49,6 @@ import System.Process.Run (cwd, proc) import System.Process.Text (readCreateProcessWithExitCode) import Text.Read (readMaybe) -#if MIN_VERSION_fourmolu(0,12,0) -import qualified Paths_fourmolu as Fourmolu -#endif - descriptor :: Recorder (WithPriority LogEvent) -> PluginId -> PluginDescriptor IdeState descriptor recorder plId = (defaultPluginDescriptor plId) @@ -81,11 +76,7 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro (pure . Left . PluginInternalError . T.pack . show) $ runExceptT $ cliHandler fileOpts else do -#if MIN_VERSION_fourmolu(0,12,0) - logWith recorder Info $ LogBuiltinVersion $ Just Fourmolu.version -#else - logWith recorder Info $ LogBuiltinVersion Nothing -#endif + 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) @@ -141,12 +132,12 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro traverse (readMaybe @Int . T.unpack) $ T.splitOn "." v case version of Just v -> do - logWith recorder Info $ LogExternalVersion v + logWith recorder Debug $ LogExternalVersion v pure CLIVersionInfo { noCabal = v >= [0, 7] } Nothing -> do - logWith recorder Info $ LogExternalVersion [] + logWith recorder Debug $ LogExternalVersion [] logWith recorder Warning $ NoVersion out pure CLIVersionInfo { noCabal = True @@ -175,7 +166,7 @@ data LogEvent | ConfigPath FilePath | NoConfigPath [FilePath] | StdErr Text - | LogBuiltinVersion (Maybe Version) + | LogCompiledInVersion String | LogExternalVersion [Int] deriving (Show) @@ -186,8 +177,11 @@ instance Pretty LogEvent where NoConfigPath ps -> "No " <> pretty configFileName <> " found in any of:" <> line <> indent 2 (vsep (map (pretty . show) ps)) StdErr t -> "Fourmolu stderr:" <> line <> indent 2 (pretty t) - LogBuiltinVersion v -> "Using builtin fourmolu" <> pretty (maybe "" showVersion v) - LogExternalVersion v -> "Using external fourmolu" <> pretty (intercalate "." $ map show v) + LogCompiledInVersion v -> "Using compiled in fourmolu-" <> pretty v + LogExternalVersion v -> + "Using external fourmolu" + <> if null v then "" else "-" + <> pretty (intercalate "." $ map show v) convertDynFlags :: DynFlags -> [String] convertDynFlags df = From f90eddacff5028febbc9123bed9cac6daec70cab Mon Sep 17 00:00:00 2001 From: Lei Zhu Date: Mon, 7 Aug 2023 22:28:34 +0800 Subject: [PATCH 4/5] Log Ormolu version --- plugins/hls-ormolu-plugin/src/Ide/Plugin/Ormolu.hs | 12 +++++++----- src/HlsPlugins.hs | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/plugins/hls-ormolu-plugin/src/Ide/Plugin/Ormolu.hs b/plugins/hls-ormolu-plugin/src/Ide/Plugin/Ormolu.hs index f18323374d..248235dc94 100644 --- a/plugins/hls-ormolu-plugin/src/Ide/Plugin/Ormolu.hs +++ b/plugins/hls-ormolu-plugin/src/Ide/Plugin/Ormolu.hs @@ -35,21 +35,23 @@ import System.FilePath (takeFileName) -- --------------------------------------------------------------------- -descriptor :: PluginId -> PluginDescriptor IdeState -descriptor plId = (defaultPluginDescriptor plId) - { pluginHandlers = mkFormattingHandlers provider +descriptor :: Recorder (WithPriority T.Text) -> PluginId -> PluginDescriptor IdeState +descriptor recorder plId = (defaultPluginDescriptor plId) + { pluginHandlers = mkFormattingHandlers $ provider recorder } -- --------------------------------------------------------------------- -provider :: FormattingHandler IdeState -provider ideState typ contents fp _ = ExceptT $ withIndefiniteProgress title Cancellable $ runExceptT $ do +provider :: Recorder (WithPriority T.Text) -> FormattingHandler IdeState +provider recorder ideState typ contents fp _ = ExceptT $ withIndefiniteProgress title Cancellable $ runExceptT $ do ghc <- liftIO $ runAction "Ormolu" ideState $ use GhcSession fp let df = hsc_dflags . hscEnv <$> ghc fileOpts <- case df of Nothing -> pure [] Just df -> pure $ fromDyn df + logWith recorder Debug $ "Using ormolu-" <> VERSION_ormolu + let fullRegion = RegionIndices Nothing Nothing rangeRegion s e = RegionIndices (Just $ s + 1) (Just $ e + 1) diff --git a/src/HlsPlugins.hs b/src/HlsPlugins.hs index a23051fd9e..e12e5e9f35 100644 --- a/src/HlsPlugins.hs +++ b/src/HlsPlugins.hs @@ -169,7 +169,7 @@ idePlugins recorder = pluginDescToIdePlugins allPlugins let pId = "tactics" in Tactic.descriptor (pluginRecorder pId) pId: #endif #if hls_ormolu - Ormolu.descriptor "ormolu" : + let pId = "ormolu" in Ormolu.descriptor (pluginRecorder pId) pId : #endif #if hls_stylishHaskell StylishHaskell.descriptor "stylish-haskell" : From 7c5cf2bf74a7584d214215cc8db000fe98316ebc Mon Sep 17 00:00:00 2001 From: Lei Zhu Date: Mon, 7 Aug 2023 23:09:06 +0800 Subject: [PATCH 5/5] Fix ormolu test --- plugins/hls-ormolu-plugin/hls-ormolu-plugin.cabal | 1 + plugins/hls-ormolu-plugin/test/Main.hs | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/hls-ormolu-plugin/hls-ormolu-plugin.cabal b/plugins/hls-ormolu-plugin/hls-ormolu-plugin.cabal index 40902789d9..b0e90c21b9 100644 --- a/plugins/hls-ormolu-plugin/hls-ormolu-plugin.cabal +++ b/plugins/hls-ormolu-plugin/hls-ormolu-plugin.cabal @@ -57,4 +57,5 @@ test-suite tests , hls-ormolu-plugin , hls-test-utils == 2.1.0.0 , lsp-types + , text , ormolu diff --git a/plugins/hls-ormolu-plugin/test/Main.hs b/plugins/hls-ormolu-plugin/test/Main.hs index bacb9daa30..8b2e2f77a8 100644 --- a/plugins/hls-ormolu-plugin/test/Main.hs +++ b/plugins/hls-ormolu-plugin/test/Main.hs @@ -4,6 +4,7 @@ module Main ( main ) where +import qualified Data.Text as T import qualified Ide.Plugin.Ormolu as Ormolu import Language.LSP.Protocol.Types import System.FilePath @@ -12,8 +13,8 @@ import Test.Hls main :: IO () main = defaultTestRunner tests -ormoluPlugin :: PluginTestDescriptor () -ormoluPlugin = mkPluginTestDescriptor' Ormolu.descriptor "ormolu" +ormoluPlugin :: PluginTestDescriptor T.Text +ormoluPlugin = mkPluginTestDescriptor Ormolu.descriptor "ormolu" tests :: TestTree tests = testGroup "ormolu"