11{-# LANGUAGE CPP #-}
2+ {-# LANGUAGE LambdaCase #-}
23{-# LANGUAGE OverloadedStrings #-}
34{-# LANGUAGE TypeApplications #-}
45
@@ -8,8 +9,10 @@ module Ide.Plugin.Ormolu
89 )
910where
1011
11- import Control.Exception (try )
12+ import Control.Exception (Handler (.. ), IOException ,
13+ SomeException (.. ), catches )
1214import Control.Monad.IO.Class (liftIO )
15+ import Data.Functor ((<&>) )
1316import qualified Data.Text as T
1417import Development.IDE hiding (pluginHandlers )
1518import Development.IDE.GHC.Compat (hsc_dflags , moduleNameString )
@@ -44,13 +47,26 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
4447 fullRegion = RegionIndices Nothing Nothing
4548 rangeRegion s e = RegionIndices (Just $ s + 1 ) (Just $ e + 1 )
4649 mkConf o region = defaultConfig { cfgDynOptions = o, cfgRegion = region }
47- fmt :: T. Text -> Config RegionIndices -> IO (Either OrmoluException T. Text )
48- fmt cont conf =
50+ fmt :: T. Text -> Config RegionIndices -> IO (Either SomeException T. Text )
51+ fmt cont conf = flip catches handlers $ do
52+ let fp' = fromNormalizedFilePath fp
4953#if MIN_VERSION_ormolu(0,5,3)
50- try @ OrmoluException $ ormolu conf (fromNormalizedFilePath fp) cont
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
5161#else
52- try @ OrmoluException $ ormolu conf (fromNormalizedFilePath fp) $ T. unpack cont
62+ let conf' = conf
63+ cont' = T. unpack cont
5364#endif
65+ Right <$> ormolu conf' fp' cont'
66+ handlers =
67+ [ Handler $ pure . Left . SomeException @ OrmoluException
68+ , Handler $ pure . Left . SomeException @ IOException
69+ ]
5470
5571 case typ of
5672 FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
@@ -59,7 +75,7 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
5975 where
6076 title = T. pack $ " Formatting " <> takeFileName (fromNormalizedFilePath fp)
6177
62- ret :: Either OrmoluException T. Text -> Either ResponseError (List TextEdit )
78+ ret :: Either SomeException T. Text -> Either ResponseError (List TextEdit )
6379 ret (Left err) = Left . responseError . T. pack $ " ormoluCmd: " ++ show err
6480 ret (Right new) = Right $ makeDiffTextEdit contents new
6581
0 commit comments