Can I programatically disable built-in refactorings?

I would like to create an "untested legacy code mode" plugin for R#. This would allow the user to enter a "working with unverified code" mode.R# is great for legacy, but the problem is that it does too much. It performs both strict (behavior-preserving) refactorings and a bunch of useful semantic-level code transforms (not behavior-preserving). When I am first getting nasty legacy under test, I want a tool that restricts me to known-safe refactorings. Once I've got some protection, then I want to pull out the full capabilities. I want this in one tool.It occurred to me that I could use  R# plugin to solve this problem - if I could figure out how to turn off all the code changing features & then selectively white-list the known-safe ones. Is there a supported, good way to accomplish this goal?The answer need only work on 7.1 and later. My users upgrade with each release.Thanks!

You can't directly disable refactorings. Perhaps the closest you can do is to find all of the actions that invoke refactorings (e.g. ChangeSignatureAction, RenameAction, or any other action that derives from ExtensibleRefactoringAction<T>) and adding a new handler that returns false from the Update method to indicate that the action is not available.

In other words, get an instance of IActionManager (via component injection into your constructor), call actionManager.TryGetAction(actionId) and cast to IUpdatableAction. Then call AddHandler and pass in an instance of a class that implements IActionHandler. This can be a simple class that listens on a flag to know when to switch into "restricted" mode. If "restricted" mode is true, return false to the Update method, and then your Execute method won't be called. If you're not in "restricted" mode, call the nextUpdate delegate passed in to Update or Execute. This will cause the original action to execute.

It might be possible for refactorings to be started by means other than these actions, but this might be a good place to start.


Can you update this or elaborate on how to find existing Actions or a list of these actions? I am facing the same issue since I need to selectively exclude certain files from refactoring at clean-up code time. It might be the same if there were a way to easily exclude these files using code to update the settings dynamically. Thanks!


Please sign in to leave a comment.