|
| 1 | +{-# LANGUAGE CPP #-} |
1 | 2 | {-# LANGUAGE DataKinds #-} |
2 | 3 | {-# LANGUAGE LambdaCase #-} |
3 | 4 | {-# LANGUAGE OverloadedLabels #-} |
@@ -45,6 +46,10 @@ import System.Process.Run (cwd, proc) |
45 | 46 | import System.Process.Text (readCreateProcessWithExitCode) |
46 | 47 | import Text.Read (readMaybe) |
47 | 48 |
|
| 49 | +#if MIN_VERSION_fourmolu(0,16,0) |
| 50 | +import qualified Data.Yaml as Yaml |
| 51 | +#endif |
| 52 | + |
48 | 53 | descriptor :: Recorder (WithPriority LogEvent) -> PluginId -> PluginDescriptor IdeState |
49 | 54 | descriptor recorder plId = |
50 | 55 | (defaultPluginDescriptor plId desc) |
@@ -79,24 +84,7 @@ provider recorder plId ideState token typ contents fp fo = ExceptT $ pluginWithI |
79 | 84 | runExceptT (cliHandler fourmoluExePath fileOpts) |
80 | 85 | else do |
81 | 86 | 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' |
100 | 88 | let config = |
101 | 89 | refineConfig ModuleSource Nothing Nothing Nothing $ |
102 | 90 | defaultConfig |
@@ -157,6 +145,49 @@ provider recorder plId ideState token typ contents fp fo = ExceptT $ pluginWithI |
157 | 145 | logWith recorder Info $ StdErr err |
158 | 146 | throwError $ PluginInternalError $ "Fourmolu failed with exit code " <> T.pack (show n) |
159 | 147 |
|
| 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 | + |
160 | 191 | data LogEvent |
161 | 192 | = NoVersion Text |
162 | 193 | | ConfigPath FilePath |
|
0 commit comments