@@ -10,6 +10,7 @@ module Development.IDE.Spans.AtPoint (
10
10
atPoint
11
11
, gotoDefinition
12
12
, gotoTypeDefinition
13
+ , gotoImplementation
13
14
, documentHighlight
14
15
, pointCommand
15
16
, referencesAtPoint
@@ -214,6 +215,20 @@ gotoDefinition
214
215
gotoDefinition withHieDb getHieFile ideOpts imports srcSpans pos
215
216
= lift $ locationsAtPoint withHieDb getHieFile ideOpts imports pos srcSpans
216
217
218
+ -- | Locate the implementation definition of the name at a given position.
219
+ -- Finds the implementation for a overloaded function.
220
+ gotoImplementation
221
+ :: MonadIO m
222
+ => WithHieDb
223
+ -> LookupModule m
224
+ -> IdeOptions
225
+ -> M. Map ModuleName NormalizedFilePath
226
+ -> HieAstResult
227
+ -> Position
228
+ -> MaybeT m [Location ]
229
+ gotoImplementation withHieDb getHieFile ideOpts imports srcSpans pos
230
+ = lift $ instanceLocationsAtPoint withHieDb getHieFile ideOpts imports pos srcSpans
231
+
217
232
-- | Synopsis for the name at a given position.
218
233
atPoint
219
234
:: IdeOptions
@@ -228,7 +243,7 @@ atPoint IdeOptions{} (HAR _ (hf :: HieASTs a) rf _ (kind :: HieKind hietype)) (D
228
243
-- Hover info for values/data
229
244
hoverInfo :: HieAST hietype -> IO (Maybe Range , [T. Text ])
230
245
hoverInfo ast = do
231
- prettyNames <- mapM prettyName filteredNames
246
+ prettyNames <- mapM prettyName names
232
247
pure (Just range, prettyNames ++ pTypes)
233
248
where
234
249
pTypes :: [T. Text ]
@@ -245,27 +260,20 @@ atPoint IdeOptions{} (HAR _ (hf :: HieASTs a) rf _ (kind :: HieKind hietype)) (D
245
260
info :: NodeInfo hietype
246
261
info = nodeInfoH kind ast
247
262
263
+ -- We want evidence variables to be displayed last.
264
+ -- Evidence trees contain information of secondary relevance.
248
265
names :: [(Identifier , IdentifierDetails hietype )]
249
266
names = sortOn (any isEvidenceUse . identInfo . snd ) $ M. assocs $ nodeIdentifiers info
250
267
251
- -- Check for evidence bindings
252
- isInternal :: (Identifier , IdentifierDetails a ) -> Bool
253
- isInternal (Right _, dets) =
254
- any isEvidenceContext $ identInfo dets
255
- isInternal (Left _, _) = False
256
-
257
- filteredNames :: [(Identifier , IdentifierDetails hietype )]
258
- filteredNames = filter (not . isInternal) names
259
-
260
268
prettyName :: (Either ModuleName Name , IdentifierDetails hietype ) -> IO T. Text
261
269
prettyName (Right n, dets)
262
- | any isEvidenceUse (identInfo dets) =
263
- pure $ maybe " " (printOutputable . renderEvidenceTree) (getEvidenceTree rf n) <> " \n "
270
+ -- We don't want to print evidence variables as they are generated.
271
+ | any isEvidenceUse (identInfo dets) = pure $ maybe " " (printOutputable . renderEvidenceTree) (getEvidenceTree rf n) <> " \n "
264
272
| otherwise = pure $ T. unlines $
265
273
wrapHaskell (printOutputable n <> maybe " " (" :: " <> ) ((prettyType <$> identType dets) <|> maybeKind))
266
274
: maybeToList (pretty (definedAt n) (prettyPackageName n))
267
275
++ catMaybes [ T. unlines . spanDocToMarkdown <$> lookupNameEnv dm n
268
- ]
276
+ ]
269
277
where maybeKind = fmap printOutputable $ safeTyThingType =<< lookupNameEnv km n
270
278
pretty Nothing Nothing = Nothing
271
279
pretty (Just define) Nothing = Just $ define <> " \n "
@@ -299,7 +307,7 @@ atPoint IdeOptions{} (HAR _ (hf :: HieASTs a) rf _ (kind :: HieKind hietype)) (D
299
307
version = T. pack $ showVersion (unitPackageVersion conf)
300
308
pure $ pkgName <> " -" <> version
301
309
302
- -- Type info for the current node, it may contains several symbols
310
+ -- Type info for the current node, it may contain several symbols
303
311
-- for one range, like wildcard
304
312
types :: [hietype ]
305
313
types = nodeType info
@@ -308,10 +316,7 @@ atPoint IdeOptions{} (HAR _ (hf :: HieASTs a) rf _ (kind :: HieKind hietype)) (D
308
316
prettyTypes = map ((" _ :: " <> ) . prettyType) types
309
317
310
318
prettyType :: hietype -> T. Text
311
- prettyType t = case kind of
312
- HieFresh -> printOutputable t
313
- HieFromDisk full_file -> printOutputable $ hieTypeToIface $ recoverFullType t (hie_types full_file)
314
- -- prettyType = printOutputable . expandType
319
+ prettyType = printOutputable . expandType
315
320
316
321
expandType :: a -> SDoc
317
322
expandType t = case kind of
@@ -418,16 +423,37 @@ locationsAtPoint
418
423
-> HieAstResult
419
424
-> m [(Location , Identifier )]
420
425
locationsAtPoint withHieDb lookupModule _ideOptions imports pos (HAR _ ast _rm _ _) =
426
+ let ns = concat $ pointCommand ast pos (M. keys . getNodeIds)
427
+ zeroPos = Position 0 0
428
+ zeroRange = Range zeroPos zeroPos
429
+ modToLocation m = fmap (\ fs -> pure (Location (fromNormalizedUri $ filePathToUri' fs) zeroRange)) $ M. lookup m imports
430
+ in fmap (nubOrd . concat ) $ mapMaybeM
431
+ (either (\ m -> pure ((fmap $ fmap (,Left m)) (modToLocation m)))
432
+ (\ n -> fmap (fmap $ fmap (,Right n)) (nameToLocation withHieDb lookupModule n)))
433
+ ns
434
+
435
+ -- | Find 'Location's of definition at a specific point and return them along with their 'Identifier's.
436
+ instanceLocationsAtPoint
437
+ :: forall m
438
+ . MonadIO m
439
+ => WithHieDb
440
+ -> LookupModule m
441
+ -> IdeOptions
442
+ -> M. Map ModuleName NormalizedFilePath
443
+ -> Position
444
+ -> HieAstResult
445
+ -> m [Location ]
446
+ instanceLocationsAtPoint withHieDb lookupModule _ideOptions imports pos (HAR _ ast _rm _ _) =
421
447
let ns = concat $ pointCommand ast pos (M. keys . getNodeIds)
422
448
evTrees = mapMaybe (either (const Nothing ) $ getEvidenceTree _rm) ns
423
449
evNs = concatMap (map (Right . evidenceVar) . T. flatten) evTrees
424
450
zeroPos = Position 0 0
425
451
zeroRange = Range zeroPos zeroPos
426
452
modToLocation m = fmap (\ fs -> pure (Location (fromNormalizedUri $ filePathToUri' fs) zeroRange)) $ M. lookup m imports
427
- in fmap (nubOrd . concat ) $ mapMaybeM
453
+ in fmap (fmap fst . nubOrd . concat ) $ mapMaybeM
428
454
(either (\ m -> pure ((fmap $ fmap (,Left m)) (modToLocation m)))
429
455
(\ n -> fmap (fmap $ fmap (,Right n)) (nameToLocation withHieDb lookupModule n)))
430
- (ns ++ evNs)
456
+ evNs
431
457
432
458
-- | Given a 'Name' attempt to find the location where it is defined.
433
459
nameToLocation :: MonadIO m => WithHieDb -> LookupModule m -> Name -> m (Maybe [Location ])
0 commit comments