Skip to content

Commit 107230a

Browse files
Razmo99JustinGrote
authored andcommitted
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 939d568 commit 107230a

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

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

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,12 @@ public static FunctionDefinitionAst GetFunctionDefByCommandAst(string OldName, i
100100
return CorrectDefinition;
101101
}
102102

103-
public static bool AssertContainsDotSourced(Ast ScriptAst){
104-
Ast dotsourced = ScriptAst.Find(ast =>{
103+
public static bool AssertContainsDotSourced(Ast ScriptAst)
104+
{
105+
Ast dotsourced = ScriptAst.Find(ast =>
106+
{
105107
return ast is CommandAst commandAst && commandAst.InvocationOperator == TokenKind.Dot;
106-
},true);
108+
}, true);
107109
if (dotsourced != null)
108110
{
109111
return true;
@@ -121,8 +123,36 @@ public static Ast GetAst(int StartLineNumber, int StartColumnNumber, Ast Ast)
121123
StartColumnNumber >= ast.Extent.StartColumnNumber;
122124
}, true);
123125

124-
IEnumerable<Ast> token = null;
125-
token = Ast.FindAll(ast =>
126+
if (token is NamedBlockAst)
127+
{
128+
// NamedBlockAST starts on the same line as potentially another AST,
129+
// its likley a user is not after the NamedBlockAst but what it contains
130+
IEnumerable<Ast> stacked_tokens = token.FindAll(ast =>
131+
{
132+
return StartLineNumber == ast.Extent.StartLineNumber &&
133+
ast.Extent.EndColumnNumber >= StartColumnNumber
134+
&& StartColumnNumber >= ast.Extent.StartColumnNumber;
135+
}, true);
136+
137+
if (stacked_tokens.Count() > 1)
138+
{
139+
return stacked_tokens.LastOrDefault();
140+
}
141+
142+
return token.Parent;
143+
}
144+
145+
if (null == token)
146+
{
147+
IEnumerable<Ast> LineT = Ast.FindAll(ast =>
148+
{
149+
return StartLineNumber == ast.Extent.StartLineNumber &&
150+
StartColumnNumber >= ast.Extent.StartColumnNumber;
151+
}, true);
152+
return LineT.OfType<FunctionDefinitionAst>()?.LastOrDefault();
153+
}
154+
155+
IEnumerable<Ast> tokens = token.FindAll(ast =>
126156
{
127157
return ast.Extent.EndColumnNumber >= StartColumnNumber
128158
&& StartColumnNumber >= ast.Extent.StartColumnNumber;
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
@@ -152,6 +152,23 @@ public void GetFunctionDefinitionAst()
152152
Assert.Equal(1, symbol.Extent.StartLineNumber);
153153
Assert.Equal(1, symbol.Extent.StartColumnNumber);
154154

155+
}
156+
[Fact]
157+
public void GetVariableUnderFunctionDef()
158+
{
159+
RenameSymbolParams request = new(){
160+
Column=5,
161+
Line=2,
162+
RenameTo="Renamed",
163+
FileName="TestDetectionUnderFunctionDef.ps1"
164+
};
165+
ScriptFile scriptFile = GetTestScript(request.FileName);
166+
167+
Ast symbol = Utilities.GetAst(request.Line,request.Column,scriptFile.ScriptAst);
168+
Assert.IsType<VariableExpressionAst>(symbol);
169+
Assert.Equal(2,symbol.Extent.StartLineNumber);
170+
Assert.Equal(5,symbol.Extent.StartColumnNumber);
171+
155172
}
156173
[Fact]
157174
public void AssertContainsDotSourcingTrue()

0 commit comments

Comments
 (0)