@@ -13,10 +13,6 @@ module Ide.Plugin.Ormolu
13
13
where
14
14
15
15
import Control.Exception
16
- import Control.Monad
17
- import Data.Char
18
- import Data.List
19
- import Data.Maybe
20
16
import qualified Data.Text as T
21
17
import Development.IDE.Core.Rules
22
18
import Development.IDE.Types.Diagnostics as D
@@ -25,7 +21,7 @@ import qualified DynFlags as D
25
21
import qualified EnumSet as S
26
22
import GHC
27
23
import Ide.Types
28
- import qualified HIE.Bios as BIOS
24
+ import Ide.PluginUtils
29
25
import Ide.Plugin.Formatter
30
26
import Language.Haskell.LSP.Types
31
27
import Ormolu
@@ -51,18 +47,7 @@ descriptor plId = PluginDescriptor
51
47
52
48
provider :: FormattingProvider IO
53
49
#if __GLASGOW_HASKELL__ >= 806
54
- provider ideState typ contents fp _ = do
55
- let
56
- exop s =
57
- " -X" `isPrefixOf` s || " -fplugin=" `isPrefixOf` s || " -pgmF=" `isPrefixOf` s
58
- opts <- lookupBiosComponentOptions fp
59
- let cradleOpts =
60
- map DynOption
61
- $ filter exop
62
- $ join
63
- $ maybeToList
64
- $ BIOS. componentOptions
65
- <$> opts
50
+ provider _lf ideState typ contents fp _ = do
66
51
let
67
52
fromDyn :: ParsedModule -> IO [DynOption ]
68
53
fromDyn pmod =
@@ -82,56 +67,31 @@ provider ideState typ contents fp _ = do
82
67
Just pm -> fromDyn pm
83
68
84
69
let
85
- conf o = Config o False False True False
86
- fmt :: T. Text -> [DynOption ] -> IO (Either OrmoluException T. Text )
87
- fmt cont o =
88
- try @ OrmoluException (ormolu (conf o) (fromNormalizedFilePath fp) $ T. unpack cont)
70
+ fullRegion = RegionIndices Nothing Nothing
71
+ rangeRegion s e = RegionIndices (Just s) (Just e)
72
+ mkConf o region = defaultConfig { cfgDynOptions = o, cfgRegion = region }
73
+ fmt :: T. Text -> Config RegionIndices -> IO (Either OrmoluException T. Text )
74
+ fmt cont conf =
75
+ try @ OrmoluException (ormolu conf (fromNormalizedFilePath fp) $ T. unpack cont)
89
76
90
77
case typ of
91
- FormatText -> ret (fullRange contents) <$> fmt contents cradleOpts
78
+ FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
92
79
FormatRange r ->
93
80
let
94
- txt = T. lines $ extractRange r contents
95
- lineRange (Range (Position sl _) (Position el _)) =
96
- Range (Position sl 0 ) $ Position el $ T. length $ last txt
97
- hIsSpace (h : _) = T. all isSpace h
98
- hIsSpace _ = True
99
- fixS t = if hIsSpace txt && (not $ hIsSpace t) then " " : t else t
100
- fixE t = if T. all isSpace $ last txt then t else T. init t
101
- unStrip :: T. Text -> T. Text -> T. Text
102
- unStrip ws new =
103
- fixE $ T. unlines $ map (ws `T.append` ) $ fixS $ T. lines new
104
- mStrip :: Maybe (T. Text , T. Text )
105
- mStrip = case txt of
106
- (l : _) ->
107
- let ws = fst $ T. span isSpace l
108
- in (,) ws . T. unlines <$> traverse (T. stripPrefix ws) txt
109
- _ -> Nothing
110
- err :: IO (Either ResponseError (List TextEdit ))
111
- err = return $ Left $ responseError
112
- $ T. pack " You must format a whole block of code. Ormolu does not support arbitrary ranges."
113
- fmt' :: (T. Text , T. Text ) -> IO (Either ResponseError (List TextEdit ))
114
- fmt' (ws, striped) =
115
- ret (lineRange r) <$> (fmap (unStrip ws) <$> fmt striped fileOpts)
81
+ Range (Position sl _) (Position el _) = normalize r
116
82
in
117
- maybe err fmt' mStrip
83
+ ret <$> fmt contents (mkConf fileOpts (rangeRegion sl el))
118
84
where
119
- ret :: Range -> Either OrmoluException T. Text -> Either ResponseError (List TextEdit )
120
- ret _ (Left err) = Left
85
+ ret :: Either OrmoluException T. Text -> Either ResponseError (List TextEdit )
86
+ ret (Left err) = Left
121
87
(responseError (T. pack $ " ormoluCmd: " ++ show err) )
122
- ret r (Right new) = Right (List [ TextEdit r new] )
88
+ ret (Right new) = Right (makeDiffTextEdit contents new)
123
89
124
90
#else
125
91
provider _ _ _ _ = return $ Right [] -- NOP formatter
126
92
#endif
127
93
128
- -- ---------------------------------------------------------------------
129
-
130
- -- | Find the cradle wide 'ComponentOptions' that apply to a 'FilePath'
131
- lookupBiosComponentOptions :: (Monad m ) => NormalizedFilePath -> m (Maybe BIOS. ComponentOptions )
132
- lookupBiosComponentOptions _fp = do
133
- -- gmc <- getModuleCache
134
- -- return $ lookupInCache fp gmc (const Just) (Just . compOpts) Nothing
135
- return Nothing
136
-
137
- -- ---------------------------------------------------------------------
94
+ -- | Extend to the line below and above to replace newline character.
95
+ normalize :: Range -> Range
96
+ normalize (Range (Position sl _) (Position el _)) =
97
+ Range (Position sl 0 ) (Position (el + 1 ) 0 )
0 commit comments