Skip to content

Commit fdde5b8

Browse files
committed
Merge definitions from all plugins for Document(Type)Definition message
- enables multiple plugins to provide Document(Type)Definition for the same message
1 parent c0f7d4c commit fdde5b8

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
{-# LANGUAGE MonadComprehensions #-}
1414
{-# LANGUAGE MultiParamTypeClasses #-}
1515
{-# LANGUAGE NamedFieldPuns #-}
16-
{-# LANGUAGE OverloadedLabels #-}
1716
{-# LANGUAGE OverloadedStrings #-}
1817
{-# LANGUAGE PatternSynonyms #-}
1918
{-# LANGUAGE PolyKinds #-}
@@ -76,6 +75,7 @@ import Data.Default
7675
import Data.Dependent.Map (DMap)
7776
import qualified Data.Dependent.Map as DMap
7877
import qualified Data.DList as DList
78+
import Data.Foldable (foldl')
7979
import Data.GADT.Compare
8080
import Data.Hashable (Hashable)
8181
import Data.HashMap.Strict (HashMap)
@@ -560,7 +560,7 @@ instance PluginRequestMethod Method_TextDocumentCodeAction where
560560
-- should check whether the requested kind is a *prefix* of the action kind.
561561
-- That means, for example, we will return actions with kinds `quickfix.import` and
562562
-- `quickfix.somethingElse` if the requested kind is `quickfix`.
563-
, Just caKind <- ca ^. L.kind = any (\k -> k `codeActionKindSubsumes` caKind) allowed
563+
, Just caKind <- ca ^. L.kind = any (`codeActionKindSubsumes` caKind) allowed
564564
| otherwise = False
565565

566566
instance PluginRequestMethod Method_CodeActionResolve where
@@ -569,10 +569,10 @@ instance PluginRequestMethod Method_CodeActionResolve where
569569
combineResponses _ _ _ _ (x :| _) = x
570570

571571
instance PluginRequestMethod Method_TextDocumentDefinition where
572-
combineResponses _ _ _ _ (x :| _) = x
572+
combineResponses _ _ _ _ (x :| xs) = foldl' mergeDefinitions x xs
573573

574574
instance PluginRequestMethod Method_TextDocumentTypeDefinition where
575-
combineResponses _ _ _ _ (x :| _) = x
575+
combineResponses _ _ _ _ (x :| xs) = foldl' mergeDefinitions x xs
576576

577577
instance PluginRequestMethod Method_TextDocumentDocumentHighlight where
578578

@@ -693,6 +693,24 @@ nullToMaybe' :: (a |? (b |? Null)) -> Maybe (a |? b)
693693
nullToMaybe' (InL x) = Just $ InL x
694694
nullToMaybe' (InR (InL x)) = Just $ InR x
695695
nullToMaybe' (InR (InR _)) = Nothing
696+
697+
type Definitions = (Definition |? ([DefinitionLink] |? Null))
698+
699+
mergeDefinitions :: Definitions -> Definitions -> Definitions
700+
mergeDefinitions definitions1 definitions2 = case (definitions1, definitions2) of
701+
(InR (InR Null), def2) -> def2
702+
(def1, InR (InR Null)) -> def1
703+
(InL def1, InL def2) -> InR $ InL (defToLinks def1 ++ defToLinks def2)
704+
(InL def1, InR (InL links)) -> InR $ InL (defToLinks def1 ++ links)
705+
(InR (InL links), InL def2) -> InR $ InL (links ++ defToLinks def2)
706+
(InR (InL links1), InR (InL links2)) -> InR $ InL (links1 ++ links2)
707+
where
708+
defToLinks :: Definition -> [DefinitionLink]
709+
defToLinks (Definition (InL location)) = [DefinitionLink $ locationToLocationLink location]
710+
defToLinks (Definition (InR locations)) = map (DefinitionLink . locationToLocationLink) locations
711+
712+
locationToLocationLink :: Location -> LocationLink
713+
locationToLocationLink Location{_uri, _range} = LocationLink{_originSelectionRange = Just _range, _targetUri = _uri, _targetRange = _range, _targetSelectionRange = _range}
696714
-- ---------------------------------------------------------------------
697715
-- Plugin Notifications
698716
-- ---------------------------------------------------------------------
@@ -942,7 +960,7 @@ mkResolveHandler m f = mkPluginHandler m $ \ideState plId params -> do
942960
-- as this is filtered out in `pluginEnabled`
943961
_ -> throwError $ PluginInternalError invalidRequest
944962
where invalidRequest = "The resolve request incorrectly got routed to the wrong resolve handler!"
945-
parseError value err = "Unable to decode: " <> (T.pack $ show value) <> ". Error: " <> (T.pack $ show err)
963+
parseError value err = "Unable to decode: " <> T.pack (show value) <> ". Error: " <> T.pack (show err)
946964

947965
wrapResolveData :: L.HasData_ a (Maybe Value) => PluginId -> Uri -> a -> a
948966
wrapResolveData pid uri hasData =

0 commit comments

Comments
 (0)