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