1
- {-# LANGUAGE RecordWildCards #-}
2
- {-# LANGUAGE OverloadedStrings #-}
1
+ {-# LANGUAGE OverloadedStrings #-}
2
+ {-# LANGUAGE RecordWildCards #-}
3
3
{-# LANGUAGE ScopedTypeVariables #-}
4
- {-# LANGUAGE TypeApplications #-}
5
- {-# LANGUAGE ViewPatterns #-}
4
+ {-# LANGUAGE TypeApplications #-}
6
5
7
6
module Ide.Plugin.Ormolu
8
7
(
@@ -12,19 +11,27 @@ module Ide.Plugin.Ormolu
12
11
where
13
12
14
13
import Control.Exception
15
- import qualified Data.Text as T
14
+ import qualified Data.Text as T
16
15
import 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 )
17
19
import Development.IDE.Types.Diagnostics as D
18
20
import 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
21
23
import GHC
22
- import Ide.Types
23
- import Ide.PluginUtils
24
+ import GHC.LanguageExtensions.Type
25
+ import GhcPlugins ( HscEnv ( hsc_dflags ))
24
26
import Ide.Plugin.Formatter
27
+ import Ide.PluginUtils
28
+ import Ide.Types
29
+ import Language.Haskell.LSP.Core (LspFuncs (withIndefiniteProgress ),
30
+ ProgressCancellable (Cancellable ))
25
31
import Language.Haskell.LSP.Types
26
32
import Ormolu
27
- import Text.Regex.TDFA.Text ()
33
+ import System.FilePath (takeFileName )
34
+ import Text.Regex.TDFA.Text ()
28
35
29
36
-- ---------------------------------------------------------------------
30
37
@@ -36,24 +43,24 @@ descriptor plId = (defaultPluginDescriptor plId)
36
43
-- ---------------------------------------------------------------------
37
44
38
45
provider :: FormattingProvider IO
39
- provider _lf ideState typ contents fp _ = do
46
+ provider lf ideState typ contents fp _ = withIndefiniteProgress lf title Cancellable $ do
40
47
let
41
- fromDyn :: ParsedModule -> IO [DynOption ]
42
- fromDyn pmod =
48
+ fromDyn :: DynFlags -> IO [DynOption ]
49
+ fromDyn df =
43
50
let
44
- df = ms_hspp_opts $ pm_mod_summary pmod
45
51
pp =
46
52
let p = D. sPgm_F $ D. settings df
47
53
in if null p then [] else [" -pgmF=" <> p]
48
54
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
50
56
in
51
57
return $ map DynOption $ pp <> pm <> ex
52
58
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
55
62
Nothing -> return []
56
- Just pm -> fromDyn pm
63
+ Just df -> fromDyn df
57
64
58
65
let
59
66
fullRegion = RegionIndices Nothing Nothing
@@ -71,7 +78,12 @@ provider _lf ideState typ contents fp _ = do
71
78
in
72
79
ret <$> fmt contents (mkConf fileOpts (rangeRegion sl el))
73
80
where
81
+ title = T. pack $ " Formatting " <> takeFileName (fromNormalizedFilePath fp)
74
82
ret :: Either OrmoluException T. Text -> Either ResponseError (List TextEdit )
75
83
ret (Left err) = Left
76
84
(responseError (T. pack $ " ormoluCmd: " ++ show err) )
77
85
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