Skip to content

Commit 763d70d

Browse files
wz1000fendor
authored andcommitted
improve hover rendering
1 parent 720923d commit 763d70d

File tree

1 file changed

+25
-37
lines changed

1 file changed

+25
-37
lines changed

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

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ module Development.IDE.Spans.AtPoint (
2323
, LookupModule
2424
) where
2525

26+
27+
import GHC.Data.FastString (lengthFS)
28+
import qualified GHC.Utils.Outputable as O
29+
2630
import Development.IDE.GHC.Error
2731
import Development.IDE.GHC.Orphans ()
2832
import Development.IDE.Types.Location
@@ -59,7 +63,6 @@ import Data.Tree
5963
import qualified Data.Tree as T
6064
import Data.Version (showVersion)
6165
import Development.IDE.Types.Shake (WithHieDb)
62-
import qualified GHC.Utils.Outputable as O
6366
import HieDb hiding (pointCommand,
6467
withHieDb)
6568
import System.Directory (doesFileExist)
@@ -174,14 +177,18 @@ documentHighlight hf rf pos = pure highlights
174177
highlights = do
175178
n <- ns
176179
ref <- fromMaybe [] (M.lookup (Right n) rf)
177-
pure $ makeHighlight ref
178-
makeHighlight (sp,dets) =
179-
DocumentHighlight (realSrcSpanToRange sp) (Just $ highlightType $ identInfo dets)
180+
maybeToList (makeHighlight n ref)
181+
makeHighlight n (sp,dets)
182+
| isTvNameSpace (nameNameSpace n) && isBadSpan n sp = Nothing
183+
| otherwise = Just $ DocumentHighlight (realSrcSpanToRange sp) (Just $ highlightType $ identInfo dets)
180184
highlightType s =
181185
if any (isJust . getScopeFromContext) s
182186
then DocumentHighlightKind_Write
183187
else DocumentHighlightKind_Read
184188

189+
isBadSpan :: Name -> RealSrcSpan -> Bool
190+
isBadSpan n sp = srcSpanStartLine sp /= srcSpanEndLine sp || (srcSpanEndCol sp - srcSpanStartCol sp > lengthFS (occNameFS $ nameOccName n))
191+
185192
-- | Locate the type definition of the name at a given position.
186193
gotoTypeDefinition
187194
:: MonadIO m
@@ -327,23 +334,22 @@ atPoint IdeOptions{} (HAR _ (hf :: HieASTs a) rf _ (kind :: HieKind hietype)) (D
327334
renderEvidenceTree (T.Node (EvidenceInfo{evidenceDetails=Just (EvLetBind _,_,_)}) [x])
328335
= renderEvidenceTree x
329336
renderEvidenceTree (T.Node (EvidenceInfo{evidenceDetails=Just (EvLetBind _,_,_), ..}) xs)
330-
= hang (text "- Evidence of constraint `" O.<> expandType evidenceType O.<> "`") 2 $
331-
vcat $ text "depending on:" : map renderEvidenceTree' xs
332-
renderEvidenceTree x = renderEvidenceTree' x
337+
= hang (text "Evidence of constraint `" O.<> expandType evidenceType O.<> "`") 2 $
338+
vcat $ text "constructed using:" : map renderEvidenceTree' xs
339+
renderEvidenceTree (T.Node (EvidenceInfo{..}) _)
340+
= hang (text "Evidence of constraint `" O.<> expandType evidenceType O.<> "`") 2 $
341+
vcat $ printDets evidenceSpan evidenceDetails : map (text . T.unpack) (maybeToList $ definedAt evidenceVar)
333342

334343
-- renderEvidenceTree' skips let bound evidence variables and prints the children directly
335344
renderEvidenceTree' (T.Node (EvidenceInfo{evidenceDetails=Just (EvLetBind _,_,_)}) xs)
336345
= vcat (map renderEvidenceTree' xs)
337-
renderEvidenceTree' (T.Node (EvidenceInfo{..}) xs)
338-
= hang (text "- Evidence of constraint `" O.<> expandType evidenceType O.<> "`") 2 $
339-
vcat $ map (text . T.unpack) (maybeToList $ definedAt evidenceVar)
340-
++ [printDets evidenceSpan evidenceDetails (null xs)]
341-
++ map renderEvidenceTree' xs
342-
343-
printDets :: RealSrcSpan -> Maybe (EvVarSource, Scope, Maybe Span) -> Bool -> SDoc
344-
printDets _ Nothing True = text ""
345-
printDets _ Nothing False = text "constructed using:"
346-
printDets ospn (Just (src,_,mspn)) _ = pprSrc
346+
renderEvidenceTree' (T.Node (EvidenceInfo{..}) _)
347+
= hang (text "- `" O.<> expandType evidenceType O.<> "`") 2 $
348+
vcat $ printDets evidenceSpan evidenceDetails : map (text . T.unpack) (maybeToList $ definedAt evidenceVar)
349+
350+
printDets :: RealSrcSpan -> Maybe (EvVarSource, Scope, Maybe Span) -> SDoc
351+
printDets _ Nothing = text "using an external instance"
352+
printDets ospn (Just (src,_,mspn)) = pprSrc
347353
$$ text "at" <+> ppr spn
348354
where
349355
-- Use the bind span if we have one, else use the occurence span
@@ -409,37 +415,19 @@ locationsAtPoint
409415
-> IdeOptions
410416
-> M.Map ModuleName NormalizedFilePath
411417
-> Position
412-
<<<<<<< HEAD
413-
-> HieASTs a
414-
-> m [(Location, Identifier)]
415-
locationsAtPoint withHieDb lookupModule _ideOptions imports pos ast =
416-
||||||| parent of 86ebcf859 (Jump to instance definition and explain typeclass evidence)
417-
-> HieASTs a
418-
-> m [Location]
419-
locationsAtPoint withHieDb lookupModule _ideOptions imports pos ast =
420-
=======
421418
-> HieAstResult
422-
-> m [Location]
419+
-> m [(Location, Identifier)]
423420
locationsAtPoint withHieDb lookupModule _ideOptions imports pos (HAR _ ast _rm _ _) =
424-
>>>>>>> 86ebcf859 (Jump to instance definition and explain typeclass evidence)
425421
let ns = concat $ pointCommand ast pos (M.keys . getNodeIds)
426422
evTrees = mapMaybe (either (const Nothing) $ getEvidenceTree _rm) ns
427423
evNs = concatMap (map (Right . evidenceVar) . T.flatten) evTrees
428424
zeroPos = Position 0 0
429425
zeroRange = Range zeroPos zeroPos
430-
<<<<<<< HEAD
431426
modToLocation m = fmap (\fs -> pure (Location (fromNormalizedUri $ filePathToUri' fs) zeroRange)) $ M.lookup m imports
432427
in fmap (nubOrd . concat) $ mapMaybeM
433428
(either (\m -> pure ((fmap $ fmap (,Left m)) (modToLocation m)))
434429
(\n -> fmap (fmap $ fmap (,Right n)) (nameToLocation withHieDb lookupModule n)))
435-
ns
436-
||||||| parent of 86ebcf859 (Jump to instance definition and explain typeclass evidence)
437-
modToLocation m = fmap (\fs -> pure $ Location (fromNormalizedUri $ filePathToUri' fs) zeroRange) $ M.lookup m imports
438-
in fmap (nubOrd . concat) $ mapMaybeM (either (pure . modToLocation) $ nameToLocation withHieDb lookupModule) ns
439-
=======
440-
modToLocation m = (\fs -> pure $ Location (fromNormalizedUri $ filePathToUri' fs) zeroRange) <$> M.lookup m imports
441-
in nubOrd . concat <$> mapMaybeM (either (pure . modToLocation) $ nameToLocation withHieDb lookupModule) (ns ++ evNs)
442-
>>>>>>> 86ebcf859 (Jump to instance definition and explain typeclass evidence)
430+
(ns ++ evNs)
443431

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

0 commit comments

Comments
 (0)