Skip to content

Commit

Permalink
Merge pull request haskell#3860 from haskell/fourmolu-custom-path
Browse files Browse the repository at this point in the history
 Add option for setting manual path to Fourmolu binary
  • Loading branch information
fendor authored Jan 16, 2024
2 parents 8697f5f + e3d3e94 commit 4361687
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 15 deletions.
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

0 comments on commit 4361687

Please sign in to comment.