1
+ {-# LANGUAGE CPP #-}
1
2
{-# LANGUAGE OverloadedStrings #-}
2
3
{-# LANGUAGE TypeApplications #-}
3
4
@@ -7,7 +8,8 @@ module Ide.Plugin.Ormolu
7
8
)
8
9
where
9
10
10
- import Control.Exception (try )
11
+ import Control.Exception (Handler (.. ), IOException ,
12
+ SomeException (.. ), catches )
11
13
import Control.Monad.IO.Class (liftIO )
12
14
import qualified Data.Text as T
13
15
import Development.IDE hiding (pluginHandlers )
@@ -43,9 +45,21 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
43
45
fullRegion = RegionIndices Nothing Nothing
44
46
rangeRegion s e = RegionIndices (Just $ s + 1 ) (Just $ e + 1 )
45
47
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
48
+ fmt :: T. Text -> Config RegionIndices -> IO (Either SomeException T. Text )
49
+ fmt cont conf = flip catches handlers $ do
50
+ let fp' = fromNormalizedFilePath fp
51
+ #if MIN_VERSION_ormolu(0,6,0)
52
+ cabalInfo <- getCabalInfoForSourceFile fp'
53
+ fixityOverrides <- getFixityOverridesForSourceFile fp'
54
+ let conf' = refineConfig ModuleSource cabalInfo fixityOverrides conf
55
+ #else
56
+ let conf' = conf
57
+ #endif
58
+ Right <$> ormolu conf' fp' (T. unpack cont)
59
+ handlers =
60
+ [ Handler $ pure . Left . SomeException @ OrmoluException
61
+ , Handler $ pure . Left . SomeException @ IOException
62
+ ]
49
63
50
64
case typ of
51
65
FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
@@ -54,7 +68,7 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
54
68
where
55
69
title = T. pack $ " Formatting " <> takeFileName (fromNormalizedFilePath fp)
56
70
57
- ret :: Either OrmoluException T. Text -> Either ResponseError (List TextEdit )
71
+ ret :: Either SomeException T. Text -> Either ResponseError (List TextEdit )
58
72
ret (Left err) = Left . responseError . T. pack $ " ormoluCmd: " ++ show err
59
73
ret (Right new) = Right $ makeDiffTextEdit contents new
60
74
0 commit comments