1+ {-# LANGUAGE CPP #-}
12{-# LANGUAGE OverloadedStrings #-}
23{-# LANGUAGE TypeApplications #-}
34
@@ -7,7 +8,8 @@ module Ide.Plugin.Ormolu
78 )
89where
910
10- import Control.Exception (try )
11+ import Control.Exception (Handler (.. ), IOException ,
12+ SomeException (.. ), catches )
1113import Control.Monad.IO.Class (liftIO )
1214import qualified Data.Text as T
1315import Development.IDE hiding (pluginHandlers )
@@ -22,6 +24,11 @@ import Language.LSP.Types
2224import Ormolu
2325import System.FilePath (takeFileName )
2426
27+ #if MIN_VERSION_ormolu(0,5,0)
28+ import Ormolu.Utils.Cabal (getCabalInfoForSourceFile )
29+ import Ormolu.Utils.Fixity (getFixityOverridesForSourceFile )
30+ #endif
31+
2532-- ---------------------------------------------------------------------
2633
2734descriptor :: PluginId -> PluginDescriptor IdeState
@@ -43,9 +50,21 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
4350 fullRegion = RegionIndices Nothing Nothing
4451 rangeRegion s e = RegionIndices (Just $ s + 1 ) (Just $ e + 1 )
4552 mkConf o region = defaultConfig { cfgDynOptions = o, cfgRegion = region }
46- fmt :: T. Text -> Config RegionIndices -> IO (Either OrmoluException T. Text )
47- fmt cont conf =
48- try @ OrmoluException $ ormolu conf (fromNormalizedFilePath fp) $ T. unpack cont
53+ fmt :: T. Text -> Config RegionIndices -> IO (Either SomeException T. Text )
54+ fmt cont conf = flip catches handlers $ do
55+ let fp' = fromNormalizedFilePath fp
56+ #if MIN_VERSION_ormolu(0,5,0)
57+ fixityOverrides <-
58+ getFixityOverridesForSourceFile =<< getCabalInfoForSourceFile fp'
59+ let conf' = conf { cfgFixityOverrides = fixityOverrides }
60+ #else
61+ let conf' = conf
62+ #endif
63+ Right <$> ormolu conf' fp' (T. unpack cont)
64+ handlers =
65+ [ Handler $ pure . Left . SomeException @ OrmoluException
66+ , Handler $ pure . Left . SomeException @ IOException
67+ ]
4968
5069 case typ of
5170 FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
@@ -54,7 +73,7 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
5473 where
5574 title = T. pack $ " Formatting " <> takeFileName (fromNormalizedFilePath fp)
5675
57- ret :: Either OrmoluException T. Text -> Either ResponseError (List TextEdit )
76+ ret :: Either SomeException T. Text -> Either ResponseError (List TextEdit )
5877 ret (Left err) = Left . responseError . T. pack $ " ormoluCmd: " ++ show err
5978 ret (Right new) = Right $ makeDiffTextEdit contents new
6079
0 commit comments