Skip to content

"Smart" variable rename #261

Open
Open

Description

When I rename a variable in one place, I want to ensure all references to that variable are renamed that are in the same scope. Basically, I want to ensure when a rename a variable that anything that might be depending on that name is renamed as well in the same script. Here's an example:

http://www.screencast.com/t/ggUIoKqSC

Activity

  1. added this to the milestone on Sep 2, 2016
  2. Bill-Stewart commented on Sep 21, 2016

    @Bill-Stewart

    I think you can do this already by 1) selecting the variable, 2) use the command editor.action.selectHighlights (default key mapping is Ctrl+Shift+L), then 3) type the replacement name.

  3. sgtoj commented on Sep 23, 2016

    @sgtoj

    Bill Stewart (@Bill-Stewart) You're somewhat correct. Adam Bertram (@adbertram) is referring to the Rename Symbol (F2) command. It will smartly update the symbol (e.g. variable, class, functions, etc.) versus a blind change of all occurrence of a word.

    Example:

    # get some objects to work with
    $something    = (Get-Something);
    $anotherthing = (Get-AnotherThing);
    
    # set some value from the first object
    $someValue1  = $something.SomeValue;
    $someValue2  = $something.SomeValue;
    
    # set another one from the other object
    $somevalue3  = $anotherthing.SomeValue;
    

    Basic Explanations:
    With Symbol Rename, the editor will understand that both occurrence SomeValue from $something.SomeValue are the same but the one from $anotherthing.SomeValue is not. So the Symbol Rename command of SomeValue property on the $something object will only make two changes even though there are at least 3 occurrence of "somevalue" (actually there are 6).

  4. Bill-Stewart commented on Sep 23, 2016

    @Bill-Stewart

    Well, I interpreted the OP's request as variables rather than object members. I wouldn't mind this if performance is good. (As noted, we can already change variables pretty easily.)

  5. mattmcnabb commented on Dec 5, 2016

    @mattmcnabb
    Contributor

    Plus one on the request from Adam Bertram (@adbertram). The key here is that the token rename should be scope-aware in PowerShell. We've been doing this in ISESteroids for a while:

    image

  6. rkeithhill commented on Dec 5, 2016

    @rkeithhill
    Contributor

    Because of PowerShell's dynamic scoping that is hard to get right i.e. infer scripter intent. In the example above, I'd argue that renaming $Var1 on line 2 shouldn't rename the variable on line 5. Line 2 should be considered a "new" $Var1 variable because of PowerShell's "copy on write" behavior.

    Now if line 2 was $script:Var1 = ... then it would be obvious that line 5 should be renamed.

    However, with dynamic scopes you don't know until runtime which scope a variable is defined in if it isn't defined in the local scope. That makes it hard to ensure correctness with this feature request IMO.

  7. mattmcnabb commented on Dec 6, 2016

    @mattmcnabb
    Contributor

    Keith Hill (@rkeithhill) you're right about the intention of the example above. Sounds like it's difficult to implement - still would be really nice to have. Wonder how it's achieved in ISE Steroids?

  8. adbertram commented on Dec 6, 2016

    @adbertram
    ContributorAuthor
  9. daviwil commented on Dec 6, 2016

    @daviwil
    Contributor

    The question isn't whether it's possible (it is), it's whether the change we make is correct in terms of how PowerShell's variable scoping works. Right now our symbol renaming is primitive but once we start doing it the "right" way, it will adhere as well as it can to PowerShell's scoping rules.

  10. adbertram commented on Dec 6, 2016

    @adbertram
    ContributorAuthor
  11. daviwil commented on Dec 6, 2016

    @daviwil
    Contributor

    Nope ;) You will get a new release with some other shiny features, though.

  12. clijsters commented on Mar 21, 2017

    @clijsters

    Would be a great feature!

  13. ajansveld commented on Aug 16, 2017

    @ajansveld

    Already a fan of vscode but this would really take it to the next level.

  14. johntiger1 commented on Dec 6, 2017

    @johntiger1

    Also would like this feature

  15. 30 remaining items

  16. Omzig commented on Mar 21, 2024

    @Omzig

    can we bump this somehow and get vscode to F2 (rename) a powershell symbol?

  17. SeidChr commented on Mar 21, 2024

    @SeidChr

    *bump

  18. JustinGrote commented on Sep 12, 2024

    @JustinGrote
    Collaborator

    EDIT: New Build 2025-03-01
    Progress is happening! Here is a new alpha preview build of the functionality if you want to try it out.

    powershell-2025.99.0-renameAlpha.zip

    1. Download the above file
    2. Rename .zip to .vsix
    3. Open VSCode, hit F1, and type "VSIX" and hit enter
    4. Browse for the vsix file
    5. Reload VSCode

    Open a .ps1 file on disk (unsaved/untitled currently unsupported) and hit F2. You'll get a popup asking you to opt in, then you can try out renaming functions/providers/symbols

    Capture.mp4
  19. MartinGC94 commented on Sep 12, 2024

    @MartinGC94
    Contributor

    Awesome! It even takes splatting into account (though it is erroneously adding a $ before the parameter name).
    Regarding the creation of an alias attribute when renaming a parameter, I don't think it should be enabled by default. I'd imagine most people will primarily use it in the initial development phase where breaking changes are not a concern so it just gets in the way.

  20. JustinGrote commented on Sep 12, 2024

    @JustinGrote
    Collaborator

    MartinGC94 that's a fair point, it'll be a toggleable setting either way.

    We are building a test suite of supported scenarios, I'm currently refactoring it to be LSP-only so that it will be portable to neovim and whatnot. Once that's done I may recruit you if you have some time to help us refine the AST visitors to help with more edge cases we pick up.

  21. sba923 commented on Sep 20, 2024

    @sba923

    F2 gets me nothing but Unable to write to User Settings because powershell.renameSymbol.acceptRenameDisclaimer is not a registered configuration.

  22. JustinGrote commented on Sep 20, 2024

    @JustinGrote
    Collaborator

    Stéphane BARIZIEN (@sba923) I'm currently doing a pretty major rewrite/refactor on another branch, the vsix I provided should be fine but I should have a better version by net week.

  23. JustinGrote commented on Mar 2, 2025

    @JustinGrote
    Collaborator

    I'm working on this again, just rebased it with the latest changes. See the previous post for the newest build

  24. dotnetCarpenter commented on Jul 29, 2025

    @dotnetCarpenter

    EDIT: New Build 2025-03-01 Progress is happening! Here is a new alpha preview build of the functionality if you want to try it out.

    powershell-2025.99.0-renameAlpha.zip

    Justin Grote (@JustinGrote) I have been using your extension Build 2025-03-01 today for some hours within a single .ps1 and it worked without any issues. Thanks! vscode: 1.102.2, linux

  25. JustinGrote commented on Jul 29, 2025

    @JustinGrote
    Collaborator

    Jon Ege Ronnenberg (@dotnetCarpenter) glad to hear it! The current status is that I realized there is some overlap in AST code between this and our "Find All References" provider, and I would like to unify the logic between the two so that they consistently find references in the same manner. That's going to take some work and I have some tasks I want to finish ahead of it, but it's still very high on my personal VSCode "to-do" list

  26. dotnetCarpenter commented on Jul 30, 2025

    @dotnetCarpenter

    Justin Grote (@JustinGrote) I found a single case where your rename extension does not seem to work.

    function Invoke-CustomFunction {
        $PSVersionTable
    }
    
    Export-ModuleMember -Function Invoke-CustomFunction

    Steps to reproduce

    1. Select Invoke-CustomFunction
    2. Rename the function name (F2)
    3. Observe that the last line Export-ModuleMember -Function Invoke-CustomFunction does not change name.
  27. JustinGrote commented on Jul 31, 2025

    @JustinGrote
    Collaborator

    Jon Ege Ronnenberg (@dotnetCarpenter) in this case it's a string (powershell implicitly makes it 'Invoke-CustomFunction'), not a function name, so the symbols don't match in the AST as it isn't a valid reference. We can look at possiblying doing this for export-modulemember but it is still working "as designed"

  28. dotnetCarpenter commented on Aug 1, 2025

    @dotnetCarpenter

    Ahh, I see. It would be nice if there was a special case for function references after Export-ModuleMember -Function … for modules that export a lot of functions IMHO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions