Skip to content

Commit f15c0b7

Browse files
Fix loadConfigFile
1 parent 75daa44 commit f15c0b7

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

haskell-language-server.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,7 @@ library hls-fourmolu-plugin
14701470
, process-extras >= 0.7.1
14711471
, text
14721472
, transformers
1473-
1473+
, yaml
14741474

14751475
test-suite hls-fourmolu-plugin-tests
14761476
import: defaults, pedantic, test-defaults, warnings

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

+49-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE DataKinds #-}
23
{-# LANGUAGE LambdaCase #-}
34
{-# LANGUAGE OverloadedLabels #-}
@@ -45,6 +46,10 @@ import System.Process.Run (cwd, proc)
4546
import System.Process.Text (readCreateProcessWithExitCode)
4647
import Text.Read (readMaybe)
4748

49+
#if MIN_VERSION_fourmolu(0,16,0)
50+
import qualified Data.Yaml as Yaml
51+
#endif
52+
4853
descriptor :: Recorder (WithPriority LogEvent) -> PluginId -> PluginDescriptor IdeState
4954
descriptor recorder plId =
5055
(defaultPluginDescriptor plId desc)
@@ -79,24 +84,7 @@ provider recorder plId ideState token typ contents fp fo = ExceptT $ pluginWithI
7984
runExceptT (cliHandler fourmoluExePath fileOpts)
8085
else do
8186
logWith recorder Debug $ LogCompiledInVersion (showVersion Fourmolu.version)
82-
FourmoluConfig{..} <-
83-
liftIO (loadConfigFile fp') >>= \case
84-
ConfigLoaded file opts -> do
85-
logWith recorder Info $ ConfigPath file
86-
pure opts
87-
ConfigNotFound searchDirs -> do
88-
logWith recorder Info $ NoConfigPath searchDirs
89-
pure emptyConfig
90-
ConfigParseError f err -> do
91-
lift $ pluginSendNotification SMethod_WindowShowMessage $
92-
ShowMessageParams
93-
{ _type_ = MessageType_Error
94-
, _message = errorMessage
95-
}
96-
throwError $ PluginInternalError errorMessage
97-
where
98-
errorMessage = "Failed to load " <> T.pack f <> ": " <> T.pack (show err)
99-
87+
FourmoluConfig{..} <- loadConfig recorder fp'
10088
let config =
10189
refineConfig ModuleSource Nothing Nothing Nothing $
10290
defaultConfig
@@ -157,6 +145,49 @@ provider recorder plId ideState token typ contents fp fo = ExceptT $ pluginWithI
157145
logWith recorder Info $ StdErr err
158146
throwError $ PluginInternalError $ "Fourmolu failed with exit code " <> T.pack (show n)
159147

148+
loadConfig ::
149+
Recorder (WithPriority LogEvent) ->
150+
FilePath ->
151+
ExceptT PluginError (HandlerM Ide.Types.Config) FourmoluConfig
152+
#if MIN_VERSION_fourmolu(0,16,0)
153+
loadConfig recorder fp = do
154+
liftIO (findConfigFile fp) >>= \case
155+
Left (ConfigNotFound searchDirs) -> do
156+
logWith recorder Info $ NoConfigPath searchDirs
157+
pure emptyConfig
158+
Right file -> do
159+
logWith recorder Info $ ConfigPath file
160+
liftIO (Yaml.decodeFileEither file) >>= \case
161+
Left err -> do
162+
let errorMessage = "Failed to load " <> T.pack file <> ": " <> T.pack (show err)
163+
lift $ pluginSendNotification SMethod_WindowShowMessage $
164+
ShowMessageParams
165+
{ _type_ = MessageType_Error
166+
, _message = errorMessage
167+
}
168+
throwError $ PluginInternalError errorMessage
169+
Right cfg -> do
170+
pure cfg
171+
#else
172+
loadConfig recorder fp = do
173+
liftIO (loadConfigFile fp) >>= \case
174+
ConfigLoaded file opts -> do
175+
logWith recorder Info $ ConfigPath file
176+
pure opts
177+
ConfigNotFound searchDirs -> do
178+
logWith recorder Info $ NoConfigPath searchDirs
179+
pure emptyConfig
180+
ConfigParseError f err -> do
181+
lift $ pluginSendNotification SMethod_WindowShowMessage $
182+
ShowMessageParams
183+
{ _type_ = MessageType_Error
184+
, _message = errorMessage
185+
}
186+
throwError $ PluginInternalError errorMessage
187+
where
188+
errorMessage = "Failed to load " <> T.pack f <> ": " <> T.pack (show err)
189+
#endif
190+
160191
data LogEvent
161192
= NoVersion Text
162193
| ConfigPath FilePath

0 commit comments

Comments
 (0)