Skip to content

Commit 5a01652

Browse files
committed
Fixing an odd edge case, of not being to rename a variable directly under a function definition, but selecting one column to the right worked
1 parent a95cf08 commit 5a01652

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/PowerShellEditorServices/Services/PowerShell/Refactoring/Utilities.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ public static Ast GetAst(int StartLineNumber, int StartColumnNumber, Ast Ast)
123123

124124
if (token is NamedBlockAst)
125125
{
126+
// NamedBlockAST starts on the same line as potentially another AST,
127+
// its likley a user is not after the NamedBlockAst but what it contains
128+
IEnumerable<Ast> stacked_tokens = token.FindAll(ast =>
129+
{
130+
return StartLineNumber == ast.Extent.StartLineNumber &&
131+
ast.Extent.EndColumnNumber >= StartColumnNumber
132+
&& StartColumnNumber >= ast.Extent.StartColumnNumber;
133+
}, true);
134+
135+
if(stacked_tokens.Count() > 1){
136+
return stacked_tokens.LastOrDefault();
137+
}
138+
126139
return token.Parent;
127140
}
128141

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function Write-Item($itemCount) {
2+
$i = 1
3+
4+
while ($i -le $itemCount) {
5+
$str = "Output $i"
6+
Write-Output $str
7+
8+
# In the gutter on the left, right click and select "Add Conditional Breakpoint"
9+
# on the next line. Use the condition: $i -eq 25
10+
$i = $i + 1
11+
12+
# Slow down execution a bit so user can test the "Pause debugger" feature.
13+
Start-Sleep -Milliseconds $DelayMilliseconds
14+
}
15+
}

test/PowerShellEditorServices.Test/Refactoring/RefactorUtilitiesTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ public void GetFunctionDefinitionAst()
142142
Assert.Equal(1,symbol.Extent.StartLineNumber);
143143
Assert.Equal(1,symbol.Extent.StartColumnNumber);
144144

145+
}
146+
[Fact]
147+
public void GetVariableUnderFunctionDef()
148+
{
149+
RenameSymbolParams request = new(){
150+
Column=5,
151+
Line=2,
152+
RenameTo="Renamed",
153+
FileName="TestDetectionUnderFunctionDef.ps1"
154+
};
155+
ScriptFile scriptFile = GetTestScript(request.FileName);
156+
157+
Ast symbol = Utilities.GetAst(request.Line,request.Column,scriptFile.ScriptAst);
158+
Assert.IsType<VariableExpressionAst>(symbol);
159+
Assert.Equal(2,symbol.Extent.StartLineNumber);
160+
Assert.Equal(5,symbol.Extent.StartColumnNumber);
161+
145162
}
146163
[Fact]
147164
public void AssertContainsDotSourcingTrue()

0 commit comments

Comments
 (0)