@@ -13,10 +13,6 @@ module Ide.Plugin.Ormolu
1313where
1414
1515import Control.Exception
16- import Control.Monad
17- import Data.Char
18- import Data.List
19- import Data.Maybe
2016import qualified Data.Text as T
2117import Development.IDE.Core.Rules
2218import Development.IDE.Types.Diagnostics as D
@@ -25,7 +21,7 @@ import qualified DynFlags as D
2521import qualified EnumSet as S
2622import GHC
2723import Ide.Types
28- import qualified HIE.Bios as BIOS
24+ import Ide.PluginUtils
2925import Ide.Plugin.Formatter
3026import Language.Haskell.LSP.Types
3127import Ormolu
@@ -51,18 +47,7 @@ descriptor plId = PluginDescriptor
5147
5248provider :: FormattingProvider IO
5349#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
6651 let
6752 fromDyn :: ParsedModule -> IO [DynOption ]
6853 fromDyn pmod =
@@ -82,56 +67,31 @@ provider ideState typ contents fp _ = do
8267 Just pm -> fromDyn pm
8368
8469 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)
8976
9077 case typ of
91- FormatText -> ret (fullRange contents) <$> fmt contents cradleOpts
78+ FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
9279 FormatRange r ->
9380 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
11682 in
117- maybe err fmt' mStrip
83+ ret <$> fmt contents (mkConf fileOpts (rangeRegion sl el))
11884 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
12187 (responseError (T. pack $ " ormoluCmd: " ++ show err) )
122- ret r (Right new) = Right (List [ TextEdit r new] )
88+ ret (Right new) = Right (makeDiffTextEdit contents new)
12389
12490#else
12591provider _ _ _ _ = return $ Right [] -- NOP formatter
12692#endif
12793
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