Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display instances on hover for type and data constructors #3963

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions ghcide/src/Development/IDE/Spans/AtPoint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import Development.IDE.Core.PositionMapping
import Development.IDE.Core.RuleTypes
import Development.IDE.GHC.Compat
import qualified Development.IDE.GHC.Compat.Util as Util
import Development.IDE.GHC.Util (printOutputable)
import Development.IDE.GHC.Util (evalGhcEnv,
printOutputable)
import Development.IDE.Spans.Common
import Development.IDE.Types.Options

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

import Data.Version (showVersion)
import Development.IDE.Types.Shake (WithHieDb)
import GHC (getInstancesForType)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get it from Compat?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since in general we import ghc stuff from Development.IDE.GHC.Compat

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I should just export it in Development.IDE.GHC.Compat? Why is this needed? It seems to be available in this module in all the ghc versions supported by hls

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure of our policy here, wdyt @wz1000 ?

import HieDb hiding (pointCommand,
withHieDb)
import System.Directory (doesFileExist)
Expand Down Expand Up @@ -218,7 +220,8 @@ atPoint IdeOptions{} (HAR _ hf _ _ (kind :: HieKind hietype)) (DKMap dm km) env
hoverInfo :: HieAST hietype -> IO (Maybe Range, [T.Text])
hoverInfo ast = do
prettyNames <- mapM prettyName filteredNames
pure (Just range, prettyNames ++ pTypes)
instances <- catMaybes <$> mapM (either (const $ pure Nothing) prettyInstances . fst) filteredNames
pure (Just range, prettyNames ++ pTypes ++ instances)
where
pTypes :: [T.Text]
pTypes
Expand Down Expand Up @@ -306,6 +309,28 @@ atPoint IdeOptions{} (HAR _ hf _ _ (kind :: HieKind hietype)) (DKMap dm km) env
UnhelpfulLoc {} | isInternalName name || isSystemName name -> Nothing
_ -> Just $ "*Defined " <> printOutputable (pprNameDefnLoc name) <> "*"

prettyInstances :: Name -> IO (Maybe T.Text)
prettyInstances n =
fmap (wrapHaskell . T.unlines . fmap printOutputable) <$> instancesForName
where
instancesForName :: IO (Maybe [ClsInst])
instancesForName = runMaybeT $ do
typ <- MaybeT . pure $ lookupNameEnv km n >>= tyThingAsDataType
clsInst <- liftIO $ evalGhcEnv env $ getInstancesForType typ
-- Avoid creating an empty wrapped section if no instances are found
guard $ not $ null clsInst
return clsInst

-- | Gets the datatype `Type` corresponding to a TyThing, if it repressents a datatype or
-- a data constructor.
tyThingAsDataType :: TyThing -> Maybe Type
tyThingAsDataType (AnId _) = Nothing
tyThingAsDataType (ACoAxiom _) = Nothing
tyThingAsDataType (AConLike cl) = case cl of
PatSynCon _ -> Nothing
RealDataCon dc -> Just $ mkTyConTy $ dataConTyCon dc
tyThingAsDataType (ATyCon tc) = Just $ mkTyConTy tc

typeLocationsAtPoint
:: forall m
. MonadIO m
Expand Down
Loading