Skip to content

Commit eb7c90b

Browse files
author
DeviousStoat
committed
Display instances on hover for type and data constructors
1 parent db8efbe commit eb7c90b

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ import Development.IDE.Core.PositionMapping
3737
import Development.IDE.Core.RuleTypes
3838
import Development.IDE.GHC.Compat
3939
import qualified Development.IDE.GHC.Compat.Util as Util
40-
import Development.IDE.GHC.Util (printOutputable)
40+
import Development.IDE.GHC.Util (evalGhcEnv,
41+
printOutputable)
4142
import Development.IDE.Spans.Common
4243
import Development.IDE.Types.Options
4344

@@ -59,6 +60,7 @@ import Data.List.Extra (dropEnd1, nubOrd)
5960

6061
import Data.Version (showVersion)
6162
import Development.IDE.Types.Shake (WithHieDb)
63+
import GHC (getInstancesForType)
6264
import HieDb hiding (pointCommand,
6365
withHieDb)
6466
import System.Directory (doesFileExist)
@@ -220,7 +222,8 @@ atPoint IdeOptions{} (HAR _ hf _ _ (kind :: HieKind hietype)) (DKMap dm km) env
220222
hoverInfo :: HieAST hietype -> IO (Maybe Range, [T.Text])
221223
hoverInfo ast = do
222224
prettyNames <- mapM prettyName filteredNames
223-
pure (Just range, prettyNames ++ pTypes)
225+
instances <- catMaybes <$> mapM prettyInstances filteredNames
226+
pure (Just range, prettyNames ++ pTypes ++ instances)
224227
where
225228
pTypes :: [T.Text]
226229
pTypes
@@ -308,6 +311,24 @@ atPoint IdeOptions{} (HAR _ hf _ _ (kind :: HieKind hietype)) (DKMap dm km) env
308311
UnhelpfulLoc {} | isInternalName name || isSystemName name -> Nothing
309312
_ -> Just $ "*Defined " <> printOutputable (pprNameDefnLoc name) <> "*"
310313

314+
prettyInstances :: (Either ModuleName Name, IdentifierDetails hietype) -> IO (Maybe T.Text)
315+
prettyInstances (Right n, _) =
316+
fmap (wrapHaskell . T.unlines . fmap printOutputable) <$> instancesForName
317+
where
318+
instancesForName :: IO (Maybe [ClsInst])
319+
instancesForName = runMaybeT $ do
320+
typ <- MaybeT . pure $ lookupNameEnv km n >>= tyThingToType
321+
liftIO $ evalGhcEnv env $ getInstancesForType typ
322+
323+
tyThingToType :: TyThing -> Maybe Type
324+
tyThingToType (AnId _) = Nothing
325+
tyThingToType (ACoAxiom _) = Nothing
326+
tyThingToType (AConLike cl) = case cl of
327+
PatSynCon _ -> Nothing
328+
RealDataCon dc -> Just $ mkTyConTy $ dataConTyCon dc
329+
tyThingToType (ATyCon tc) = Just $ mkTyConTy tc
330+
prettyInstances (Left _, _) = pure Nothing
331+
311332
typeLocationsAtPoint
312333
:: forall m
313334
. MonadIO m

0 commit comments

Comments
 (0)