Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit a63cbcc

Browse files
committed
Simplify findTypeDef by utilising typeMap
Undo changes that are not related to this feature
1 parent f75ab8a commit a63cbcc

File tree

2 files changed

+26
-61
lines changed

2 files changed

+26
-61
lines changed

haskell-ide-engine.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ library
8787
, safe
8888
, sorted-list >= 0.2.1.0
8989
, stm
90-
, syb
9190
, tagsoup
9291
, text
9392
, transformers

src/Haskell/Ide/Engine/Plugin/HieExtras.hs

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import Control.Monad.Except
3434
import Data.Aeson
3535
import qualified Data.Aeson.Types as J
3636
import Data.Char
37-
import qualified Data.Generics as SYB
3837
import Data.IORef
3938
import qualified Data.List as List
4039
import qualified Data.Map as Map
@@ -182,9 +181,6 @@ mkPragmaCompl label insertText =
182181
safeTyThingId :: TyThing -> Maybe Id
183182
safeTyThingId (AnId i) = Just i
184183
safeTyThingId (AConLike (RealDataCon dc)) = Just $ dataConWrapId dc
185-
safeTyThingId (ATyCon tyCon) = case GHC.tyConTyVars tyCon of
186-
[] -> Nothing
187-
(a:_) -> Just a
188184
safeTyThingId _ = Nothing
189185

190186
-- Associates a module's qualifier with its members
@@ -547,85 +543,55 @@ getModule df n = do
547543
return (pkg, T.pack $ moduleNameString $ moduleName m)
548544

549545
-- ---------------------------------------------------------------------
550-
-- TODO: there has to be a simpler way, using the appropriate GHC internals
551-
-- |Find the Id for a given Name.
552-
-- Requires an already TypecheckedModule.
553-
-- A TypecheckedModule can be obtained by using the functions
554-
-- @ifCachedModuleAndData@ or @withCachedModuleAndData@.
555-
--
556-
-- Function is copied from @HaRe/src/Language/Haskell/Refact/Utils/TypeUtils.hs:2954@.
557-
findIdForName :: TypecheckedModule -> Name -> IdeM (Maybe Id)
558-
findIdForName tm n = do
559-
let t = GHC.tm_typechecked_source tm
560-
let r = SYB.something (SYB.mkQ Nothing worker) t
561-
worker (i :: GHC.Id) | nameUnique n == varUnique i = Just i
562-
worker _ = Nothing
563-
return r
564-
565-
-- ---------------------------------------------------------------------
566-
567-
-- | Get the type for a name.
568-
-- Requires an already TypecheckedModule.
569-
-- A TypecheckedModule can be obtained by using the functions
570-
-- @ifCachedModuleAndData@ or @withCachedModuleAndData@.
571-
--
572-
-- Returns the type of a variable or a sum type constructor.
573-
--
574-
-- Function is taken from @HaRe/src/Language/Haskell/Refact/Utils/TypeUtils.hs:2966@.
575-
getTypeForName' :: TypecheckedModule -> Name -> IdeM (Maybe Type)
576-
getTypeForName' tm n = do
577-
mId <- findIdForName tm n
578-
case mId of
579-
Nothing -> getTypeForName n
580-
Just i -> return $ Just (varType i)
581546

582547
-- | Return the type definition of the symbol at the given position.
583-
-- Works for Datatypes, Newtypes and Type Definitions.
584-
-- The latter is only possible, if the type that is defined is defined in the project.
548+
-- Works for Datatypes, Newtypes and Type Definitions, as well as paremterized types.
549+
-- Type Definitions can only be looked up, if the corresponding type is defined in the project.
585550
-- Sum Types can also be searched.
586551
findTypeDef :: Uri -> Position -> IdeDeferM (IdeResult [Location])
587552
findTypeDef uri pos = pluginGetFile "findTypeDef: " uri $ \file ->
588-
ifCachedModuleAndData -- Dont wait on this function if the module is not cached.
589-
file
553+
withCachedInfo
554+
file
590555
(IdeResultOk []) -- Default result
591-
(\tm info NMD{} -> do
556+
(\info -> do
592557
let rfm = revMap info
593-
lm = locMap info
594558
mm = moduleMap info
559+
tmap = typeMap info
595560
oldPos = newPosToOld info pos
561+
596562
case (\x -> Just $ getArtifactsAtPos x mm) =<< oldPos of
597563
Just ((_, mn) : _) -> gotoModule rfm mn
598-
_ -> do
564+
_ -> do
599565
let
600566
-- | Get SrcSpan of the name at the given position.
601567
-- If the old position is Nothing, e.g. there is no cached info about it,
602568
-- Nothing is returned.
603569
--
604570
-- Otherwise, searches for the Type of the given position
605571
-- and retrieves its SrcSpan.
606-
getSrcSpanFromPosition :: Maybe Position -> ExceptT () IdeDeferM SrcSpan
607-
getSrcSpanFromPosition maybeOldPosition = do
572+
getTypeSrcSpanFromPosition
573+
:: Maybe Position -> ExceptT () IdeDeferM SrcSpan
574+
getTypeSrcSpanFromPosition maybeOldPosition = do
608575
oldPosition <- liftMaybe maybeOldPosition
609-
(_, n) <- liftMaybe $ symbolFromTypecheckedModule lm oldPosition
610-
t <- liftMaybeM (lift $ getTypeForName' tm n)
611-
tyCon <- liftMaybe $ tyConAppTyCon_maybe t
612-
case nameSrcSpan (getName tyCon) of
613-
UnhelpfulSpan _ -> throwError ()
614-
realSpan -> return realSpan
615-
576+
let tmapRes = getArtifactsAtPos oldPosition tmap
577+
case tmapRes of
578+
[] -> throwError ()
579+
a -> do
580+
-- take last type since this is always the most accurate one
581+
tyCon <- liftMaybe $ tyConAppTyCon_maybe (snd $ last a)
582+
case nameSrcSpan (getName tyCon) of
583+
UnhelpfulSpan _ -> throwError ()
584+
realSpan -> return realSpan
585+
616586
liftMaybe :: Monad m => Maybe a -> ExceptT () m a
617-
liftMaybe val = liftEither $ case val of
587+
liftMaybe val = liftEither $ case val of
618588
Nothing -> Left ()
619-
Just s -> Right s
620-
621-
liftMaybeM :: Monad m => m (Maybe a) -> ExceptT () m a
622-
liftMaybeM mval = do
623-
val <- lift mval
624-
liftMaybe val
589+
Just s -> Right s
625590

626-
runExceptT (getSrcSpanFromPosition oldPos) >>= \case
591+
runExceptT (getTypeSrcSpanFromPosition oldPos) >>= \case
627592
Left () -> return $ IdeResultOk []
628-
Right realSpan -> lift $ srcSpanToFileLocation "hare:findTypeDef" rfm realSpan
593+
Right realSpan ->
594+
lift $ srcSpanToFileLocation "hare:findTypeDef" rfm realSpan
629595
)
630596

631597
-- | Return the definition

0 commit comments

Comments
 (0)