Prevent rename refactoring

I'm defining references and usages for my custom language plugin. Since I cannot rename those consistently I would like to disable the "rename refactoring" operation for these elements.

My initial approach was to have

     isMemberInplaceRenameAvailable 


return false, but this only prevents inline editing, not renaming in general. I still get a popup and default renaming.

My second guess was to implement a specific

     RenamePsiElementProcessor


but there seems to be no method that decides on renaming or not.

What's the proposed way of disabling rename refactoring? (Merely ignoring the rename operation in <element>.setName() seems broken.)

Lars

5 comments
Comment actions Permalink

The easiest solution is to use the <vetoRenameCondition> extension point. Extensions for this EP are implementations of Condition<PsiElement> that return true if renaming the element should be forbidden.

2
Comment actions Permalink

Thanks, very simple and works beautifully.

For the benefit of others, here's some example code: link
And here's the list of extension points, seems there's an extension for everything: link (scroll down to "How to Get the Extension Points List?")

Lars

1
Comment actions Permalink

Hello Lars,

multiple RenameHandler's can be defined. Custom handlers are iterated until in one of them

 
public final boolean isAvailableOnDataContext(final DataContext dataContext) 


returns true. If none of the custom handlers return true, then default handler is checked & invoked.



So to disable rename you could add a RenameHandler,
make its isAvailableOnDataContext return true,
and override its

 
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, final DataContext dataContext)

and

 
public void invoke(@NotNull final Project project, @NotNull final PsiElement[] elements, final DataContext dataContext)

to empty body { }


This might work.

0
Comment actions Permalink

Thanks for your suggestion ... but the veto extension point works and is really simple to set up.
Lars

0
Comment actions Permalink

you are welcome.

I had written that reply before I saw Dmitry's message.

ah well, 2 answers are better than no answer ;)

Someone else might need to tweak rename. That reply could give them some ideas.

0

Please sign in to leave a comment.