Skip to content

Commit 1dc0c45

Browse files
committed
adjusted rename to use utilities
1 parent 15ecd2b commit 1dc0c45

File tree

2 files changed

+7
-25
lines changed

2 files changed

+7
-25
lines changed

src/PowerShellEditorServices/Services/PowerShell/Handlers/PrepareRenameSymbol.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
1212
using Microsoft.PowerShell.EditorServices.Refactoring;
13-
using System.Collections.Generic;
14-
using System.Linq;
13+
using Microsoft.PowerShell.EditorServices.Services.Symbols;
1514

1615
namespace Microsoft.PowerShell.EditorServices.Handlers
1716
{
@@ -55,16 +54,9 @@ public async Task<PrepareRenameSymbolResult> Handle(PrepareRenameSymbolParams re
5554
message = ""
5655
};
5756
// ast is FunctionDefinitionAst or CommandAst or VariableExpressionAst or StringConstantExpressionAst &&
58-
Ast token = scriptFile.ScriptAst.Find(ast =>
59-
{
60-
return request.Line + 1 == ast.Extent.StartLineNumber &&
61-
request.Column + 1 >= ast.Extent.StartColumnNumber;
62-
}, true);
63-
IEnumerable<Ast> tokens = token.FindAll(ast =>{
64-
return ast.Extent.StartColumnNumber <= request.Column &&
65-
ast.Extent.EndColumnNumber >= request.Column;
66-
},true);
67-
token = tokens.LastOrDefault();
57+
SymbolReference symbol = scriptFile.References.TryGetSymbolAtPosition(request.Line + 1, request.Column + 1);
58+
Ast token = Utilities.GetAst(request.Line + 1,request.Column + 1,scriptFile.ScriptAst);
59+
6860
if (token == null)
6961
{
7062
result.message = "Unable to find symbol";
@@ -93,7 +85,7 @@ public async Task<PrepareRenameSymbolResult> Handle(PrepareRenameSymbolParams re
9385
break;
9486
}
9587

96-
case VariableExpressionAst or CommandAst:
88+
case VariableExpressionAst or CommandAst or CommandParameterAst or ParameterAst or StringConstantExpressionAst:
9789
{
9890

9991
try

src/PowerShellEditorServices/Services/PowerShell/Handlers/RenameSymbol.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.Extensions.Logging;
1212
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
1313
using Microsoft.PowerShell.EditorServices.Refactoring;
14-
using System.Linq;
1514
namespace Microsoft.PowerShell.EditorServices.Handlers
1615
{
1716
[Serial, Method("powerShell/renameSymbol")]
@@ -94,7 +93,7 @@ internal static ModifiedFileResponse RenameFunction(Ast token, Ast scriptAst, Re
9493
}
9594
internal static ModifiedFileResponse RenameVariable(Ast symbol, Ast scriptAst, RenameSymbolParams request)
9695
{
97-
if (symbol is VariableExpressionAst or ParameterAst)
96+
if (symbol is VariableExpressionAst or ParameterAst or CommandParameterAst or StringConstantExpressionAst)
9897
{
9998
IterativeVariableRename visitor = new(request.RenameTo,
10099
symbol.Extent.StartLineNumber,
@@ -122,16 +121,7 @@ public async Task<RenameSymbolResult> Handle(RenameSymbolParams request, Cancell
122121
return await Task.Run(() =>
123122
{
124123

125-
Ast token = scriptFile.ScriptAst.Find(ast =>
126-
{
127-
return request.Line + 1 == ast.Extent.StartLineNumber &&
128-
request.Column + 1 >= ast.Extent.StartColumnNumber;
129-
}, true);
130-
IEnumerable<Ast> tokens = token.FindAll(ast =>{
131-
return ast.Extent.StartColumnNumber <= request.Column &&
132-
ast.Extent.EndColumnNumber >= request.Column;
133-
},true);
134-
token = tokens.LastOrDefault();
124+
Ast token = Utilities.GetAst(request.Line + 1,request.Column + 1,scriptFile.ScriptAst);
135125

136126
if (token == null) { return null; }
137127

0 commit comments

Comments
 (0)