Skip to content

Commit 7f8ee62

Browse files
committed
get(Type)Definition with its Identifier
1 parent 6a51da6 commit 7f8ee62

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

ghcide/src/Development/IDE/Core/Actions.hs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,30 @@ toCurrentLocations mapping file = mapMaybeM go
9898
nUri = toNormalizedUri uri
9999

100100
-- | Goto Definition.
101-
getDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [Location])
101+
getDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [(Location, Identifier)])
102102
getDefinition file pos = runMaybeT $ do
103103
ide@ShakeExtras{ withHieDb, hiedbWriter } <- ask
104104
opts <- liftIO $ getIdeOptionsIO ide
105105
(HAR _ hf _ _ _, mapping) <- useWithStaleFastMT GetHieAst file
106106
(ImportMap imports, _) <- useWithStaleFastMT GetImportMap file
107107
!pos' <- MaybeT (pure $ fromCurrentPosition mapping pos)
108-
locations <- AtPoint.gotoDefinition withHieDb (lookupMod hiedbWriter) opts imports hf pos'
109-
MaybeT $ Just <$> toCurrentLocations mapping file locations
108+
locationsWithIdentifier <- AtPoint.gotoDefinition withHieDb (lookupMod hiedbWriter) opts imports hf pos'
109+
MaybeT $ do
110+
let (locations, names) = unzip locationsWithIdentifier
111+
curLocations <- toCurrentLocations mapping file locations
112+
pure (Just $ zip curLocations names)
110113

111-
getTypeDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [Location])
114+
getTypeDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [(Location, Identifier)])
112115
getTypeDefinition file pos = runMaybeT $ do
113116
ide@ShakeExtras{ withHieDb, hiedbWriter } <- ask
114117
opts <- liftIO $ getIdeOptionsIO ide
115118
(hf, mapping) <- useWithStaleFastMT GetHieAst file
116119
!pos' <- MaybeT (return $ fromCurrentPosition mapping pos)
117-
locations <- AtPoint.gotoTypeDefinition withHieDb (lookupMod hiedbWriter) opts hf pos'
118-
MaybeT $ Just <$> toCurrentLocations mapping file locations
120+
locationsWithIdentifier <- AtPoint.gotoTypeDefinition withHieDb (lookupMod hiedbWriter) opts hf pos'
121+
MaybeT $ do
122+
let (locations, names) = unzip locationsWithIdentifier
123+
curLocations <- toCurrentLocations mapping file locations
124+
pure (Just $ zip curLocations names)
119125

120126
highlightAtPoint :: NormalizedFilePath -> Position -> IdeAction (Maybe [DocumentHighlight])
121127
highlightAtPoint file pos = runMaybeT $ do

ghcide/src/Development/IDE/GHC/Compat/Core.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ instance HasSrcSpan (EpAnn a) where
633633
#if MIN_VERSION_ghc(9,9,0)
634634
instance HasSrcSpan (SrcLoc.GenLocated (EpAnn ann) a) where
635635
getLoc (L l _) = getLoc l
636+
instance HasSrcSpan (SrcLoc.GenLocated (GHC.EpaLocation) a) where
637+
getLoc = GHC.getHasLoc
636638
#else
637639
instance HasSrcSpan (SrcSpanAnn' ann) where
638640
getLoc = GHC.locA

ghcide/src/Development/IDE/LSP/HoverDefinition.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ gotoDefinition :: Recorder (WithPriority Log) -> IdeState -> TextDocumentPos
4747
hover :: Recorder (WithPriority Log) -> IdeState -> TextDocumentPositionParams -> ExceptT PluginError (HandlerM c) (Hover |? Null)
4848
gotoTypeDefinition :: Recorder (WithPriority Log) -> IdeState -> TextDocumentPositionParams -> ExceptT PluginError (HandlerM c) (MessageResult Method_TextDocumentTypeDefinition)
4949
documentHighlight :: Recorder (WithPriority Log) -> IdeState -> TextDocumentPositionParams -> ExceptT PluginError (HandlerM c) ([DocumentHighlight] |? Null)
50-
gotoDefinition = request "Definition" getDefinition (InR $ InR Null) (InL . Definition. InR)
51-
gotoTypeDefinition = request "TypeDefinition" getTypeDefinition (InR $ InR Null) (InL . Definition. InR)
50+
gotoDefinition = request "Definition" getDefinition (InR $ InR Null) (InL . Definition. InR . map fst)
51+
gotoTypeDefinition = request "TypeDefinition" getTypeDefinition (InR $ InR Null) (InL . Definition. InR . map fst)
5252
hover = request "Hover" getAtPoint (InR Null) foundHover
5353
documentHighlight = request "DocumentHighlight" highlightAtPoint (InR Null) InL
5454

ghcide/src/Development/IDE/Spans/AtPoint.hs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ gotoTypeDefinition
186186
-> IdeOptions
187187
-> HieAstResult
188188
-> Position
189-
-> MaybeT m [Location]
189+
-> MaybeT m [(Location, Identifier)]
190190
gotoTypeDefinition withHieDb lookupModule ideOpts srcSpans pos
191191
= lift $ typeLocationsAtPoint withHieDb lookupModule ideOpts pos srcSpans
192192

@@ -199,7 +199,7 @@ gotoDefinition
199199
-> M.Map ModuleName NormalizedFilePath
200200
-> HieASTs a
201201
-> Position
202-
-> MaybeT m [Location]
202+
-> MaybeT m [(Location, Identifier)]
203203
gotoDefinition withHieDb getHieFile ideOpts imports srcSpans pos
204204
= lift $ locationsAtPoint withHieDb getHieFile ideOpts imports pos srcSpans
205205

@@ -314,7 +314,7 @@ typeLocationsAtPoint
314314
-> IdeOptions
315315
-> Position
316316
-> HieAstResult
317-
-> m [Location]
317+
-> m [(Location, Identifier)]
318318
typeLocationsAtPoint withHieDb lookupModule _ideOptions pos (HAR _ ast _ _ hieKind) =
319319
case hieKind of
320320
HieFromDisk hf ->
@@ -332,12 +332,12 @@ typeLocationsAtPoint withHieDb lookupModule _ideOptions pos (HAR _ ast _ _ hieKi
332332
HQualTy a b -> getTypes' [a,b]
333333
HCastTy a -> getTypes' [a]
334334
_ -> []
335-
in fmap nubOrd $ concatMapM (fmap (fromMaybe []) . nameToLocation withHieDb lookupModule) (getTypes' ts)
335+
in fmap nubOrd $ concatMapM (\n -> fmap (maybe [] (fmap (,Right n))) (nameToLocation withHieDb lookupModule n)) (getTypes' ts)
336336
HieFresh ->
337337
let ts = concat $ pointCommand ast pos getts
338338
getts x = nodeType ni ++ (mapMaybe identType $ M.elems $ nodeIdentifiers ni)
339339
where ni = nodeInfo x
340-
in fmap nubOrd $ concatMapM (fmap (fromMaybe []) . nameToLocation withHieDb lookupModule) (getTypes ts)
340+
in fmap nubOrd $ concatMapM (\n -> fmap (maybe [] (fmap (,Right n))) (nameToLocation withHieDb lookupModule n)) (getTypes ts)
341341

342342
namesInType :: Type -> [Name]
343343
namesInType (TyVarTy n) = [varName n]
@@ -361,13 +361,16 @@ locationsAtPoint
361361
-> M.Map ModuleName NormalizedFilePath
362362
-> Position
363363
-> HieASTs a
364-
-> m [Location]
364+
-> m [(Location, Identifier)]
365365
locationsAtPoint withHieDb lookupModule _ideOptions imports pos ast =
366366
let ns = concat $ pointCommand ast pos (M.keys . getNodeIds)
367367
zeroPos = Position 0 0
368368
zeroRange = Range zeroPos zeroPos
369-
modToLocation m = fmap (\fs -> pure $ Location (fromNormalizedUri $ filePathToUri' fs) zeroRange) $ M.lookup m imports
370-
in fmap (nubOrd . concat) $ mapMaybeM (either (pure . modToLocation) $ nameToLocation withHieDb lookupModule) ns
369+
modToLocation m = fmap (\fs -> pure (Location (fromNormalizedUri $ filePathToUri' fs) zeroRange)) $ M.lookup m imports
370+
in fmap (nubOrd . concat) $ mapMaybeM
371+
(either (\m -> pure ((fmap $ fmap (,Left m)) (modToLocation m)))
372+
(\n -> fmap (fmap $ fmap (,Right n)) (nameToLocation withHieDb lookupModule n)))
373+
ns
371374

372375
-- | Given a 'Name' attempt to find the location where it is defined.
373376
nameToLocation :: MonadIO m => WithHieDb -> LookupModule m -> Name -> m (Maybe [Location])

0 commit comments

Comments
 (0)