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 )
@@ -22,6 +24,11 @@ import Language.LSP.Types
22
24
import Ormolu
23
25
import System.FilePath (takeFileName )
24
26
27
+ #if MIN_VERSION_ormolu(0,5,0)
28
+ import Ormolu.Utils.Cabal (getCabalInfoForSourceFile )
29
+ import Ormolu.Utils.Fixity (getFixityOverridesForSourceFile )
30
+ #endif
31
+
25
32
-- ---------------------------------------------------------------------
26
33
27
34
descriptor :: PluginId -> PluginDescriptor IdeState
@@ -43,9 +50,21 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
43
50
fullRegion = RegionIndices Nothing Nothing
44
51
rangeRegion s e = RegionIndices (Just $ s + 1 ) (Just $ e + 1 )
45
52
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
53
+ fmt :: T. Text -> Config RegionIndices -> IO (Either SomeException T. Text )
54
+ fmt cont conf = flip catches handlers $ do
55
+ let fp' = fromNormalizedFilePath fp
56
+ #if MIN_VERSION_ormolu(0,5,0)
57
+ fixityOverrides <-
58
+ getFixityOverridesForSourceFile =<< getCabalInfoForSourceFile fp'
59
+ let conf' = conf { cfgFixityOverrides = fixityOverrides }
60
+ #else
61
+ let conf' = conf
62
+ #endif
63
+ Right <$> ormolu conf' fp' (T. unpack cont)
64
+ handlers =
65
+ [ Handler $ pure . Left . SomeException @ OrmoluException
66
+ , Handler $ pure . Left . SomeException @ IOException
67
+ ]
49
68
50
69
case typ of
51
70
FormatText -> ret <$> fmt contents (mkConf fileOpts fullRegion)
@@ -54,7 +73,7 @@ provider ideState typ contents fp _ = withIndefiniteProgress title Cancellable $
54
73
where
55
74
title = T. pack $ " Formatting " <> takeFileName (fromNormalizedFilePath fp)
56
75
57
- ret :: Either OrmoluException T. Text -> Either ResponseError (List TextEdit )
76
+ ret :: Either SomeException T. Text -> Either ResponseError (List TextEdit )
58
77
ret (Left err) = Left . responseError . T. pack $ " ormoluCmd: " ++ show err
59
78
ret (Right new) = Right $ makeDiffTextEdit contents new
60
79
0 commit comments