Skip to content

Commit f4393e6

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

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
@@ -35,7 +35,8 @@ import Development.IDE.Core.PositionMapping
3535
import Development.IDE.Core.RuleTypes
3636
import Development.IDE.GHC.Compat
3737
import qualified Development.IDE.GHC.Compat.Util as Util
38-
import Development.IDE.GHC.Util (printOutputable)
38+
import Development.IDE.GHC.Util (evalGhcEnv,
39+
printOutputable)
3940
import Development.IDE.Spans.Common
4041
import Development.IDE.Types.Options
4142

@@ -57,6 +58,7 @@ import Data.List.Extra (dropEnd1, nubOrd)
5758

5859
import Data.Version (showVersion)
5960
import Development.IDE.Types.Shake (WithHieDb)
61+
import GHC (getInstancesForType)
6062
import HieDb hiding (pointCommand,
6163
withHieDb)
6264
import System.Directory (doesFileExist)
@@ -218,7 +220,8 @@ atPoint IdeOptions{} (HAR _ hf _ _ (kind :: HieKind hietype)) (DKMap dm km) env
218220
hoverInfo :: HieAST hietype -> IO (Maybe Range, [T.Text])
219221
hoverInfo ast = do
220222
prettyNames <- mapM prettyName filteredNames
221-
pure (Just range, prettyNames ++ pTypes)
223+
instances <- catMaybes <$> mapM prettyInstances filteredNames
224+
pure (Just range, prettyNames ++ pTypes ++ instances)
222225
where
223226
pTypes :: [T.Text]
224227
pTypes
@@ -306,6 +309,24 @@ atPoint IdeOptions{} (HAR _ hf _ _ (kind :: HieKind hietype)) (DKMap dm km) env
306309
UnhelpfulLoc {} | isInternalName name || isSystemName name -> Nothing
307310
_ -> Just $ "*Defined " <> printOutputable (pprNameDefnLoc name) <> "*"
308311

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

0 commit comments

Comments
 (0)