Rename / FindUsage for 'adhoc' declaration


I'm having a issue getting the refactor-rename mechanism to pop-up for the following scenario:

The plugin is extending the YAML language and I'm creating a declaration using a sequence, then at another location I'm using that variable. So let's say the document looks like:


-  $variable = 'something'
someProperty: $variable


I can create a reference from the usage at someProperty which will resolve to the YamlPlainText ($variable = 'something'). But there was no FindUsage available. I fixed that by using the PsiSymbolDeclarationProvider which creates the PsiSymbolDeclaration. Now the FindUsage works :)
But now I also want to support refactor-rename which is not available as an option when I have the caret at the declaration. When I have the caret at the usage than it works. Unfortunately I cannot 'catch' the refactor-rename operation this way and since the PlainText is not a PsiNamedElement with a NameIdentifier it doesn't rename the declaration.

I could work around this too by placing a reference on the declaration which points to itself. Now I can use the handleElementRename of the reference to rightfully rename the PlainText but this leaves me with the last issue: I can no longer trigger the FindUsages by clicking on the declaration, since that would simply resolve the self-reference.

I wonder if I'm missing some kind of provider that I can register for these kinds of situations. Ideally I would like to be able to override a method in the PsiSymbolDeclaration where it would also be called with a newName so I can determine exactly how the PlainText should look like afterwards.

1 comment

Never mind, I found it.

- adding RenameHandler that handles this specific case
- adding RenamePsiElementProcessor to capture how the PlainTextImpl is to be renamed

The self-reference construction can be removed (luckily)

So to summarize, in order for the YAMLPlainText $variable = 'something' to behave like a declaration I added:

- PsiSymbolDeclarationProvider => FindUsage is triggered when ctrl+clicking it
- RenameHandler => Enables the refactor-rename option
- RenamePsiElementProcessor => Replaces the YAMLPlainText correctly with the new value

Don't know if this is the desired way to do it, but it works :)


Please sign in to leave a comment.