Caret should be positioned at symbol to be renamed

Answered

Hello all,

Trying to write my first plugin I am trying to rename the identifier at the caret in a java file using the builtin RenameProcessor.

I then systematically get the same error message 

"Cannot perform refactoring"

"Caret should be positioned at symbol to be renamed".

The caret is positioned correctly and the identifier can be renamed from the Refactor/Rename... menu item.

 

 

I have seen a similar error reported by different users with different languages (at least JSon) but no solution posted yet.
I am running Community 2022.2.4

 

0
6 comments

Hi,

I suggest debugging:

https://github.com/JetBrains/intellij-community/blob/master/platform/refactoring/src/com/intellij/refactoring/rename/PsiElementRenameHandler.java#L125

in both cases: renaming from your action and from the Refactor | Rename and compare what is missing in your case. It's hard to guess what the reason is with the information you provided.

0

Thank you Karol Lewandowski. Indeed debugging seems the way to go.

0

Hi,

Could you please share the reason and solution to help others who face the same in the future?

0

Sorry about my phrasing. I meant that the idea of debugging seems reasonable: not that it has actually worked yet. But of course, if I find the reason I will post it.

0

I have set a breakpoint at the line you linked.

hasRenameProcessor is false

hasWritableMetaData  is false

element is an instance of PsiIdentifierImpl and does not seem to be an (indrect) instance of PsiNamedElement

if (!hasRenameProcessor && !hasWritableMetaData && !(element instanceof PsiNamedElement)) {
return RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.symbol.to.rename"));
}

0

ChatGPT4 has found the bug. Instead of an element a namedElement is needed as suggested by the condition you pointed at Karol Lewandowski, thanks.
This line must be added after the initialization of element and of course namedElement must be used the in stead of element.

PsiNamedElement namedElement = PsiTreeUtil.getParentOfType(element, PsiNamedElement.class);
0

Please sign in to leave a comment.