@@ -34,7 +34,6 @@ import Control.Monad.Except
34
34
import Data.Aeson
35
35
import qualified Data.Aeson.Types as J
36
36
import Data.Char
37
- import qualified Data.Generics as SYB
38
37
import Data.IORef
39
38
import qualified Data.List as List
40
39
import qualified Data.Map as Map
@@ -182,9 +181,6 @@ mkPragmaCompl label insertText =
182
181
safeTyThingId :: TyThing -> Maybe Id
183
182
safeTyThingId (AnId i) = Just i
184
183
safeTyThingId (AConLike (RealDataCon dc)) = Just $ dataConWrapId dc
185
- safeTyThingId (ATyCon tyCon) = case GHC. tyConTyVars tyCon of
186
- [] -> Nothing
187
- (a: _) -> Just a
188
184
safeTyThingId _ = Nothing
189
185
190
186
-- Associates a module's qualifier with its members
@@ -547,85 +543,55 @@ getModule df n = do
547
543
return (pkg, T. pack $ moduleNameString $ moduleName m)
548
544
549
545
-- ---------------------------------------------------------------------
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)
581
546
582
547
-- | 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.
585
550
-- Sum Types can also be searched.
586
551
findTypeDef :: Uri -> Position -> IdeDeferM (IdeResult [Location ])
587
552
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
590
555
(IdeResultOk [] ) -- Default result
591
- (\ tm info NMD {} -> do
556
+ (\ info -> do
592
557
let rfm = revMap info
593
- lm = locMap info
594
558
mm = moduleMap info
559
+ tmap = typeMap info
595
560
oldPos = newPosToOld info pos
561
+
596
562
case (\ x -> Just $ getArtifactsAtPos x mm) =<< oldPos of
597
563
Just ((_, mn) : _) -> gotoModule rfm mn
598
- _ -> do
564
+ _ -> do
599
565
let
600
566
-- | Get SrcSpan of the name at the given position.
601
567
-- If the old position is Nothing, e.g. there is no cached info about it,
602
568
-- Nothing is returned.
603
569
--
604
570
-- Otherwise, searches for the Type of the given position
605
571
-- 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
608
575
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
+
616
586
liftMaybe :: Monad m => Maybe a -> ExceptT () m a
617
- liftMaybe val = liftEither $ case val of
587
+ liftMaybe val = liftEither $ case val of
618
588
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
625
590
626
- runExceptT (getSrcSpanFromPosition oldPos) >>= \ case
591
+ runExceptT (getTypeSrcSpanFromPosition oldPos) >>= \ case
627
592
Left () -> return $ IdeResultOk []
628
- Right realSpan -> lift $ srcSpanToFileLocation " hare:findTypeDef" rfm realSpan
593
+ Right realSpan ->
594
+ lift $ srcSpanToFileLocation " hare:findTypeDef" rfm realSpan
629
595
)
630
596
631
597
-- | Return the definition
0 commit comments