Skip to content

Add support for Fourmolu 0.16 #4314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix loadConfigFile
  • Loading branch information
brandonchinn178 committed Jun 14, 2024
commit f15c0b7c769fcfd10ee504db51a485b03fe794c1
2 changes: 1 addition & 1 deletion haskell-language-server.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ library hls-fourmolu-plugin
, process-extras >= 0.7.1
, text
, transformers

, yaml

test-suite hls-fourmolu-plugin-tests
import: defaults, pedantic, test-defaults, warnings
Expand Down
67 changes: 49 additions & 18 deletions plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedLabels #-}
Expand Down Expand Up @@ -45,6 +46,10 @@ import System.Process.Run (cwd, proc)
import System.Process.Text (readCreateProcessWithExitCode)
import Text.Read (readMaybe)

#if MIN_VERSION_fourmolu(0,16,0)
import qualified Data.Yaml as Yaml
#endif

descriptor :: Recorder (WithPriority LogEvent) -> PluginId -> PluginDescriptor IdeState
descriptor recorder plId =
(defaultPluginDescriptor plId desc)
Expand Down Expand Up @@ -79,24 +84,7 @@ provider recorder plId ideState token typ contents fp fo = ExceptT $ pluginWithI
runExceptT (cliHandler fourmoluExePath fileOpts)
else do
logWith recorder Debug $ LogCompiledInVersion (showVersion Fourmolu.version)
FourmoluConfig{..} <-
liftIO (loadConfigFile fp') >>= \case
ConfigLoaded file opts -> do
logWith recorder Info $ ConfigPath file
pure opts
ConfigNotFound searchDirs -> do
logWith recorder Info $ NoConfigPath searchDirs
pure emptyConfig
ConfigParseError f err -> do
lift $ pluginSendNotification SMethod_WindowShowMessage $
ShowMessageParams
{ _type_ = MessageType_Error
, _message = errorMessage
}
throwError $ PluginInternalError errorMessage
where
errorMessage = "Failed to load " <> T.pack f <> ": " <> T.pack (show err)

FourmoluConfig{..} <- loadConfig recorder fp'
let config =
refineConfig ModuleSource Nothing Nothing Nothing $
defaultConfig
Expand Down Expand Up @@ -157,6 +145,49 @@ provider recorder plId ideState token typ contents fp fo = ExceptT $ pluginWithI
logWith recorder Info $ StdErr err
throwError $ PluginInternalError $ "Fourmolu failed with exit code " <> T.pack (show n)

loadConfig ::
Recorder (WithPriority LogEvent) ->
FilePath ->
ExceptT PluginError (HandlerM Ide.Types.Config) FourmoluConfig
#if MIN_VERSION_fourmolu(0,16,0)
loadConfig recorder fp = do
liftIO (findConfigFile fp) >>= \case
Left (ConfigNotFound searchDirs) -> do
logWith recorder Info $ NoConfigPath searchDirs
pure emptyConfig
Right file -> do
logWith recorder Info $ ConfigPath file
liftIO (Yaml.decodeFileEither file) >>= \case
Left err -> do
let errorMessage = "Failed to load " <> T.pack file <> ": " <> T.pack (show err)
lift $ pluginSendNotification SMethod_WindowShowMessage $
ShowMessageParams
{ _type_ = MessageType_Error
, _message = errorMessage
}
throwError $ PluginInternalError errorMessage
Right cfg -> do
pure cfg
#else
loadConfig recorder fp = do
liftIO (loadConfigFile fp) >>= \case
ConfigLoaded file opts -> do
logWith recorder Info $ ConfigPath file
pure opts
ConfigNotFound searchDirs -> do
logWith recorder Info $ NoConfigPath searchDirs
pure emptyConfig
ConfigParseError f err -> do
lift $ pluginSendNotification SMethod_WindowShowMessage $
ShowMessageParams
{ _type_ = MessageType_Error
, _message = errorMessage
}
throwError $ PluginInternalError errorMessage
where
errorMessage = "Failed to load " <> T.pack f <> ": " <> T.pack (show err)
#endif

data LogEvent
= NoVersion Text
| ConfigPath FilePath
Expand Down