11{-# LANGUAGE RecordWildCards #-}
2- {-# LANGUAGE CPP #-}
32{-# LANGUAGE OverloadedStrings #-}
43{-# LANGUAGE ScopedTypeVariables #-}
54{-# LANGUAGE TypeApplications #-}
@@ -13,10 +12,6 @@ module Ide.Plugin.Ormolu
1312where
1413
1514import Control.Exception
16- import Control.Monad
17- import Data.Char
18- import Data.List
19- import Data.Maybe
2015import qualified Data.Text as T
2116import Development.IDE.Core.Rules
2217import Development.IDE.Types.Diagnostics as D
@@ -25,7 +20,7 @@ import qualified DynFlags as D
2520import qualified EnumSet as S
2621import GHC
2722import Ide.Types
28- import qualified HIE.Bios as BIOS
23+ import Ide.PluginUtils
2924import Ide.Plugin.Formatter
3025import Language.Haskell.LSP.Types
3126import Ormolu
@@ -50,19 +45,7 @@ descriptor plId = PluginDescriptor
5045-- ---------------------------------------------------------------------
5146
5247provider :: FormattingProvider IO
53- #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
48+ provider _lf ideState typ contents fp _ = do
6649 let
6750 fromDyn :: ParsedModule -> IO [DynOption ]
6851 fromDyn pmod =
@@ -76,62 +59,28 @@ provider ideState typ contents fp _ = do
7659 in
7760 return $ map DynOption $ pp <> pm <> ex
7861
79- m_parsed <- runAction ideState $ getParsedModule fp
62+ m_parsed <- runAction " Ormolu " ideState $ getParsedModule fp
8063 fileOpts <- case m_parsed of
8164 Nothing -> return []
8265 Just pm -> fromDyn pm
8366
8467 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)
68+ fullRegion = RegionIndices Nothing Nothing
69+ rangeRegion s e = RegionIndices (Just s) (Just e)
70+ mkConf o region = defaultConfig { cfgDynOptions = o, cfgRegion = region }
71+ fmt :: T. Text -> Config RegionIndices -> IO (Either OrmoluException T. Text )
72+ fmt cont conf =
73+ try @ OrmoluException (ormolu conf (fromNormalizedFilePath fp) $ T. unpack cont)
8974
9075 case typ of
91- FormatText -> ret (fullRange contents) <$> fmt contents cradleOpts
76+ FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
9277 FormatRange r ->
9378 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)
79+ Range (Position sl _) (Position el _) = normalize r
11680 in
117- maybe err fmt' mStrip
81+ ret <$> fmt contents (mkConf fileOpts (rangeRegion sl el))
11882 where
119- ret :: Range -> Either OrmoluException T. Text -> Either ResponseError (List TextEdit )
120- ret _ (Left err) = Left
83+ ret :: Either OrmoluException T. Text -> Either ResponseError (List TextEdit )
84+ ret (Left err) = Left
12185 (responseError (T. pack $ " ormoluCmd: " ++ show err) )
122- ret r (Right new) = Right (List [TextEdit r new])
123-
124- #else
125- provider _ _ _ _ = return $ Right [] -- NOP formatter
126- #endif
127-
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- -- ---------------------------------------------------------------------
86+ ret (Right new) = Right (makeDiffTextEdit contents new)
0 commit comments