@@ -23,6 +23,7 @@ import Data.List (intercalate)
2323import Data.Maybe (catMaybes )
2424import Data.Text (Text )
2525import qualified Data.Text as T
26+ import Data.Version (showVersion )
2627import Development.IDE hiding (pluginHandlers )
2728import Development.IDE.GHC.Compat as Compat hiding (Cpp , Warning ,
2829 hang , vcat )
@@ -38,20 +39,25 @@ import Language.LSP.Protocol.Types
3839import Language.LSP.Server hiding (defaultConfig )
3940import Ormolu
4041import Ormolu.Config
42+ import qualified Paths_fourmolu as Fourmolu
4143import System.Exit
4244import System.FilePath
4345import System.Process.Run (cwd , proc )
4446import System.Process.Text (readCreateProcessWithExitCode )
4547import Text.Read (readMaybe )
4648
49+ #if MIN_VERSION_fourmolu(0,16,0)
50+ import qualified Data.Yaml as Yaml
51+ #endif
52+
4753descriptor :: Recorder (WithPriority LogEvent ) -> PluginId -> PluginDescriptor IdeState
4854descriptor recorder plId =
4955 (defaultPluginDescriptor plId desc)
5056 { pluginHandlers = mkFormattingHandlers $ provider recorder plId
5157 , pluginConfigDescriptor = defaultConfigDescriptor{configCustomConfig = mkCustomConfig properties}
5258 }
5359 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
5561
5662properties :: Properties '[ 'PropertyKey " external" 'TBoolean, 'PropertyKey " path" 'TString]
5763properties =
@@ -77,36 +83,17 @@ provider recorder plId ideState token typ contents fp fo = ExceptT $ pluginWithI
7783 handle @ IOException (pure . Left . PluginInternalError . T. pack . show ) $
7884 runExceptT (cliHandler fourmoluExePath fileOpts)
7985 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'
9988 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+ }
11097 ExceptT . liftIO $
11198 bimap (PluginInternalError . T. pack . show ) (InL . makeDiffTextEdit contents)
11299 <$> try @ OrmoluException (ormolu config fp' contents)
@@ -158,6 +145,49 @@ provider recorder plId ideState token typ contents fp fo = ExceptT $ pluginWithI
158145 logWith recorder Info $ StdErr err
159146 throwError $ PluginInternalError $ " Fourmolu failed with exit code " <> T. pack (show n)
160147
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+
161191data LogEvent
162192 = NoVersion Text
163193 | ConfigPath FilePath
@@ -197,8 +227,3 @@ newtype CLIVersionInfo = CLIVersionInfo
197227
198228mwhen :: Monoid a => Bool -> a -> a
199229mwhen 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