Skip to content

Add option for setting manual path to Fourmolu binary #3860

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 6 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 12 additions & 7 deletions plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@ descriptor recorder plId =
where
desc = "Provides formatting of Haskell files via fourmolu. Built with fourmolu-" <> VERSION_fourmolu

properties :: Properties '[ 'PropertyKey "external" 'TBoolean]
properties :: Properties '[ 'PropertyKey "external" 'TBoolean, 'PropertyKey "path" 'TString]
properties =
emptyProperties
& defineStringProperty
#path
"Set path to executable (for \"external\" mode)."
"fourmolu"
& defineBooleanProperty
#external
"Call out to an external \"fourmolu\" executable, rather than using the bundled library"
"Call out to an external \"fourmolu\" executable, rather than using the bundled library."
False

provider :: Recorder (WithPriority LogEvent) -> PluginId -> FormattingHandler IdeState
Expand All @@ -70,10 +74,11 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro
maybe [] (convertDynFlags . hsc_dflags . hscEnv)
<$> liftIO (runAction "Fourmolu" ideState $ use GhcSession fp)
useCLI <- liftIO $ runAction "Fourmolu" ideState $ usePropertyAction #external plId properties
fourmoluExePath <- fmap T.unpack $ liftIO $ runAction "Fourmolu" ideState $ usePropertyAction #path plId properties
if useCLI
then ExceptT . liftIO $
handle @IOException (pure . Left . PluginInternalError . T.pack . show) $
runExceptT (cliHandler fileOpts)
runExceptT (cliHandler fourmoluExePath fileOpts)
else do
logWith recorder Debug $ LogCompiledInVersion VERSION_fourmolu
FourmoluConfig{..} <-
Expand Down Expand Up @@ -117,10 +122,10 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro
RegionIndices Nothing Nothing
FormatRange (Range (Position sl _) (Position el _)) ->
RegionIndices (Just $ fromIntegral $ sl + 1) (Just $ fromIntegral $ el + 1)
cliHandler :: [String] -> ExceptT PluginError IO ([TextEdit] |? Null)
cliHandler fileOpts = do
cliHandler :: FilePath -> [String] -> ExceptT PluginError IO ([TextEdit] |? Null)
cliHandler path fileOpts = do
CLIVersionInfo{noCabal} <- do -- check Fourmolu version so that we know which flags to use
(exitCode, out, _err) <- liftIO $ readCreateProcessWithExitCode ( proc "fourmolu" ["-v"] ) ""
(exitCode, out, _err) <- liftIO $ readCreateProcessWithExitCode ( proc path ["-v"] ) ""
let version = do
guard $ exitCode == ExitSuccess
"fourmolu" : v : _ <- pure $ T.words out
Expand All @@ -139,7 +144,7 @@ provider recorder plId ideState typ contents fp fo = ExceptT $ withIndefinitePro
}
(exitCode, out, err) <- -- run Fourmolu
liftIO $ readCreateProcessWithExitCode
( proc "fourmolu" $
( proc path $
map ("-o" <>) fileOpts
<> mwhen noCabal ["--no-cabal"]
<> catMaybes
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/schema/ghc92/default-config.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
},
"fourmolu": {
"config": {
"external": false
"external": false,
"path": "fourmolu"
}
},
"gadt": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@
},
"haskell.plugin.fourmolu.config.external": {
"default": false,
"markdownDescription": "Call out to an external \"fourmolu\" executable, rather than using the bundled library",
"markdownDescription": "Call out to an external \"fourmolu\" executable, rather than using the bundled library.",
"scope": "resource",
"type": "boolean"
},
"haskell.plugin.fourmolu.config.path": {
"default": "fourmolu",
"markdownDescription": "Set path to executable (for \"external\" mode).",
"scope": "resource",
"type": "string"
},
"haskell.plugin.gadt.globalOn": {
"default": true,
"description": "Enables gadt plugin",
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/schema/ghc94/default-config.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
},
"fourmolu": {
"config": {
"external": false
"external": false,
"path": "fourmolu"
}
},
"gadt": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@
},
"haskell.plugin.fourmolu.config.external": {
"default": false,
"markdownDescription": "Call out to an external \"fourmolu\" executable, rather than using the bundled library",
"markdownDescription": "Call out to an external \"fourmolu\" executable, rather than using the bundled library.",
"scope": "resource",
"type": "boolean"
},
"haskell.plugin.fourmolu.config.path": {
"default": "fourmolu",
"markdownDescription": "Set path to executable (for \"external\" mode).",
"scope": "resource",
"type": "string"
},
"haskell.plugin.gadt.globalOn": {
"default": true,
"description": "Enables gadt plugin",
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/schema/ghc96/default-config.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
},
"fourmolu": {
"config": {
"external": false
"external": false,
"path": "fourmolu"
}
},
"gadt": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@
},
"haskell.plugin.fourmolu.config.external": {
"default": false,
"markdownDescription": "Call out to an external \"fourmolu\" executable, rather than using the bundled library",
"markdownDescription": "Call out to an external \"fourmolu\" executable, rather than using the bundled library.",
"scope": "resource",
"type": "boolean"
},
"haskell.plugin.fourmolu.config.path": {
"default": "fourmolu",
"markdownDescription": "Set path to executable (for \"external\" mode).",
"scope": "resource",
"type": "string"
},
"haskell.plugin.gadt.globalOn": {
"default": true,
"description": "Enables gadt plugin",
Expand Down
3 changes: 2 additions & 1 deletion test/testdata/schema/ghc98/default-config.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
},
"fourmolu": {
"config": {
"external": false
"external": false,
"path": "fourmolu"
}
},
"ghcide-completions": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@
},
"haskell.plugin.fourmolu.config.external": {
"default": false,
"markdownDescription": "Call out to an external \"fourmolu\" executable, rather than using the bundled library",
"markdownDescription": "Call out to an external \"fourmolu\" executable, rather than using the bundled library.",
"scope": "resource",
"type": "boolean"
},
"haskell.plugin.fourmolu.config.path": {
"default": "fourmolu",
"markdownDescription": "Set path to executable (for \"external\" mode).",
"scope": "resource",
"type": "string"
},
"haskell.plugin.ghcide-completions.config.autoExtendOn": {
"default": true,
"markdownDescription": "Extends the import list automatically when completing a out-of-scope identifier",
Expand Down