Skip to content

Commit 848a9f6

Browse files
committed
Use proper structured logging for Fourmolu
Previously we just printed directly to stdout and stderr.
1 parent 8a5840a commit 848a9f6

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

exe/Plugins.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ idePlugins recorder includeExamples = pluginDescToIdePlugins allPlugins
136136
Floskell.descriptor "floskell" :
137137
#endif
138138
#if fourmolu
139-
Fourmolu.descriptor "fourmolu" :
139+
Fourmolu.descriptor pluginRecorder "fourmolu" :
140140
#endif
141141
#if tactic
142142
Tactic.descriptor pluginRecorder "tactics" :

plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,31 @@ import Control.Monad
1818
import Control.Monad.IO.Class
1919
import Data.Bifunctor (first)
2020
import Data.Maybe
21+
import Data.Text (Text)
2122
import qualified Data.Text as T
22-
import qualified Data.Text.IO as T
2323
import Development.IDE hiding (pluginHandlers)
24-
import Development.IDE.GHC.Compat as Compat hiding (Cpp)
24+
import Development.IDE.GHC.Compat as Compat hiding (Cpp, Warning,
25+
hang, vcat)
2526
import qualified Development.IDE.GHC.Compat.Util as S
2627
import GHC.LanguageExtensions.Type (Extension (Cpp))
2728
import Ide.Plugin.Properties
2829
import Ide.PluginUtils (makeDiffTextEdit,
2930
usePropertyLsp)
3031
import Ide.Types
3132
import Language.LSP.Server hiding (defaultConfig)
32-
import Language.LSP.Types
33+
import Language.LSP.Types hiding (line)
3334
import Language.LSP.Types.Lens (HasTabSize (tabSize))
3435
import Ormolu
3536
import Ormolu.Config
3637
import System.Exit
3738
import System.FilePath
38-
import System.IO (stderr)
3939
import System.Process.Run (cwd, proc)
4040
import System.Process.Text (readCreateProcessWithExitCode)
4141

42-
descriptor :: PluginId -> PluginDescriptor IdeState
43-
descriptor plId =
42+
descriptor :: Recorder (WithPriority LogEvent) -> PluginId -> PluginDescriptor IdeState
43+
descriptor recorder plId =
4444
(defaultPluginDescriptor plId)
45-
{ pluginHandlers = mkFormattingHandlers $ provider plId
45+
{ pluginHandlers = mkFormattingHandlers $ provider recorder plId
4646
}
4747

4848
properties :: Properties '[ 'PropertyKey "external" 'TBoolean]
@@ -53,8 +53,8 @@ properties =
5353
"Call out to an external \"fourmolu\" executable, rather than using the bundled library"
5454
False
5555

56-
provider :: PluginId -> FormattingHandler IdeState
57-
provider plId ideState typ contents fp fo = withIndefiniteProgress title Cancellable $ do
56+
provider :: Recorder (WithPriority LogEvent) -> PluginId -> FormattingHandler IdeState
57+
provider recorder plId ideState typ contents fp fo = withIndefiniteProgress title Cancellable $ do
5858
fileOpts <-
5959
maybe [] (convertDynFlags . hsc_dflags . hscEnv)
6060
<$> liftIO (runAction "Fourmolu" ideState $ use GhcSession fp)
@@ -75,7 +75,7 @@ provider plId ideState typ contents fp fo = withIndefiniteProgress title Cancell
7575
{ noCabal = v >= ["0", "7"]
7676
}
7777
Nothing -> do
78-
T.hPutStrLn stderr "couldn't get Fourmolu version"
78+
logWith recorder Warning $ NoVersion out
7979
pure CLIVersionInfo
8080
{ noCabal = True
8181
}
@@ -91,11 +91,12 @@ provider plId ideState typ contents fp fo = withIndefiniteProgress title Cancell
9191
<> map ("-o" <>) fileOpts
9292
){cwd = Just $ takeDirectory fp'}
9393
contents
94-
T.hPutStrLn stderr err
9594
case exitCode of
96-
ExitSuccess ->
95+
ExitSuccess -> do
96+
logWith recorder Debug $ StdErr err
9797
pure . Right $ makeDiffTextEdit contents out
98-
ExitFailure n ->
98+
ExitFailure n -> do
99+
logWith recorder Info $ StdErr err
99100
pure . Left . responseError $ "Fourmolu failed with exit code " <> T.pack (show n)
100101
else do
101102
let format fourmoluConfig =
@@ -125,13 +126,10 @@ provider plId ideState typ contents fp fo = withIndefiniteProgress title Cancell
125126
}
126127
in liftIO (loadConfigFile fp') >>= \case
127128
ConfigLoaded file opts -> liftIO $ do
128-
putStrLn $ "Loaded Fourmolu config from: " <> file
129+
logWith recorder Info $ ConfigPath file
129130
format opts
130131
ConfigNotFound searchDirs -> liftIO $ do
131-
putStrLn
132-
. unlines
133-
$ ("No " ++ show configFileName ++ " found in any of:") :
134-
map (" " ++) searchDirs
132+
logWith recorder Info $ NoConfigPath searchDirs
135133
format emptyOptions
136134
where
137135
emptyOptions =
@@ -170,6 +168,21 @@ provider plId ideState typ contents fp fo = withIndefiniteProgress title Cancell
170168
FormatRange (Range (Position sl _) (Position el _)) ->
171169
RegionIndices (Just $ fromIntegral $ sl + 1) (Just $ fromIntegral $ el + 1)
172170

171+
data LogEvent
172+
= NoVersion Text
173+
| ConfigPath FilePath
174+
| NoConfigPath [FilePath]
175+
| StdErr Text
176+
deriving (Show)
177+
178+
instance Pretty LogEvent where
179+
pretty = \case
180+
NoVersion t -> "Couldn't get Fourmolu version:" <> line <> indent 2 (pretty t)
181+
ConfigPath p -> "Loaded Fourmolu config from: " <> pretty (show p)
182+
NoConfigPath ps -> "No " <> pretty configFileName <> " found in any of:"
183+
<> line <> indent 2 (vsep (map (pretty . show) ps))
184+
StdErr t -> "Fourmolu stderr:" <> line <> indent 2 (pretty t)
185+
173186
convertDynFlags :: DynFlags -> [String]
174187
convertDynFlags df =
175188
let pp = ["-pgmF=" <> p | not (null p)]

0 commit comments

Comments
 (0)