1- {-# LANGUAGE RecordWildCards #-}
2- {-# LANGUAGE OverloadedStrings #-}
1+ {-# LANGUAGE OverloadedStrings #-}
2+ {-# LANGUAGE RecordWildCards #-}
33{-# LANGUAGE ScopedTypeVariables #-}
4- {-# LANGUAGE TypeApplications #-}
5- {-# LANGUAGE ViewPatterns #-}
4+ {-# LANGUAGE TypeApplications #-}
65
76module Ide.Plugin.Ormolu
87 (
@@ -12,19 +11,27 @@ module Ide.Plugin.Ormolu
1211where
1312
1413import Control.Exception
15- import qualified Data.Text as T
14+ import qualified Data.Text as T
1615import Development.IDE.Core.Rules
16+ import Development.IDE.Core.RuleTypes (GhcSession (GhcSession ))
17+ import Development.IDE.Core.Shake (use )
18+ import Development.IDE.GHC.Util (hscEnv )
1719import Development.IDE.Types.Diagnostics as D
1820import Development.IDE.Types.Location
19- import qualified DynFlags as D
20- import qualified EnumSet as S
21+ import qualified DynFlags as D
22+ import qualified EnumSet as S
2123import GHC
22- import Ide.Types
23- import Ide.PluginUtils
24+ import GHC.LanguageExtensions.Type
25+ import GhcPlugins ( HscEnv ( hsc_dflags ))
2426import Ide.Plugin.Formatter
27+ import Ide.PluginUtils
28+ import Ide.Types
29+ import Language.Haskell.LSP.Core (LspFuncs (withIndefiniteProgress ),
30+ ProgressCancellable (Cancellable ))
2531import Language.Haskell.LSP.Types
2632import Ormolu
27- import Text.Regex.TDFA.Text ()
33+ import System.FilePath (takeFileName )
34+ import Text.Regex.TDFA.Text ()
2835
2936-- ---------------------------------------------------------------------
3037
@@ -36,24 +43,24 @@ descriptor plId = (defaultPluginDescriptor plId)
3643-- ---------------------------------------------------------------------
3744
3845provider :: FormattingProvider IO
39- provider _lf ideState typ contents fp _ = do
46+ provider lf ideState typ contents fp _ = withIndefiniteProgress lf title Cancellable $ do
4047 let
41- fromDyn :: ParsedModule -> IO [DynOption ]
42- fromDyn pmod =
48+ fromDyn :: DynFlags -> IO [DynOption ]
49+ fromDyn df =
4350 let
44- df = ms_hspp_opts $ pm_mod_summary pmod
4551 pp =
4652 let p = D. sPgm_F $ D. settings df
4753 in if null p then [] else [" -pgmF=" <> p]
4854 pm = map ((" -fplugin=" <> ) . moduleNameString) $ D. pluginModNames df
49- ex = map (( " -X " <> ) . show ) $ S. toList $ D. extensionFlags df
55+ ex = map showExtension $ S. toList $ D. extensionFlags df
5056 in
5157 return $ map DynOption $ pp <> pm <> ex
5258
53- m_parsed <- runAction " Ormolu" ideState $ getParsedModule fp
54- fileOpts <- case m_parsed of
59+ ghc <- runAction " Ormolu" ideState $ use GhcSession fp
60+ let df = hsc_dflags . hscEnv <$> ghc
61+ fileOpts <- case df of
5562 Nothing -> return []
56- Just pm -> fromDyn pm
63+ Just df -> fromDyn df
5764
5865 let
5966 fullRegion = RegionIndices Nothing Nothing
@@ -71,7 +78,12 @@ provider _lf ideState typ contents fp _ = do
7178 in
7279 ret <$> fmt contents (mkConf fileOpts (rangeRegion sl el))
7380 where
81+ title = T. pack $ " Formatting " <> takeFileName (fromNormalizedFilePath fp)
7482 ret :: Either OrmoluException T. Text -> Either ResponseError (List TextEdit )
7583 ret (Left err) = Left
7684 (responseError (T. pack $ " ormoluCmd: " ++ show err) )
7785 ret (Right new) = Right (makeDiffTextEdit contents new)
86+
87+ showExtension :: Extension -> String
88+ showExtension Cpp = " -XCPP"
89+ showExtension other = " -X" ++ show other
0 commit comments