@@ -20,9 +20,13 @@ import Data.Char
20
20
import Data.List
21
21
import Data.Maybe
22
22
import qualified Data.Text as T
23
+ import GHC
23
24
import Ormolu
24
25
import Haskell.Ide.Engine.PluginUtils
26
+ import Haskell.Ide.Engine.Support.HieExtras
25
27
import HIE.Bios.Types
28
+ import qualified DynFlags as D
29
+ import qualified EnumSet as S
26
30
#endif
27
31
28
32
ormoluDescriptor :: PluginId -> PluginDescriptor
@@ -44,62 +48,58 @@ provider :: FormattingProvider
44
48
{-# LANGUAGE BlockArguments #-}
45
49
provider contents uri typ _ = pluginGetFile contents uri $ \ fp -> do
46
50
opts <- lookupComponentOptions fp
47
- let opts' =
51
+ let cradleOpts =
48
52
map DynOption
49
53
$ filter exop
50
54
$ join
51
55
$ maybeToList
52
56
$ componentOptions
53
57
<$> opts
54
- conf = Config opts' False False True False
55
- fmt :: T. Text -> IdeM (Either OrmoluException T. Text )
56
- fmt cont = liftIO $ try @ OrmoluException (ormolu conf fp $ T. unpack cont)
58
+
59
+ fromDyn tcm _ () =
60
+ let
61
+ df = getDynFlags tcm
62
+ pp =
63
+ let p = D. sPgm_F $ D. settings df
64
+ in if null p then [] else [" -pgmF=" <> p]
65
+ pm = map ((" -fplugin=" <> ) . moduleNameString) $ D. pluginModNames df
66
+ ex = map ((" -X" <> ) . show ) $ S. toList $ D. extensionFlags df
67
+ in
68
+ return $ map DynOption $ pp <> pm <> ex
69
+ fileOpts <- ifCachedModuleAndData fp cradleOpts fromDyn
70
+ let
71
+ conf o = Config o False False True False
72
+ fmt :: T. Text -> [DynOption ] -> IdeM (Either OrmoluException T. Text )
73
+ fmt cont o =
74
+ liftIO $ try @ OrmoluException (ormolu (conf o) fp $ T. unpack cont)
57
75
58
76
case typ of
59
- FormatText -> ret (fullRange contents) <$> fmt contents
77
+ FormatText -> ret (fullRange contents) <$> fmt contents cradleOpts
60
78
FormatRange r ->
61
79
let
62
80
txt = T. lines $ extractRange r contents
63
81
lineRange (Range (Position sl _) (Position el _)) =
64
82
Range (Position sl 0 ) $ Position el $ T. length $ last txt
65
- -- Pragmas will not be picked up in a non standard location,
66
- -- or when range starts on a Pragma
67
- extPragmas = takeWhile (" {-#" `T.isPrefixOf` )
68
- pragmas =
69
- let cp = extPragmas $ T. lines contents
70
- rp = not $ null $ extPragmas txt
71
- in if null cp || rp
72
- then []
73
- -- head txt is safe when extractRange txt is safe
74
- else cp <> if T. all isSpace $ head txt then [] else [" " ]
75
83
fixLine t = if T. all isSpace $ last txt then t else T. init t
76
84
unStrip ws new =
77
- fixLine
78
- $ T. unlines
79
- $ map (ws `T.append` )
80
- $ drop (length pragmas)
81
- $ T. lines new
85
+ fixLine $ T. unlines $ map (ws `T.append` ) $ T. lines new
82
86
mStrip = case txt of
83
87
(l : _) ->
84
88
let ws = fst $ T. span isSpace l
85
89
in (,) ws . T. unlines <$> traverse (T. stripPrefix ws) txt
86
90
_ -> Nothing
87
- in
88
- maybe
89
- (return $ IdeResultFail
90
- (IdeError
91
- PluginError
92
- (T. pack
93
- " You must format a whole block of code. Ormolu does not support arbitrary ranges."
94
- )
95
- Null
91
+ err = return $ IdeResultFail
92
+ (IdeError
93
+ PluginError
94
+ (T. pack
95
+ " You must format a whole block of code. Ormolu does not support arbitrary ranges."
96
96
)
97
+ Null
97
98
)
98
- (\ (ws, striped) ->
99
- ret (lineRange r)
100
- <$> (fmap (unStrip ws) <$> fmt (T. unlines pragmas <> striped))
101
- )
102
- mStrip
99
+ fmt' (ws, striped) =
100
+ ret (lineRange r) <$> (fmap (unStrip ws) <$> fmt striped fileOpts)
101
+ in
102
+ maybe err fmt' mStrip
103
103
where
104
104
ret _ (Left err) = IdeResultFail
105
105
(IdeError PluginError (T. pack $ " ormoluCmd: " ++ show err) Null )
0 commit comments