Skip to content

Commit 77d0429

Browse files
drsoochmergify[bot]jneira
authored
Centralize common Functions. (#2433)
`response`, `handleMaybe`, `handleMaybeM` are three functions that pop up in a variety of plugins. This commit centralizes these three functions into one module, and makes the change across the related plugins. Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Javier Neira <[email protected]>
1 parent 8441ef2 commit 77d0429

File tree

9 files changed

+31
-46
lines changed

9 files changed

+31
-46
lines changed

hls-plugin-api/hls-plugin-api.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ library
4343
, dependent-sum
4444
, Diff ^>=0.4.0
4545
, dlist
46+
, extra
4647
, ghc
4748
, hashable
4849
, hls-graph >=1.4 && < 1.6
@@ -54,6 +55,7 @@ library
5455
, process
5556
, regex-tdfa >=1.3.1.0
5657
, text
58+
, transformers
5759
, unordered-containers
5860

5961
if os(windows)

hls-plugin-api/src/Ide/PluginUtils.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,22 @@ module Ide.PluginUtils
2626
installSigUsr1Handler,
2727
subRange,
2828
usePropertyLsp,
29+
response,
30+
handleMaybe,
31+
handleMaybeM,
2932
)
3033
where
3134

3235

36+
import Control.Monad.Extra (maybeM)
37+
import Control.Monad.Trans.Class (lift)
38+
import Control.Monad.Trans.Except (ExceptT, runExceptT, throwE)
3339
import Data.Algorithm.Diff
3440
import Data.Algorithm.DiffOutput
41+
import Data.Bifunctor (Bifunctor (first))
3542
import Data.Containers.ListUtils (nubOrdOn)
3643
import qualified Data.HashMap.Strict as H
44+
import Data.String (IsString (fromString))
3745
import qualified Data.Text as T
3846
import Ide.Plugin.Config
3947
import Ide.Plugin.Properties
@@ -236,3 +244,15 @@ allLspCmdIds pid commands = concatMap go commands
236244
where
237245
go (plid, cmds) = map (mkLspCmdId pid plid . commandId) cmds
238246

247+
-- ---------------------------------------------------------------------
248+
249+
handleMaybe :: Monad m => e -> Maybe b -> ExceptT e m b
250+
handleMaybe msg = maybe (throwE msg) return
251+
252+
handleMaybeM :: Monad m => e -> m (Maybe b) -> ExceptT e m b
253+
handleMaybeM msg act = maybeM (throwE msg) return $ lift act
254+
255+
response :: Monad m => ExceptT String m a -> m (Either ResponseError a)
256+
response =
257+
fmap (first (\msg -> ResponseError InternalError (fromString msg) Nothing))
258+
. runExceptT

plugins/hls-alternate-number-format-plugin/hls-alternate-number-format-plugin.cabal

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ library
2929
, ghc-boot-th
3030
, hls-graph
3131
, hls-plugin-api >=1.1 && < 1.3
32-
, hls-retrie-plugin
3332
, hie-compat
3433
, lens
3534
, lsp
@@ -41,7 +40,6 @@ library
4140

4241
default-language: Haskell2010
4342
default-extensions:
44-
CPP
4543
LambdaCase
4644
NamedFieldPuns
4745
OverloadedStrings

plugins/hls-alternate-number-format-plugin/src/Ide/Plugin/AlternateNumberFormat.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Ide.Plugin.Conversion (FormatType, alternateFormat,
2323
toFormatTypes)
2424
import Ide.Plugin.Literals (Literal (..), collectLiterals,
2525
getSrcSpan, getSrcText)
26-
import Ide.Plugin.Retrie (handleMaybe, handleMaybeM,
26+
import Ide.PluginUtils (handleMaybe, handleMaybeM,
2727
response)
2828
import Ide.Types
2929
import Language.LSP.Types

plugins/hls-eval-plugin/src/Ide/Plugin/Eval/CodeLens.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ import Ide.Plugin.Eval.Parse.Comments (commentsToSections)
9090
import Ide.Plugin.Eval.Parse.Option (parseSetFlags)
9191
import Ide.Plugin.Eval.Rules (queueForEvaluation)
9292
import Ide.Plugin.Eval.Types
93-
import Ide.Plugin.Eval.Util (asS, gStrictTry, handleMaybe,
94-
handleMaybeM, isLiterate,
95-
logWith, response, response',
96-
timed)
93+
import Ide.Plugin.Eval.Util (asS, gStrictTry, isLiterate,
94+
logWith, response', timed)
95+
import Ide.PluginUtils (handleMaybe, handleMaybeM,
96+
response)
9797
import Ide.Types
9898
import Language.LSP.Server
9999
import Language.LSP.Types hiding

plugins/hls-eval-plugin/src/Ide/Plugin/Eval/Util.hs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,15 @@ module Ide.Plugin.Eval.Util (
77
asS,
88
timed,
99
isLiterate,
10-
handleMaybe,
11-
handleMaybeM,
12-
response,
1310
response',
1411
gStrictTry,
1512
logWith,
1613
) where
1714

1815
import Control.Exception (SomeException, evaluate)
19-
import Control.Monad.Extra (maybeM)
2016
import Control.Monad.IO.Class (MonadIO (liftIO))
21-
import Control.Monad.Trans.Class (lift)
22-
import Control.Monad.Trans.Except (ExceptT (..), runExceptT,
23-
throwE)
17+
import Control.Monad.Trans.Except (ExceptT (..), runExceptT)
2418
import Data.Aeson (Value (Null))
25-
import Data.Bifunctor (first)
2619
import Data.String (IsString (fromString))
2720
import qualified Data.Text as T
2821
import Development.IDE (IdeState, Priority (..),
@@ -71,17 +64,6 @@ logLevel = Debug -- Info
7164
isLiterate :: FilePath -> Bool
7265
isLiterate x = takeExtension x `elem` [".lhs", ".lhs-boot"]
7366

74-
handleMaybe :: Monad m => e -> Maybe b -> ExceptT e m b
75-
handleMaybe msg = maybe (throwE msg) return
76-
77-
handleMaybeM :: Monad m => e -> m (Maybe b) -> ExceptT e m b
78-
handleMaybeM msg act = maybeM (throwE msg) return $ lift act
79-
80-
response :: Functor f => ExceptT String f c -> f (Either ResponseError c)
81-
response =
82-
fmap (first (\msg -> ResponseError InternalError (fromString msg) Nothing))
83-
. runExceptT
84-
8567
response' :: ExceptT String (LspM c) WorkspaceEdit -> LspM c (Either ResponseError Value)
8668
response' act = do
8769
res <- runExceptT act

plugins/hls-rename-plugin/hls-rename-plugin.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ library
2828
, ghcide >=1.4 && <1.6
2929
, hiedb
3030
, hls-plugin-api ^>=1.2
31-
, hls-retrie-plugin >=1.0.1.1
3231
, lsp
3332
, lsp-types
3433
, syb

plugins/hls-rename-plugin/src/Ide/Plugin/Rename.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import Name
2727
#endif
2828
import HieDb.Query
2929
import Ide.Plugin.Config
30-
import Ide.Plugin.Retrie hiding (descriptor)
3130
import Ide.PluginUtils
3231
import Ide.Types
3332
import Language.Haskell.GHC.ExactPrint

plugins/hls-retrie-plugin/src/Ide/Plugin/Retrie.hs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@
1414

1515
{-# OPTIONS -Wno-orphans #-}
1616

17-
module Ide.Plugin.Retrie (descriptor, response, handleMaybe, handleMaybeM) where
17+
module Ide.Plugin.Retrie (descriptor) where
1818

1919
import Control.Concurrent.Extra (readVar)
2020
import Control.Exception.Safe (Exception (..),
2121
SomeException, catch,
2222
throwIO, try)
2323
import Control.Monad (forM, unless)
24-
import Control.Monad.Extra (maybeM)
2524
import Control.Monad.IO.Class (MonadIO (liftIO))
2625
import Control.Monad.Trans.Class (MonadTrans (lift))
27-
import Control.Monad.Trans.Except (ExceptT (..), runExceptT,
28-
throwE)
26+
import Control.Monad.Trans.Except (ExceptT (ExceptT),
27+
runExceptT)
2928
import Control.Monad.Trans.Maybe
3029
import Data.Aeson (FromJSON (..),
3130
ToJSON (..),
@@ -499,20 +498,6 @@ _useRuleStale label state rule f =
499498
-- | Chosen approach for calling ghcide Shake rules
500499
useRule label = _useRuleStale ("Retrie." <> label)
501500

502-
-------------------------------------------------------------------------------
503-
-- Error handling combinators
504-
505-
handleMaybe :: Monad m => e -> Maybe b -> ExceptT e m b
506-
handleMaybe msg = maybe (throwE msg) return
507-
508-
handleMaybeM :: Monad m => e -> m (Maybe b) -> ExceptT e m b
509-
handleMaybeM msg act = maybeM (throwE msg) return $ lift act
510-
511-
response :: Monad m => ExceptT String m a -> m (Either ResponseError a)
512-
response =
513-
fmap (first (\msg -> ResponseError InternalError (fromString msg) Nothing))
514-
. runExceptT
515-
516501
-------------------------------------------------------------------------------
517502
-- Serialization wrappers and instances
518503

0 commit comments

Comments
 (0)