-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RenameProvider for variable/function renaming #2152
base: main
Are you sure you want to change the base?
Conversation
We will also need to make sure we account for the scoping issues as mentioned by Rob that are very difficult to handle, even if it means we warn about this as best effort, or straight up refuse to do it. |
…nder a function definition, but selecting one column to the right worked
I made some test cases for renaming from within a for / each loop and limiting the rename to the scope if the var is defined within the creation of the statement. My only concern is a case like below. If you run the code powershell treats the $a = 1..5
$b = 6..10
function test {
process {
$i = 10
for ($Renamed = 0; $Renamed -lt $b.Count; $Renamed++) {
$null = $Renamed
}
for ($i = 0; $i -lt $a.Count; $i++) {
write-output $i
}
write-output "I will be 5 : $i not 10"
}
}
test |
test/PowerShellEditorServices.Test/Refactoring/RefactorVariableTests.cs
Outdated
Show resolved
Hide resolved
test/PowerShellEditorServices.Test.Shared/Refactoring/Functions/ForeachFunction.ps1
Outdated
Show resolved
Hide resolved
test/PowerShellEditorServices.Test.Shared/Refactoring/Functions/ForeachFunction.ps1
Outdated
Show resolved
Hide resolved
foreach ($number in $x) { | ||
testing_files $number | ||
|
||
function testing_files { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename on function
applies to the keyword. I like this behavior if it isn't immediately intuitive, should we do the same for like foreach as well for the inner var?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this behavior aswell, its a happy byproduct of how the FunctionDefinitionAST works, I don't think it would be too hard to get it working for for / foreach statments. In the screenshot below you can see that the VariableExpressionAst is nicely nested just need some logic to target it when the parent is selected.
src/PowerShellEditorServices/Services/PowerShell/Refactoring/IterativeVariableVisitor.cs
Outdated
Show resolved
Hide resolved
test/PowerShellEditorServices.Test/Refactoring/RefactorFunctionTests.cs
Outdated
Show resolved
Hide resolved
… scriptAST correctly otherwise it just picks up the $a = 1..5
…mer, proper error return's now when renaming commandAST thats not defined in the script file
…d if it exists before the functions definition
…o use TheoryData / parameterized tests
…l be reused for variable tests
…which is just sorting the changes
…r is target is within a parameter block within a function to solve
…scope and a helper method IsVariableExpressionAssignedInTargetScope
PR Summary
This is a sister pull request to pr #4950 and issue Smart Variable Rename. It implements the server side component to allow refactoring of Powershell functions and variables.
It does this by implementing two new handlers
RenameSymbolHandler
andPrepareRenameSymbolHandler
which is just plumbing to receive the incoming requests from the client.PrepareRenameSymbolHandler
is used to determine if the rename is possible andRenameSymbolHandler
is what actually returns the edits to the client.All the action to decide what is renamed is done within two classes
IterativeFunctionRename
andIterativeVariableRename
to boil it down the Abstract Syntax Tree(AST) of the script file is analyzed, then the symbol at the target line and column is retrieved. It then loops through the AST following rules to determine scope and what to rename. This is then passed back to the client to action.Below are the current features
Below are the current limitations
Tests to cover as many scenarios as I could think of and to ensure the features described above function have been written
PR Context
This pull request should be helpful as its implementing a feature that I think has been desired for some time among Powershell developers. It should improve coding efficiency and allow code to be refactored with ease.