@@ -23,6 +23,7 @@ import Data.List (intercalate)
23
23
import Data.Maybe (catMaybes )
24
24
import Data.Text (Text )
25
25
import qualified Data.Text as T
26
+ import Data.Version (showVersion )
26
27
import Development.IDE hiding (pluginHandlers )
27
28
import Development.IDE.GHC.Compat as Compat hiding (Cpp , Warning ,
28
29
hang , vcat )
@@ -38,20 +39,25 @@ import Language.LSP.Protocol.Types
38
39
import Language.LSP.Server hiding (defaultConfig )
39
40
import Ormolu
40
41
import Ormolu.Config
42
+ import qualified Paths_fourmolu as Fourmolu
41
43
import System.Exit
42
44
import System.FilePath
43
45
import System.Process.Run (cwd , proc )
44
46
import System.Process.Text (readCreateProcessWithExitCode )
45
47
import Text.Read (readMaybe )
46
48
49
+ #if MIN_VERSION_fourmolu(0,16,0)
50
+ import qualified Data.Yaml as Yaml
51
+ #endif
52
+
47
53
descriptor :: Recorder (WithPriority LogEvent ) -> PluginId -> PluginDescriptor IdeState
48
54
descriptor recorder plId =
49
55
(defaultPluginDescriptor plId desc)
50
56
{ pluginHandlers = mkFormattingHandlers $ provider recorder plId
51
57
, pluginConfigDescriptor = defaultConfigDescriptor{configCustomConfig = mkCustomConfig properties}
52
58
}
53
59
where
54
- desc = " Provides formatting of Haskell files via fourmolu. Built with fourmolu-" <> VERSION_fourmolu
60
+ desc = T. pack $ " Provides formatting of Haskell files via fourmolu. Built with fourmolu-" <> showVersion Fourmolu. version
55
61
56
62
properties :: Properties '[ 'PropertyKey " external" 'TBoolean, 'PropertyKey " path" 'TString]
57
63
properties =
@@ -77,36 +83,17 @@ provider recorder plId ideState token typ contents fp fo = ExceptT $ pluginWithI
77
83
handle @ IOException (pure . Left . PluginInternalError . T. pack . show ) $
78
84
runExceptT (cliHandler fourmoluExePath fileOpts)
79
85
else do
80
- logWith recorder Debug $ LogCompiledInVersion VERSION_fourmolu
81
- FourmoluConfig {.. } <-
82
- liftIO (loadConfigFile fp') >>= \ case
83
- ConfigLoaded file opts -> do
84
- logWith recorder Info $ ConfigPath file
85
- pure opts
86
- ConfigNotFound searchDirs -> do
87
- logWith recorder Info $ NoConfigPath searchDirs
88
- pure emptyConfig
89
- ConfigParseError f err -> do
90
- lift $ pluginSendNotification SMethod_WindowShowMessage $
91
- ShowMessageParams
92
- { _type_ = MessageType_Error
93
- , _message = errorMessage
94
- }
95
- throwError $ PluginInternalError errorMessage
96
- where
97
- errorMessage = " Failed to load " <> T. pack f <> " : " <> T. pack (show err)
98
-
86
+ logWith recorder Debug $ LogCompiledInVersion (showVersion Fourmolu. version)
87
+ FourmoluConfig {.. } <- loadConfig recorder fp'
99
88
let config =
100
- #if MIN_VERSION_fourmolu(0,13,0)
101
- refineConfig ModuleSource Nothing Nothing Nothing
102
- #endif
103
- defaultConfig
104
- { cfgDynOptions = map DynOption fileOpts
105
- , cfgFixityOverrides = cfgFileFixities
106
- , cfgRegion = region
107
- , cfgDebug = False
108
- , cfgPrinterOpts = resolvePrinterOpts [lspPrinterOpts, cfgFilePrinterOpts]
109
- }
89
+ refineConfig ModuleSource Nothing Nothing Nothing $
90
+ defaultConfig
91
+ { cfgDynOptions = map DynOption fileOpts
92
+ , cfgFixityOverrides = cfgFileFixities
93
+ , cfgRegion = region
94
+ , cfgDebug = False
95
+ , cfgPrinterOpts = resolvePrinterOpts [lspPrinterOpts, cfgFilePrinterOpts]
96
+ }
110
97
ExceptT . liftIO $
111
98
bimap (PluginInternalError . T. pack . show ) (InL . makeDiffTextEdit contents)
112
99
<$> try @ OrmoluException (ormolu config fp' contents)
@@ -158,6 +145,49 @@ provider recorder plId ideState token typ contents fp fo = ExceptT $ pluginWithI
158
145
logWith recorder Info $ StdErr err
159
146
throwError $ PluginInternalError $ " Fourmolu failed with exit code " <> T. pack (show n)
160
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
+
161
191
data LogEvent
162
192
= NoVersion Text
163
193
| ConfigPath FilePath
@@ -197,8 +227,3 @@ newtype CLIVersionInfo = CLIVersionInfo
197
227
198
228
mwhen :: Monoid a => Bool -> a -> a
199
229
mwhen b x = if b then x else mempty
200
-
201
- #if !MIN_VERSION_fourmolu(0,14,0)
202
- resolvePrinterOpts :: [PrinterOptsPartial ] -> PrinterOptsTotal
203
- resolvePrinterOpts = foldr fillMissingPrinterOpts defaultPrinterOpts
204
- #endif
0 commit comments