1+ {-# LANGUAGE CPP #-}
2+ {-# LANGUAGE LambdaCase #-}
13{-# LANGUAGE OverloadedStrings #-}
24{-# LANGUAGE TypeApplications #-}
35
@@ -7,8 +9,10 @@ module Ide.Plugin.Ormolu
79 )
810where
911
10- import Control.Exception (try )
12+ import Control.Exception (Handler (.. ), IOException ,
13+ SomeException (.. ), catches )
1114import Control.Monad.IO.Class (liftIO )
15+ import Data.Functor ((<&>) )
1216import qualified Data.Text as T
1317import Development.IDE hiding (pluginHandlers )
1418import Development.IDE.GHC.Compat (hsc_dflags , moduleNameString )
@@ -43,9 +47,26 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
4347 fullRegion = RegionIndices Nothing Nothing
4448 rangeRegion s e = RegionIndices (Just $ s + 1 ) (Just $ e + 1 )
4549 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
50+ fmt :: T. Text -> Config RegionIndices -> IO (Either SomeException T. Text )
51+ fmt cont conf = flip catches handlers $ do
52+ let fp' = fromNormalizedFilePath fp
53+ #if MIN_VERSION_ormolu(0,5,3)
54+ cabalInfo <- getCabalInfoForSourceFile fp' <&> \ case
55+ CabalNotFound -> Nothing
56+ CabalDidNotMention cabalInfo -> Just cabalInfo
57+ CabalFound cabalInfo -> Just cabalInfo
58+ fixityOverrides <- traverse getFixityOverridesForSourceFile cabalInfo
59+ let conf' = refineConfig ModuleSource cabalInfo fixityOverrides conf
60+ cont' = cont
61+ #else
62+ let conf' = conf
63+ cont' = T. unpack cont
64+ #endif
65+ Right <$> ormolu conf' fp' cont'
66+ handlers =
67+ [ Handler $ pure . Left . SomeException @ OrmoluException
68+ , Handler $ pure . Left . SomeException @ IOException
69+ ]
4970
5071 case typ of
5172 FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
@@ -54,7 +75,7 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
5475 where
5576 title = T. pack $ " Formatting " <> takeFileName (fromNormalizedFilePath fp)
5677
57- ret :: Either OrmoluException T. Text -> Either ResponseError (List TextEdit )
78+ ret :: Either SomeException T. Text -> Either ResponseError (List TextEdit )
5879 ret (Left err) = Left . responseError . T. pack $ " ormoluCmd: " ++ show err
5980 ret (Right new) = Right $ makeDiffTextEdit contents new
6081
0 commit comments