Search code examples
vbarubberduck

VBA Rubberduck How to "move closer to usage" multiple variables?


Rubberduck has a refactoring tool called "Move closer to usage". It is called by right-clicking on the variable to be refactored. Is it possible to "move closer to usage" of multiple variables at once, or is it only relevant for one?

enter image description here


Solution

  • Rubberduck performs refactorings by directly manipulating the source tokens in a "rewrite session", where multiple rewrite operations can be made, as long as no token gets rewritten twice; the rewritten token stream is then dumped to a temporary file and the entire module gets rewritten at once.

    Some refactorings are invoked by inspection quick-fixes; there's an inspection that will suggest move closer to usage as a quick-fix, and while several quick-fixes can indeed be applied to multiple inspection results at once, the refactoring-based quick-fixes explicitly disallow it:

    https://github.com/rubberduck-vba/Rubberduck/blob/d6b0835c40a79c6f01f756d31f9756d8455a782f/Rubberduck.CodeAnalysis/QuickFixes/Abstract/RefactoringQuickFixBase.cs#L42

    public override bool CanFixMultiple => false;
    public override bool CanFixInProcedure => false;
    public override bool CanFixInModule => false;
    public override bool CanFixInProject => false;
    public override bool CanFixAll => false;
    

    This is to avoid any possibility of accidentally rewriting the same token twice in the same rewrite session, which would be throwing an exception.

    So yeah, it has to be one at a time, unfortunately.