Skip to content

Commit 15f7862

Browse files
committed
Linearize hover handler using MaybeT
1 parent 6c4189c commit 15f7862

File tree

1 file changed

+23
-16
lines changed
  • plugins/hls-cabal-plugin/src/Ide/Plugin

1 file changed

+23
-16
lines changed

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal.hs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import Control.Lens ((^.))
1212
import Control.Monad.Extra
1313
import Control.Monad.IO.Class
1414
import Control.Monad.Trans.Class (lift)
15-
import Control.Monad.Trans.Maybe (runMaybeT)
15+
import Control.Monad.Trans.Maybe (MaybeT (..),
16+
hoistMaybe,
17+
runMaybeT)
1618
import qualified Data.ByteString as BS
1719
import Data.Hashable
1820
import Data.HashMap.Strict (HashMap)
@@ -349,22 +351,27 @@ cabalAddCodeAction state plId (CodeActionParams _ _ (TextDocumentIdentifier uri)
349351
-- If found that the filtered hover message is a dependency,
350352
-- adds a Documentation link.
351353
hover :: PluginMethodHandler IdeState LSP.Method_TextDocumentHover
352-
hover ide _ msgParam = do
353-
nfp <- getNormalizedFilePathE uri
354-
cabalFields <- runActionE "cabal.cabal-hover" ide $ useE ParseCabalFields nfp
355-
case CabalFields.findTextWord cursor cabalFields of
356-
Nothing ->
357-
pure $ InR Null
358-
Just cursorText -> do
359-
gpd <- runActionE "cabal.GPD" ide $ useE ParseCabalFile nfp
360-
let depsNames = map dependencyName $ allBuildDepends $ flattenPackageDescription gpd
361-
case filterVersion cursorText of
362-
Nothing -> pure $ InR Null
363-
Just txt ->
364-
if txt `elem` depsNames
365-
then pure $ foundHover (Nothing, [txt <> "\n", documentationText txt])
366-
else pure $ InR Null
354+
hover ide _ msgParam = getHoverMessage >>= showHoverMessage
367355
where
356+
-- Return the tooltip content for a hovered name...
357+
getHoverMessage = runMaybeT $ do
358+
nfp <- lift $ getNormalizedFilePathE uri
359+
cabalFields <- lift $ runActionE "cabal.cabal-hover" ide $ useE ParseCabalFields nfp
360+
-- ... at the cursor position...
361+
cursorText <- hoistMaybe $ CabalFields.findTextWord cursor cabalFields
362+
-- ... without any version information...
363+
txt <- hoistMaybe $ filterVersion cursorText
364+
-- ... and only if it's a listed depdendency.
365+
gpd <- lift $ runActionE "cabal.GPD" ide $ useE ParseCabalFile nfp
366+
let depsNames = map dependencyName $ allBuildDepends $ flattenPackageDescription gpd
367+
guard $ txt `elem` depsNames
368+
369+
pure [txt <> "\n", documentationText txt]
370+
371+
showHoverMessage = \case
372+
Nothing -> pure $ InR Null
373+
Just message -> pure $ foundHover (Nothing, message)
374+
368375
cursor = Types.lspPositionToCabalPosition (msgParam ^. JL.position)
369376
uri = msgParam ^. JL.textDocument . JL.uri
370377

0 commit comments

Comments
 (0)