Skip to content
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

Use Hackage fixities in Fourmolu CLI mode #3454

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
29 changes: 21 additions & 8 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 BlockArguments #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE LambdaCase #-}
Expand Down Expand Up @@ -25,6 +26,7 @@ import Development.IDE.GHC.Compat as Compat hiding (Cpp, Warning,
hang, vcat)
import qualified Development.IDE.GHC.Compat.Util as S
import GHC.LanguageExtensions.Type (Extension (Cpp))
import GHC.Unit (UnitId (UnitId))
import Ide.Plugin.Fourmolu.Shim
import Ide.Plugin.Properties
import Ide.PluginUtils (makeDiffTextEdit)
Expand Down Expand Up @@ -56,35 +58,46 @@ properties =

provider :: Recorder (WithPriority LogEvent) -> PluginId -> FormattingHandler IdeState
provider recorder plId ideState typ contents fp fo = withIndefiniteProgress title Cancellable $ do
fileOpts <-
maybe [] (convertDynFlags . hsc_dflags . hscEnv)
<$> liftIO (runAction "Fourmolu" ideState $ use GhcSession fp)
dynFlags <- fmap (hsc_dflags . hscEnv) <$> liftIO (runAction "Fourmolu" ideState $ use GhcSession fp)
let fileOpts = maybe [] convertDynFlags dynFlags
packages =
maybe
[]
( map (T.unpack . T.intercalate "-" . reverse . drop' 2 . reverse . T.splitOn "-" . printOutputable)
. mapMaybe getUnit . packageFlags
)
dynFlags
where
getUnit = \case
ExposePackage _ (UnitIdArg (RealUnit (Definite (UnitId s)))) _ -> Just s
_ -> Nothing
drop' n xs = drop (n `min` (Prelude.length xs - 1)) xs
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a horrible hack, but I don't have any better ideas. I wouldn't be surprised if there's no clean way to go from GHC package-id strings to Hackage library names.

useCLI <- liftIO $ runAction "Fourmolu" ideState $ usePropertyAction #external plId properties
if useCLI
then liftIO
. fmap (join . first (mkError . show))
. try @IOException
$ do
CLIVersionInfo{noCabal} <- do -- check Fourmolu version so that we know which flags to use
CLIVersionInfo{fixities} <- do -- check Fourmolu version so that we know which flags to use
(exitCode, out, _err) <- readCreateProcessWithExitCode ( proc "fourmolu" ["-v"] ) ""
let version = do
guard $ exitCode == ExitSuccess
"fourmolu" : v : _ <- pure $ T.words out
traverse (readMaybe @Int . T.unpack) $ T.splitOn "." v
case version of
Just v -> pure CLIVersionInfo
{ noCabal = v >= [0, 7]
{ fixities = v >= [0, 7]
}
Nothing -> do
logWith recorder Warning $ NoVersion out
pure CLIVersionInfo
{ noCabal = True
{ fixities = True
}
(exitCode, out, err) <- -- run Fourmolu
readCreateProcessWithExitCode
( proc "fourmolu" $
map ("-o" <>) fileOpts
<> mwhen noCabal ["--no-cabal"]
<> mwhen fixities ("--no-cabal" : map ("-p" <>) packages)
<> catMaybes
[ ("--start-line=" <>) . show <$> regionStartLine region
, ("--end-line=" <>) . show <$> regionEndLine region
Expand Down Expand Up @@ -169,7 +182,7 @@ convertDynFlags df =
in pp <> pm <> ex

newtype CLIVersionInfo = CLIVersionInfo
{ noCabal :: Bool
{ fixities :: Bool
}

mwhen :: Monoid a => Bool -> a -> a
Expand Down