Skip to content

Commit f74f084

Browse files
committed
Fix refs at name
Reverts to the old implementation of refs at name. also fixes the filter for generated references which was causing problems on cross module renames.
1 parent 5c7188c commit f74f084

1 file changed

Lines changed: 56 additions & 35 deletions

File tree

  • plugins/hls-rename-plugin/src/Ide/Plugin

plugins/hls-rename-plugin/src/Ide/Plugin/Rename.hs

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ import qualified Development.IDE.GHC.ExactPrint as E
4242
import Development.IDE.Plugin.CodeAction
4343
import Development.IDE.Spans.AtPoint
4444
import Development.IDE.Types.Location
45-
import GHC (isGoodSrcSpan)
4645
import GHC.Iface.Ext.Types (HieAST (..),
4746
HieASTs (..),
4847
NodeOrigin (..),
4948
SourcedNodeInfo (..))
5049
import GHC.Iface.Ext.Utils (generateReferencesMap)
50+
import HieDb ((:.) (..))
5151
import HieDb.Query
5252
import HieDb.Types (RefRow (refIsGenerated))
5353
import Ide.Logger (Pretty (..),
@@ -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

@@ -370,11 +369,35 @@ getNamesAtPoint' :: HieASTs a -> Position -> [Name]
370369
getNamesAtPoint' hf pos =
371370
concat $ pointCommand hf pos (rights . M.keys . getNodeIds)
372371

373-
-- | A variant of `getNamesAtPoint'` that also returns source spans.
372+
-- | A variant of `getNamesAtPoint'` that also returns source spans,
373+
-- trimmed to just the unqualified identifier (excluding any "Module." prefix).
374374
getNamesSpansAtPoint' :: HieASTs a -> Position -> [([Name], RealSrcSpan)]
375375
getNamesSpansAtPoint' hf pos =
376376
pointCommand hf pos $
377-
\astNode -> (rights . M.keys . getNodeIds $ astNode, nodeSpan astNode)
377+
\astNode ->
378+
let names = rights . M.keys . getNodeIds $ astNode
379+
srcSpan = nodeSpan astNode
380+
trimmed = trimQualifierSpan names srcSpan
381+
in (names, trimmed)
382+
383+
-- | Advance the start column of a span past any "Qualifier." prefix,
384+
-- using the OccName length of the first Name to find where the
385+
-- identifier begins.
386+
trimQualifierSpan :: [Name] -> RealSrcSpan -> RealSrcSpan
387+
trimQualifierSpan (n:_) sp
388+
| qualLen > 0
389+
= mkRealSrcSpan
390+
(mkRealSrcLoc file startLine (startCol + qualLen))
391+
(realSrcSpanEnd sp)
392+
| otherwise = sp
393+
where
394+
spanLen = srcLocCol (realSrcSpanEnd sp) - srcLocCol (realSrcSpanStart sp)
395+
identLen = length (occNameString (nameOccName n))
396+
qualLen = spanLen - identLen
397+
file = srcSpanFile sp
398+
startLine = srcLocLine (realSrcSpanStart sp)
399+
startCol = srcLocCol (realSrcSpanStart sp)
400+
trimQualifierSpan [] sp = sp
378401

379402
locToUri :: Location -> Uri
380403
locToUri (Location uri _) = uri
@@ -407,16 +430,14 @@ exportNameLocs pm names = do
407430
#else
408431
IEVar _ ieWrapped -> matchWrapped (getLoc export) ieWrapped
409432
#endif
410-
IEThingAll{} -> unsupported
411-
IEThingWith{} -> unsupported
412-
IEModuleContents{} -> unsupported
413-
IEThingAbs{} -> unsupported
414-
IEGroup{} -> unsupported
415-
IEDoc{} -> unsupported
416-
IEDocNamed{} -> unsupported
433+
IEThingAll{} -> pure []
434+
IEThingWith{} -> pure []
435+
IEModuleContents{} -> pure []
436+
IEThingAbs{} -> pure []
437+
IEGroup{} -> pure []
438+
IEDoc{} -> pure []
439+
IEDocNamed{} -> pure []
417440
where
418-
unsupported = throwError $ PluginInternalError "Renaming is unsupported for complex export forms"
419-
420441
matchWrapped :: SrcSpan -> LIEWrappedName GhcPs -> ExceptT PluginError (HandlerM config) [Location]
421442
matchWrapped l ieWrapped =
422443
case unwrapIEWrappedName (unLoc ieWrapped) of

0 commit comments

Comments
 (0)