@@ -142,9 +142,10 @@ renameProvider state pluginId (RenameParams _prog (TextDocumentIdentifier uri) p
142142 -- Perform rename
143143 let newName = mkTcOcc $ T. unpack newNameText
144144 filesRefs = collectWith locToUri refs
145+ oldOccNames = HS. fromList $ map nameOccName oldNames
145146 getFileEdit (uri, locations) = do
146147 verTxtDocId <- liftIO $ runAction " rename: getVersionedTextDoc" state $ getVersionedTextDoc (TextDocumentIdentifier uri)
147- getSrcEdit state verTxtDocId (replaceRefs newName locations)
148+ getSrcEdit state verTxtDocId (replaceRefs newName locations oldOccNames )
148149 fileEdits <- mapM getFileEdit filesRefs
149150 pure $ InL $ fold fileEdits
150151
@@ -214,10 +215,10 @@ getSrcEdit state verTxtDocId updatePs = do
214215replaceRefs ::
215216 OccName ->
216217 HashSet Location ->
218+ HashSet OccName ->
217219 ParsedSource ->
218220 ParsedSource
219- replaceRefs newName refs = everywhere $
220- -- there has to be a better way...
221+ replaceRefs newName refs oldOccNames = everywhere $
221222 mkT (replaceLoc @ AnnListItem ) `extT`
222223 -- replaceLoc @AnnList `extT` -- not needed
223224 -- replaceLoc @AnnParen `extT` -- not needed
@@ -228,8 +229,11 @@ replaceRefs newName refs = everywhere $
228229 where
229230 replaceLoc :: forall an . LocatedAn an RdrName -> LocatedAn an RdrName
230231 replaceLoc (L srcSpan oldRdrName)
231- | isRef (locA srcSpan) = L srcSpan $ replace oldRdrName
232+ | isRef (locA srcSpan)
233+ , isTarget oldRdrName
234+ = L srcSpan $ replace oldRdrName
232235 replaceLoc lOldRdrName = lOldRdrName
236+
233237 replace :: RdrName -> RdrName
234238 replace (Qual modName _) = Qual modName newName
235239 replace _ = Unqual newName
@@ -239,6 +243,10 @@ replaceRefs newName refs = everywhere $
239243 Just loc -> loc `HS.member` refs
240244 Nothing -> False
241245
246+ -- Only replace RdrNames whose OccName matches a rename target, preventing
247+ -- co-located field selectors from being incorrectly renamed.
248+ isTarget :: RdrName -> Bool
249+ isTarget rdrName = rdrNameOcc rdrName `HS.member` oldOccNames
242250---------------------------------------------------------------------------------------------------
243251-- Reference finding
244252
@@ -250,38 +258,23 @@ refsAtName ::
250258 Name ->
251259 ExceptT PluginError m [Location ]
252260refsAtName state nfp targetName = do
253- -- Get local references from current file's HieAST
254- ast <- handleGetHieAst state nfp
255- let localRefs = nameLocs targetName ast
256-
257- -- Query HieDb for global matches (by OccName)
258- ShakeExtras {withHieDb} <- liftIO $ runAction " Rename.HieDb" state getShakeExtras
259- dbCandidates <- liftIO $ withHieDb $ \ hieDb ->
260- fmap (mapMaybe rowToLoc) $
261- case nameModule_maybe targetName of
262- Just mod -> findReferences hieDb True (nameOccName targetName) (Just $ moduleName mod ) (Just $ moduleUnit mod ) []
263- Nothing -> findReferences hieDb True (nameOccName targetName) Nothing Nothing []
264-
265- -- Filter candidates by exact Name identity
266- filteredDbRefs <- filterM (matchesExactName state targetName) dbCandidates
267- pure $ localRefs ++ filteredDbRefs
268-
269- matchesExactName ::
270- MonadIO m =>
271- IdeState ->
272- Name ->
273- Location ->
274- ExceptT PluginError m Bool
275- matchesExactName state targetName loc = do
276- (file, pos) <- locToFilePos loc
277- namesAtPos <- getNamesAtPos state file pos
278- pure $ targetName `elem` namesAtPos
279-
280- nameLocs :: Name -> HieAstResult -> [Location ]
281- nameLocs name (HAR _ _ rm _ _) =
282- concatMap (map (realSrcSpanToLocation . fst ))
283- (M. lookup (Right name) rm)
284-
261+ HAR {refMap} <- handleGetHieAst state nfp
262+
263+ let localRefs =
264+ case M. lookup (Right targetName) refMap of
265+ Nothing -> []
266+ Just spans -> [ realSrcSpanToLocation sp | (sp, _) <- spans]
267+
268+ -- Only query HieDb if it's a global name
269+ globalRefs <-
270+ case nameModule_maybe targetName of
271+ Nothing -> pure []
272+ Just mod -> do
273+ ShakeExtras {withHieDb} <- liftIO $ runAction " Rename.HieDb" state getShakeExtras
274+ liftIO $ withHieDb $ \ hieDb ->
275+ fmap (mapMaybe rowToLoc) $ findReferences hieDb True (nameOccName targetName) (Just $ moduleName mod ) (Just $ moduleUnit mod ) []
276+
277+ pure (localRefs ++ globalRefs)
285278---------------------------------------------------------------------------------------------------
286279-- Util
287280
0 commit comments