Skip to content

Commit c14ceb3

Browse files
committed
improve hover rendering
1 parent 230a9c9 commit c14ceb3

File tree

1 file changed

+24
-47
lines changed

1 file changed

+24
-47
lines changed

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

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,18 @@ documentHighlight hf rf pos = pure highlights
179179
highlights = do
180180
n <- ns
181181
ref <- fromMaybe [] (M.lookup (Right n) rf)
182-
pure $ makeHighlight ref
183-
makeHighlight (sp,dets) =
184-
DocumentHighlight (realSrcSpanToRange sp) (Just $ highlightType $ identInfo dets)
182+
maybeToList (makeHighlight n ref)
183+
makeHighlight n (sp,dets)
184+
| isTvNameSpace (nameNameSpace n) && isBadSpan n sp = Nothing
185+
| otherwise = Just $ DocumentHighlight (realSrcSpanToRange sp) (Just $ highlightType $ identInfo dets)
185186
highlightType s =
186187
if any (isJust . getScopeFromContext) s
187188
then HkWrite
188189
else HkRead
189190

191+
isBadSpan :: Name -> RealSrcSpan -> Bool
192+
isBadSpan n sp = srcSpanStartLine sp /= srcSpanEndLine sp || (srcSpanEndCol sp - srcSpanStartCol sp > lengthFS (occNameFS $ nameOccName n))
193+
190194
gotoTypeDefinition
191195
:: MonadIO m
192196
=> WithHieDb
@@ -290,30 +294,29 @@ atPoint IdeOptions{} (HAR _ (hf :: HieASTs a) _rf _ kind) (DKMap dm km) env pos
290294
renderEvidenceTree (T.Node (EvidenceInfo{evidenceDetails=Just (EvLetBind _,_,_)}) [x])
291295
= renderEvidenceTree x
292296
renderEvidenceTree (T.Node (EvidenceInfo{evidenceDetails=Just (EvLetBind _,_,_), ..}) xs)
293-
= hang (text "- Evidence of constraint `" O.<> expandType evidenceType O.<> "`") 2 $
294-
vcat $ text "depending on:" : map renderEvidenceTree' xs
295-
renderEvidenceTree x = renderEvidenceTree' x
297+
= hang (text "Evidence of constraint `" O.<> expandType evidenceType O.<> "`") 2 $
298+
vcat $ text "constructed using:" : map renderEvidenceTree' xs
299+
renderEvidenceTree (T.Node (EvidenceInfo{..}) _)
300+
= hang (text "Evidence of constraint `" O.<> expandType evidenceType O.<> "`") 2 $
301+
vcat $ printDets evidenceSpan evidenceDetails : map (text . T.unpack) (definedAt evidenceVar)
296302

297303
-- renderEvidenceTree' skips let bound evidence variables and prints the children directly
298304
renderEvidenceTree' (T.Node (EvidenceInfo{evidenceDetails=Just (EvLetBind _,_,_)}) xs)
299305
= vcat (map renderEvidenceTree' xs)
300-
renderEvidenceTree' (T.Node (EvidenceInfo{..}) xs)
301-
= hang (text "- Evidence of constraint `" O.<> expandType evidenceType O.<> "`") 2 $
302-
vcat $ map (text . T.unpack) (definedAt evidenceVar)
303-
++ [printDets evidenceSpan evidenceDetails (null xs)]
304-
++ map renderEvidenceTree' xs
305-
306-
printDets :: RealSrcSpan -> Maybe (EvVarSource, Scope, Maybe Span) -> Bool -> SDoc
307-
printDets _ Nothing True = text ""
308-
printDets _ Nothing False = text "constructed using:"
309-
printDets ospn (Just (src,_,mspn)) _ = pprSrc
306+
renderEvidenceTree' (T.Node (EvidenceInfo{..}) _)
307+
= hang (text "- `" O.<> expandType evidenceType O.<> "`") 2 $
308+
vcat $ printDets evidenceSpan evidenceDetails : map (text . T.unpack) (definedAt evidenceVar)
309+
310+
printDets :: RealSrcSpan -> Maybe (EvVarSource, Scope, Maybe Span) -> SDoc
311+
printDets _ Nothing = text "using an external instance"
312+
printDets ospn (Just (src,_,mspn)) = pprSrc
310313
$$ text "at" <+> ppr spn
311314
where
312315
-- Use the bind span if we have one, else use the occurence span
313316
spn = fromMaybe ospn mspn
314317
pprSrc = case src of
315318
-- Users don't know what HsWrappers are
316-
EvWrapperBind -> "bound by type signature or pattern"
319+
EvWrapperBind -> "bound by a context"
317320
_ -> ppr src
318321
#endif
319322

@@ -352,24 +355,12 @@ typeLocationsAtPoint withHieDb lookupModule _ideOptions pos (HAR _ ast _ _ hieKi
352355
HQualTy a b -> getTypes [a,b]
353356
HCastTy a -> getTypes [a]
354357
_ -> []
355-
<<<<<<< HEAD
356-
in fmap nubOrd $ concatMapM (fmap (fromMaybe []) . nameToLocation withHieDb lookupModule) (getTypes ts)
357-
||||||| parent of dc807c4a (Jump to instance definition and explain typeclass evidence)
358-
in fmap nubOrd $ concatMapM (fmap (fromMaybe []) . nameToLocation hiedb lookupModule) (getTypes ts)
359-
=======
360-
in nubOrd <$> concatMapM (fmap (fromMaybe []) . nameToLocation hiedb lookupModule) (getTypes ts)
361-
>>>>>>> dc807c4a (Jump to instance definition and explain typeclass evidence)
358+
in nubOrd <$> concatMapM (fmap (fromMaybe []) . nameToLocation withHieDb lookupModule) (getTypes ts)
362359
HieFresh ->
363360
let ts = concat $ pointCommand ast pos getts
364361
getts x = nodeType ni ++ mapMaybe identType (M.elems $ nodeIdentifiers ni)
365362
where ni = nodeInfo x
366-
<<<<<<< HEAD
367-
in fmap nubOrd $ concatMapM (fmap (fromMaybe []) . nameToLocation withHieDb lookupModule) (getTypes ts)
368-
||||||| parent of dc807c4a (Jump to instance definition and explain typeclass evidence)
369-
in fmap nubOrd $ concatMapM (fmap (fromMaybe []) . nameToLocation hiedb lookupModule) (getTypes ts)
370-
=======
371-
in nubOrd <$> concatMapM (fmap (fromMaybe []) . nameToLocation hiedb lookupModule) (getTypes ts)
372-
>>>>>>> dc807c4a (Jump to instance definition and explain typeclass evidence)
363+
in nubOrd <$> concatMapM (fmap (fromMaybe []) . nameToLocation withHieDb lookupModule) (getTypes ts)
373364

374365
namesInType :: Type -> [Name]
375366
namesInType (TyVarTy n) = [varName n]
@@ -394,13 +385,7 @@ locationsAtPoint
394385
-> Position
395386
-> HieAstResult
396387
-> m [Location]
397-
<<<<<<< HEAD
398-
locationsAtPoint withHieDb lookupModule _ideOptions imports pos ast =
399-
||||||| parent of dc807c4a (Jump to instance definition and explain typeclass evidence)
400-
locationsAtPoint hiedb lookupModule _ideOptions imports pos ast =
401-
=======
402-
locationsAtPoint hiedb lookupModule _ideOptions imports pos (HAR _ ast _rm _ _) =
403-
>>>>>>> dc807c4a (Jump to instance definition and explain typeclass evidence)
388+
locationsAtPoint withHieDb lookupModule _ideOptions imports pos (HAR _ ast _rm _ _) =
404389
let ns = concat $ pointCommand ast pos (M.keys . getNodeIds)
405390
#if MIN_VERSION_ghc(9,0,1)
406391
evTrees = mapMaybe (either (const Nothing) $ getEvidenceTree _rm) ns
@@ -410,16 +395,8 @@ locationsAtPoint hiedb lookupModule _ideOptions imports pos (HAR _ ast _rm _ _)
410395
#endif
411396
zeroPos = Position 0 0
412397
zeroRange = Range zeroPos zeroPos
413-
<<<<<<< HEAD
414-
modToLocation m = fmap (\fs -> pure $ Location (fromNormalizedUri $ filePathToUri' fs) zeroRange) $ M.lookup m imports
415-
in fmap (nubOrd . concat) $ mapMaybeM (either (pure . modToLocation) $ nameToLocation withHieDb lookupModule) ns
416-
||||||| parent of dc807c4a (Jump to instance definition and explain typeclass evidence)
417-
modToLocation m = fmap (\fs -> pure $ Location (fromNormalizedUri $ filePathToUri' fs) zeroRange) $ M.lookup m imports
418-
in fmap (nubOrd . concat) $ mapMaybeM (either (pure . modToLocation) $ nameToLocation hiedb lookupModule) ns
419-
=======
420398
modToLocation m = (\fs -> pure $ Location (fromNormalizedUri $ filePathToUri' fs) zeroRange) <$> M.lookup m imports
421-
in nubOrd . concat <$> mapMaybeM (either (pure . modToLocation) $ nameToLocation hiedb lookupModule) (ns ++ evNs)
422-
>>>>>>> dc807c4a (Jump to instance definition and explain typeclass evidence)
399+
in nubOrd . concat <$> mapMaybeM (either (pure . modToLocation) $ nameToLocation withHieDb lookupModule) (ns ++ evNs)
423400

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

0 commit comments

Comments
 (0)