Skip to content

Pass package dependency information to Fourmolu #3165

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions plugins/hls-fourmolu-plugin/hls-fourmolu-plugin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ library
ghc-options: -Wall
build-depends:
, base >=4.12 && <5
, containers
, filepath
, fourmolu ^>=0.3 || ^>=0.4 || ^>= 0.6 || ^>= 0.7 || ^>= 0.8
, ghc
Expand Down
27 changes: 22 additions & 5 deletions plugins/hls-fourmolu-plugin/src/Ide/Plugin/Fourmolu.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ import Control.Monad
import Control.Monad.IO.Class
import Data.Bifunctor (bimap, first)
import Data.Maybe
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as T
import Development.IDE hiding (pluginHandlers)
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 GHC.Unit.Env (UnitEnv (ue_units))
import Ide.Plugin.Fourmolu.Shim
import Ide.Plugin.Properties
import Ide.PluginUtils (makeDiffTextEdit,
Expand Down Expand Up @@ -55,16 +58,25 @@ 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)
maybeHscEnv <- liftIO (runAction "Fourmolu" ideState $ use GhcSession fp)
let fileOpts = maybe [] (convertDynFlags . hsc_dflags . hscEnv) maybeHscEnv
deps = case maybeHscEnv of
Nothing -> []
Just env -> flip mapMaybe (Compat.explicitUnits $ ue_units $ hsc_unit_env $ hscEnv env) $ \case
RealUnit (Definite (UnitId x)) -> pure . reverse <=< dropChunk <=< dropChunk . reverse $ S.unpackFS x
_ -> Nothing
where
dropChunk = tailSafe . dropWhile (/= '-')
tailSafe = \case
_ : xs -> Just xs
_ -> Nothing
useCLI <- usePropertyLsp #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{noCabal, packages} <- 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
Expand All @@ -73,17 +85,20 @@ provider recorder plId ideState typ contents fp fo = withIndefiniteProgress titl
case version of
Just v -> pure CLIVersionInfo
{ noCabal = v >= [0, 7]
, packages = v >= [0, 7]
}
Nothing -> do
logWith recorder Warning $ NoVersion out
pure CLIVersionInfo
{ noCabal = True
, packages = True
}
(exitCode, out, err) <- -- run Fourmolu
readCreateProcessWithExitCode
( proc "fourmolu" $
map ("-o" <>) fileOpts
<> mwhen noCabal ["--no-cabal"]
<> mwhen packages (deps >>= \d -> ["-p", d])
<> catMaybes
[ ("--start-line=" <>) . show <$> regionStartLine region
, ("--end-line=" <>) . show <$> regionEndLine region
Expand Down Expand Up @@ -113,6 +128,7 @@ provider recorder plId ideState typ contents fp fo = withIndefiniteProgress titl
fillMissingPrinterOpts
(printerOpts <> lspPrinterOpts)
defaultPrinterOpts
, cfgDependencies = Set.fromList deps
}
in liftIO (loadConfigFile fp') >>= \case
ConfigLoaded file opts -> liftIO $ do
Expand Down Expand Up @@ -167,8 +183,9 @@ convertDynFlags df =
x -> "-X" ++ show x
in pp <> pm <> ex

newtype CLIVersionInfo = CLIVersionInfo
data CLIVersionInfo = CLIVersionInfo
{ noCabal :: Bool
, packages :: Bool
}

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