setName called on invalidated PSI node

Howdy. My input is just "func f() { }" for a little sample language. I'm trying to rename f to g.

During a rename, RenameUtil.doRenameGenericNamedElement() calls handleElementRename() on my reference to the "f" node which replaces that node with a new PSI element.  BUT, then right below that in doRenameGenericNamedElement() it does this:

PsiElement namedElementAfterRename = ((PsiNamedElement)namedElement).setName(newName);

calling setName on the namedElement which just got replaced, invaliding namedElement! setName fails obviously.

The return value is currently ignored from both handleElementRename() and setName() so I assume I should do an element.replace(newNode) inside each.

What specifically should handleElementRename() do vs setName()? They are both referring to the same ID node. Should I try to detect when a node has already been replaced in setName? What is the proper thing to do?

(Is this a case where I need to distinguish between def and ref of a function name via PsiNameIdentifierOwner?)



Please sign in to leave a comment.