-pprTyThingInContext pefas (AnId id) = pprIdInContext pefas id
-pprTyThingInContext pefas (ADataCon dataCon) = pprDataCon pefas dataCon
-pprTyThingInContext pefas (ATyCon tyCon) = pprTyCon pefas tyCon
-pprTyThingInContext pefas (AClass cls) = pprClass pefas cls
+pprTyThingInContext pefas thing
+ | Just parent <- pprTyThingParent_maybe thing
+ = ppr_ty_thing pefas (== GHC.getName thing) parent
+ | otherwise
+ = pprTyThing pefas thing
+
+-- | Like 'pprTyThingInContext', but adds the defining location.
+pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
+pprTyThingInContextLoc pefas tyThing
+ = showWithLoc (pprNameLoc (GHC.getName tyThing))
+ (pprTyThingInContext pefas tyThing)
+
+pprTyThingParent_maybe :: TyThing -> Maybe TyThing
+-- (pprTyThingParent_maybe x) returns (Just p)
+-- when pprTyThingInContext sould print a declaration for p
+-- (albeit with some "..." in it) when asked to show x
+pprTyThingParent_maybe (ADataCon dc) = Just (ATyCon (dataConTyCon dc))
+pprTyThingParent_maybe (AnId id) = case idDetails id of
+ RecSelId { sel_tycon = tc } -> Just (ATyCon tc)
+ ClassOpId cls -> Just (AClass cls)
+ _other -> Nothing
+pprTyThingParent_maybe _other = Nothing