@@ -27,6 +27,7 @@ import qualified Data.Map as M
2727import Data.Maybe
2828import Data.Mod.Word
2929import qualified Data.Text as T
30+ import HieDb ((:.) (.. ))
3031import Development.IDE (Recorder , WithPriority ,
3132 usePropertyAction )
3233import Development.IDE.Core.FileStore (getVersionedTextDoc )
@@ -42,7 +43,6 @@ import qualified Development.IDE.GHC.ExactPrint as E
4243import Development.IDE.Plugin.CodeAction
4344import Development.IDE.Spans.AtPoint
4445import Development.IDE.Types.Location
45- import GHC (isGoodSrcSpan )
4646import GHC.Iface.Ext.Types (HieAST (.. ),
4747 HieASTs (.. ),
4848 NodeOrigin (.. ),
@@ -296,29 +296,28 @@ refsAtName ::
296296 NormalizedFilePath ->
297297 Name ->
298298 ExceptT PluginError m [Location ]
299- refsAtName state nfp targetName = do
300- HAR {refMap} <- handleGetHieAst state nfp
301-
302- let localRefs =
303- case M. lookup (Right targetName) refMap of
304- Nothing -> []
305- Just spans -> [ realSrcSpanToLocation sp | (sp, _) <- spans]
306-
307- let defLoc = nameSrcSpan targetName
308- defLocations = case srcSpanToLocation defLoc of
309- Just loc | isGoodSrcSpan defLoc -> [loc]
310- _ -> []
311-
312- -- Only query HieDb if it's a global name
313- globalRefs <-
314- case nameModule_maybe targetName of
315- Nothing -> pure []
316- Just mod -> do
317- ShakeExtras {withHieDb} <- liftIO $ runAction " Rename.HieDb" state getShakeExtras
318- liftIO $ withHieDb $ \ hieDb ->
319- fmap (mapMaybe rowToLoc) $ findReferences hieDb True (nameOccName targetName) (Just $ moduleName mod ) (Just $ moduleUnit mod ) []
320-
321- pure (defLocations ++ localRefs ++ globalRefs)
299+ refsAtName state nfp name = do
300+ ShakeExtras {withHieDb} <- liftIO $ runAction " Rename.HieDb" state getShakeExtras
301+ ast <- handleGetHieAst state nfp
302+ dbRefs <- case nameModule_maybe name of
303+ Nothing -> pure []
304+ Just mod -> liftIO $ mapMaybe rowToLoc <$> withHieDb (\ hieDb ->
305+ -- See Note [Generated references]
306+ filter (\ (refRow HieDb. :. _) -> not (refIsGenerated refRow)) <$>
307+ findReferences
308+ hieDb
309+ True
310+ (nameOccName name)
311+ (Just $ moduleName mod )
312+ (Just $ moduleUnit mod )
313+ [fromNormalizedFilePath nfp]
314+ )
315+ pure $ nameLocs name ast ++ dbRefs
316+
317+ nameLocs :: Name -> HieAstResult -> [Location ]
318+ nameLocs name (HAR _ _ rm _ _) =
319+ concatMap (map (realSrcSpanToLocation . fst ))
320+ (M. lookup (Right name) rm)
322321---------------------------------------------------------------------------------------------------
323322-- Util
324323
@@ -371,10 +370,35 @@ getNamesAtPoint' hf pos =
371370 concat $ pointCommand hf pos (rights . M. keys . getNodeIds)
372371
373372-- | A variant of `getNamesAtPoint'` that also returns source spans.
373+ -- | A variant of `getNamesAtPoint'` that also returns source spans,
374+ -- trimmed to just the unqualified identifier (excluding any "Module." prefix).
374375getNamesSpansAtPoint' :: HieASTs a -> Position -> [([Name ], RealSrcSpan )]
375376getNamesSpansAtPoint' hf pos =
376377 pointCommand hf pos $
377- \ astNode -> (rights . M. keys . getNodeIds $ astNode, nodeSpan astNode)
378+ \ astNode ->
379+ let names = rights . M. keys . getNodeIds $ astNode
380+ srcSpan = nodeSpan astNode
381+ trimmed = trimQualifierSpan names srcSpan
382+ in (names, trimmed)
383+
384+ -- | Advance the start column of a span past any "Qualifier." prefix,
385+ -- using the OccName length of the first Name to find where the
386+ -- identifier begins.
387+ trimQualifierSpan :: [Name ] -> RealSrcSpan -> RealSrcSpan
388+ trimQualifierSpan (n: _) sp
389+ | qualLen > 0
390+ = mkRealSrcSpan
391+ (mkRealSrcLoc file startLine (startCol + qualLen))
392+ (realSrcSpanEnd sp)
393+ | otherwise = sp
394+ where
395+ spanLen = srcLocCol (realSrcSpanEnd sp) - srcLocCol (realSrcSpanStart sp)
396+ identLen = length (occNameString (nameOccName n))
397+ qualLen = spanLen - identLen
398+ file = srcSpanFile sp
399+ startLine = srcLocLine (realSrcSpanStart sp)
400+ startCol = srcLocCol (realSrcSpanStart sp)
401+ trimQualifierSpan [] sp = sp
378402
379403locToUri :: Location -> Uri
380404locToUri (Location uri _) = uri
@@ -407,16 +431,14 @@ exportNameLocs pm names = do
407431#else
408432 IEVar _ ieWrapped -> matchWrapped (getLoc export) ieWrapped
409433#endif
410- IEThingAll {} -> unsupported
411- IEThingWith {} -> unsupported
412- IEModuleContents {} -> unsupported
413- IEThingAbs {} -> unsupported
414- IEGroup {} -> unsupported
415- IEDoc {} -> unsupported
416- IEDocNamed {} -> unsupported
434+ IEThingAll {} -> pure []
435+ IEThingWith {} -> pure []
436+ IEModuleContents {} -> pure []
437+ IEThingAbs {} -> pure []
438+ IEGroup {} -> pure []
439+ IEDoc {} -> pure []
440+ IEDocNamed {} -> pure []
417441 where
418- unsupported = throwError $ PluginInternalError " Renaming is unsupported for complex export forms"
419-
420442 matchWrapped :: SrcSpan -> LIEWrappedName GhcPs -> ExceptT PluginError (HandlerM config ) [Location ]
421443 matchWrapped l ieWrapped =
422444 case unwrapIEWrappedName (unLoc ieWrapped) of
0 commit comments