Skip to content

Commit 29f3062

Browse files
committedJun 6, 2024
CommandAst input was being sent to variable renamer not function renamer, proper error return's now when renaming commandAST thats not defined in the script file
1 parent e3633b6 commit 29f3062

File tree

2 files changed

+68
-42
lines changed

2 files changed

+68
-42
lines changed
 

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

+47-27
Original file line numberDiff line numberDiff line change
@@ -67,40 +67,60 @@ public async Task<PrepareRenameSymbolResult> Handle(PrepareRenameSymbolParams re
6767
result.message = "Dot Source detected, this is currently not supported operation aborted";
6868
return result;
6969
}
70+
71+
bool IsFunction = false;
72+
string tokenName = "";
73+
7074
switch (token)
7175
{
72-
case FunctionDefinitionAst funcDef:
73-
{
74-
try
75-
{
7676

77-
IterativeFunctionRename visitor = new(funcDef.Name,
78-
request.RenameTo,
79-
funcDef.Extent.StartLineNumber,
80-
funcDef.Extent.StartColumnNumber,
81-
scriptFile.ScriptAst);
82-
}
83-
catch (FunctionDefinitionNotFoundException)
84-
{
77+
case FunctionDefinitionAst FuncAst:
78+
IsFunction = true;
79+
tokenName = FuncAst.Name;
80+
break;
81+
case VariableExpressionAst or CommandParameterAst or ParameterAst:
82+
IsFunction = false;
83+
tokenName = request.RenameTo;
84+
break;
85+
case StringConstantExpressionAst:
8586

86-
result.message = "Failed to Find function definition within current file";
87-
}
88-
89-
break;
87+
if (token.Parent is CommandAst CommAst)
88+
{
89+
IsFunction = true;
90+
tokenName = CommAst.GetCommandName();
9091
}
91-
92-
case VariableExpressionAst or CommandAst or CommandParameterAst or ParameterAst or StringConstantExpressionAst:
92+
else
9393
{
94-
IterativeVariableRename visitor = new(request.RenameTo,
95-
token.Extent.StartLineNumber,
96-
token.Extent.StartColumnNumber,
97-
scriptFile.ScriptAst);
98-
if (visitor.TargetVariableAst == null)
99-
{
100-
result.message = "Failed to find variable definition within the current file";
101-
}
102-
break;
94+
IsFunction = false;
10395
}
96+
break;
97+
}
98+
99+
if (IsFunction)
100+
{
101+
try
102+
{
103+
IterativeFunctionRename visitor = new(tokenName,
104+
request.RenameTo,
105+
token.Extent.StartLineNumber,
106+
token.Extent.StartColumnNumber,
107+
scriptFile.ScriptAst);
108+
}
109+
catch (FunctionDefinitionNotFoundException)
110+
{
111+
result.message = "Failed to Find function definition within current file";
112+
}
113+
}
114+
else
115+
{
116+
IterativeVariableRename visitor = new(tokenName,
117+
token.Extent.StartLineNumber,
118+
token.Extent.StartColumnNumber,
119+
scriptFile.ScriptAst);
120+
if (visitor.TargetVariableAst == null)
121+
{
122+
result.message = "Failed to find variable definition within the current file";
123+
}
104124
}
105125
return result;
106126
}).ConfigureAwait(false);

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

+21-15
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,28 @@ public RenameSymbolHandler(ILoggerFactory loggerFactory, WorkspaceService worksp
7373
}
7474
internal static ModifiedFileResponse RenameFunction(Ast token, Ast scriptAst, RenameSymbolParams request)
7575
{
76+
string tokenName = "";
7677
if (token is FunctionDefinitionAst funcDef)
7778
{
78-
IterativeFunctionRename visitor = new(funcDef.Name,
79-
request.RenameTo,
80-
funcDef.Extent.StartLineNumber,
81-
funcDef.Extent.StartColumnNumber,
82-
scriptAst);
83-
visitor.Visit(scriptAst);
84-
ModifiedFileResponse FileModifications = new(request.FileName)
85-
{
86-
Changes = visitor.Modifications
87-
};
88-
return FileModifications;
89-
79+
tokenName = funcDef.Name;
9080
}
91-
return null;
81+
else if (token.Parent is CommandAst CommAst)
82+
{
83+
tokenName = CommAst.GetCommandName();
84+
}
85+
IterativeFunctionRename visitor = new(tokenName,
86+
request.RenameTo,
87+
token.Extent.StartLineNumber,
88+
token.Extent.StartColumnNumber,
89+
scriptAst);
90+
visitor.Visit(scriptAst);
91+
ModifiedFileResponse FileModifications = new(request.FileName)
92+
{
93+
Changes = visitor.Modifications
94+
};
95+
return FileModifications;
96+
97+
9298

9399
}
94100
internal static ModifiedFileResponse RenameVariable(Ast symbol, Ast scriptAst, RenameSymbolParams request)
@@ -121,11 +127,11 @@ public async Task<RenameSymbolResult> Handle(RenameSymbolParams request, Cancell
121127
return await Task.Run(() =>
122128
{
123129

124-
Ast token = Utilities.GetAst(request.Line + 1,request.Column + 1,scriptFile.ScriptAst);
130+
Ast token = Utilities.GetAst(request.Line + 1, request.Column + 1, scriptFile.ScriptAst);
125131

126132
if (token == null) { return null; }
127133

128-
ModifiedFileResponse FileModifications = token is FunctionDefinitionAst
134+
ModifiedFileResponse FileModifications = (token is FunctionDefinitionAst || token.Parent is CommandAst)
129135
? RenameFunction(token, scriptFile.ScriptAst, request)
130136
: RenameVariable(token, scriptFile.ScriptAst, request);
131137

0 commit comments

Comments
 (0)