1
1
{-# LANGUAGE CPP #-}
2
+ {-# LANGUAGE LambdaCase #-}
2
3
{-# LANGUAGE OverloadedStrings #-}
3
4
{-# LANGUAGE TypeApplications #-}
4
5
@@ -8,8 +9,10 @@ module Ide.Plugin.Ormolu
8
9
)
9
10
where
10
11
11
- import Control.Exception (try )
12
+ import Control.Exception (Handler (.. ), IOException ,
13
+ SomeException (.. ), catches )
12
14
import Control.Monad.IO.Class (liftIO )
15
+ import Data.Functor ((<&>) )
13
16
import qualified Data.Text as T
14
17
import Development.IDE hiding (pluginHandlers )
15
18
import Development.IDE.GHC.Compat (hsc_dflags , moduleNameString )
@@ -44,13 +47,26 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
44
47
fullRegion = RegionIndices Nothing Nothing
45
48
rangeRegion s e = RegionIndices (Just $ s + 1 ) (Just $ e + 1 )
46
49
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
49
53
#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
51
61
#else
52
- try @ OrmoluException $ ormolu conf (fromNormalizedFilePath fp) $ T. unpack cont
62
+ let conf' = conf
63
+ cont' = T. unpack cont
53
64
#endif
65
+ Right <$> ormolu conf' fp' cont'
66
+ handlers =
67
+ [ Handler $ pure . Left . SomeException @ OrmoluException
68
+ , Handler $ pure . Left . SomeException @ IOException
69
+ ]
54
70
55
71
case typ of
56
72
FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
@@ -59,7 +75,7 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
59
75
where
60
76
title = T. pack $ " Formatting " <> takeFileName (fromNormalizedFilePath fp)
61
77
62
- ret :: Either OrmoluException T. Text -> Either ResponseError (List TextEdit )
78
+ ret :: Either SomeException T. Text -> Either ResponseError (List TextEdit )
63
79
ret (Left err) = Left . responseError . T. pack $ " ormoluCmd: " ++ show err
64
80
ret (Right new) = Right $ makeDiffTextEdit contents new
65
81
0 commit comments