@@ -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
@@ -58,6 +59,8 @@ import qualified Data.Array as A
58
59
import Data.Either
59
60
import Data.List.Extra (dropEnd1 , nubOrd )
60
61
62
+
63
+ import Data.Either.Extra (eitherToMaybe )
61
64
import Data.List (isSuffixOf , sortOn )
62
65
import Data.Tree
63
66
import qualified Data.Tree as T
@@ -214,6 +217,19 @@ gotoDefinition
214
217
gotoDefinition withHieDb getHieFile ideOpts imports srcSpans pos
215
218
= lift $ locationsAtPoint withHieDb getHieFile ideOpts imports pos srcSpans
216
219
220
+ -- | Locate the implementation definition of the name at a given position.
221
+ -- Goto Implementation for an overloaded function.
222
+ gotoImplementation
223
+ :: MonadIO m
224
+ => WithHieDb
225
+ -> LookupModule m
226
+ -> IdeOptions
227
+ -> HieAstResult
228
+ -> Position
229
+ -> MaybeT m [Location ]
230
+ gotoImplementation withHieDb getHieFile ideOpts srcSpans pos
231
+ = lift $ instanceLocationsAtPoint withHieDb getHieFile ideOpts pos srcSpans
232
+
217
233
-- | Synopsis for the name at a given position.
218
234
atPoint
219
235
:: IdeOptions
@@ -228,7 +244,7 @@ atPoint IdeOptions{} (HAR _ (hf :: HieASTs a) rf _ (kind :: HieKind hietype)) (D
228
244
-- Hover info for values/data
229
245
hoverInfo :: HieAST hietype -> IO (Maybe Range , [T. Text ])
230
246
hoverInfo ast = do
231
- prettyNames <- mapM prettyName filteredNames
247
+ prettyNames <- mapM prettyName names
232
248
pure (Just range, prettyNames ++ pTypes)
233
249
where
234
250
pTypes :: [T. Text ]
@@ -245,27 +261,20 @@ atPoint IdeOptions{} (HAR _ (hf :: HieASTs a) rf _ (kind :: HieKind hietype)) (D
245
261
info :: NodeInfo hietype
246
262
info = nodeInfoH kind ast
247
263
264
+ -- We want evidence variables to be displayed last.
265
+ -- Evidence trees contain information of secondary relevance.
248
266
names :: [(Identifier , IdentifierDetails hietype )]
249
267
names = sortOn (any isEvidenceUse . identInfo . snd ) $ M. assocs $ nodeIdentifiers info
250
268
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
269
prettyName :: (Either ModuleName Name , IdentifierDetails hietype ) -> IO T. Text
261
270
prettyName (Right n, dets)
262
- | any isEvidenceUse (identInfo dets) =
263
- pure $ maybe " " (printOutputable . renderEvidenceTree) (getEvidenceTree rf n) <> " \n "
271
+ -- We want to print evidence variable using a readable tree structure.
272
+ | any isEvidenceUse (identInfo dets) = pure $ maybe " " (printOutputable . renderEvidenceTree) (getEvidenceTree rf n) <> " \n "
264
273
| otherwise = pure $ T. unlines $
265
274
wrapHaskell (printOutputable n <> maybe " " (" :: " <> ) ((prettyType <$> identType dets) <|> maybeKind))
266
275
: maybeToList (pretty (definedAt n) (prettyPackageName n))
267
276
++ catMaybes [ T. unlines . spanDocToMarkdown <$> lookupNameEnv dm n
268
- ]
277
+ ]
269
278
where maybeKind = fmap printOutputable $ safeTyThingType =<< lookupNameEnv km n
270
279
pretty Nothing Nothing = Nothing
271
280
pretty (Just define) Nothing = Just $ define <> " \n "
@@ -299,7 +308,7 @@ atPoint IdeOptions{} (HAR _ (hf :: HieASTs a) rf _ (kind :: HieKind hietype)) (D
299
308
version = T. pack $ showVersion (unitPackageVersion conf)
300
309
pure $ pkgName <> " -" <> version
301
310
302
- -- Type info for the current node, it may contains several symbols
311
+ -- Type info for the current node, it may contain several symbols
303
312
-- for one range, like wildcard
304
313
types :: [hietype ]
305
314
types = nodeType info
@@ -308,10 +317,7 @@ atPoint IdeOptions{} (HAR _ (hf :: HieASTs a) rf _ (kind :: HieKind hietype)) (D
308
317
prettyTypes = map ((" _ :: " <> ) . prettyType) types
309
318
310
319
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
320
+ prettyType = printOutputable . expandType
315
321
316
322
expandType :: a -> SDoc
317
323
expandType t = case kind of
@@ -352,7 +358,7 @@ atPoint IdeOptions{} (HAR _ (hf :: HieASTs a) rf _ (kind :: HieKind hietype)) (D
352
358
printDets ospn (Just (src,_,mspn)) = pprSrc
353
359
$$ text " at" <+> ppr spn
354
360
where
355
- -- Use the bind span if we have one, else use the occurence span
361
+ -- Use the bind span if we have one, else use the occurrence span
356
362
spn = fromMaybe ospn mspn
357
363
pprSrc = case src of
358
364
-- Users don't know what HsWrappers are
@@ -419,15 +425,31 @@ locationsAtPoint
419
425
-> m [(Location , Identifier )]
420
426
locationsAtPoint withHieDb lookupModule _ideOptions imports pos (HAR _ ast _rm _ _) =
421
427
let ns = concat $ pointCommand ast pos (M. keys . getNodeIds)
422
- evTrees = mapMaybe (either (const Nothing ) $ getEvidenceTree _rm) ns
423
- evNs = concatMap (map (Right . evidenceVar) . T. flatten) evTrees
424
428
zeroPos = Position 0 0
425
429
zeroRange = Range zeroPos zeroPos
426
430
modToLocation m = fmap (\ fs -> pure (Location (fromNormalizedUri $ filePathToUri' fs) zeroRange)) $ M. lookup m imports
427
431
in fmap (nubOrd . concat ) $ mapMaybeM
428
432
(either (\ m -> pure ((fmap $ fmap (,Left m)) (modToLocation m)))
429
433
(\ n -> fmap (fmap $ fmap (,Right n)) (nameToLocation withHieDb lookupModule n)))
430
- (ns ++ evNs)
434
+ ns
435
+
436
+ -- | Find 'Location's of a implementation definition at a specific point.
437
+ instanceLocationsAtPoint
438
+ :: forall m
439
+ . MonadIO m
440
+ => WithHieDb
441
+ -> LookupModule m
442
+ -> IdeOptions
443
+ -> Position
444
+ -> HieAstResult
445
+ -> m [Location ]
446
+ instanceLocationsAtPoint withHieDb lookupModule _ideOptions pos (HAR _ ast _rm _ _) =
447
+ let ns = concat $ pointCommand ast pos (M. keys . getNodeIds)
448
+ evTrees = mapMaybe (eitherToMaybe >=> getEvidenceTree _rm) ns
449
+ evNs = concatMap (map (evidenceVar) . T. flatten) evTrees
450
+ in fmap (nubOrd . concat ) $ mapMaybeM
451
+ (nameToLocation withHieDb lookupModule)
452
+ evNs
431
453
432
454
-- | Given a 'Name' attempt to find the location where it is defined.
433
455
nameToLocation :: MonadIO m => WithHieDb -> LookupModule m -> Name -> m (Maybe [Location ])
0 commit comments