Skip to content

Commit 43c719a

Browse files
committed
Move renameservice
1 parent 4910ab8 commit 43c719a

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/PowerShellEditorServices/Services/TextDocument/Services/RenameService.cs renamed to src/PowerShellEditorServices/Services/TextDocument/RenameService.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ ILanguageServerConfiguration config
112112

113113
internal static TextEdit[] RenameFunction(Ast target, Ast scriptAst, RenameParams renameParams)
114114
{
115-
if (target is not FunctionDefinitionAst or CommandAst)
115+
if (target is not (FunctionDefinitionAst or CommandAst))
116116
{
117117
throw new HandlerErrorException($"Asked to rename a function but the target is not a viable function type: {target.GetType()}. This should not happen as PrepareRename should have already checked for viability. File an issue if you see this.");
118118
}
@@ -151,22 +151,9 @@ internal static TextEdit[] RenameVariable(Ast symbol, Ast scriptAst, RenameParam
151151
// Filters just the ASTs that are candidates for rename
152152
typeof(FunctionDefinitionAst),
153153
typeof(VariableExpressionAst),
154-
typeof(CommandParameterAst),
155-
typeof(ParameterAst),
156-
typeof(StringConstantExpressionAst),
157154
typeof(CommandAst)
158155
]);
159156

160-
// Special detection for Function calls that dont follow verb-noun syntax e.g. DoThing
161-
// It's not foolproof but should work in most cases where it is explicit (e.g. not & $x)
162-
if (ast is StringConstantExpressionAst stringAst)
163-
{
164-
if (stringAst.Parent is not CommandAst parent) { return null; }
165-
if (parent.GetCommandName() != stringAst.Value) { return null; }
166-
if (parent.CommandElements[0] != stringAst) { return null; }
167-
// TODO: Potentially find if function was defined earlier in the file to avoid native executable renames and whatnot?
168-
}
169-
170157
// Only the function name is valid for rename, not other components
171158
if (ast is FunctionDefinitionAst funcDefAst)
172159
{
@@ -176,6 +163,20 @@ internal static TextEdit[] RenameVariable(Ast symbol, Ast scriptAst, RenameParam
176163
}
177164
}
178165

166+
// Only the command name (function call) portion is renamable
167+
if (ast is CommandAst command)
168+
{
169+
if (command.CommandElements[0] is not StringConstantExpressionAst name)
170+
{
171+
return null;
172+
}
173+
174+
if (!new ScriptExtentAdapter(name.Extent).Contains(position))
175+
{
176+
return null;
177+
}
178+
}
179+
179180
return ast;
180181
}
181182

@@ -216,18 +217,18 @@ public AstVisitAction Visit(Ast ast)
216217
// If this is our first run, we need to verify we are in scope and gather our rename operation info
217218
if (!skipVerify && CurrentDocument is null)
218219
{
219-
if (ast.Find(ast => ast == target, true) is null)
220+
CurrentDocument = ast.GetHighestParent();
221+
if (CurrentDocument.Find(ast => ast == target, true) is null)
220222
{
221223
throw new TargetSymbolNotFoundException("The target this visitor would rename is not present in the AST. This is a bug and you should file an issue");
222224
}
223-
CurrentDocument = ast.GetHighestParent();
224225

225226
FunctionDefinitionAst functionDef = target switch
226227
{
227228
FunctionDefinitionAst f => f,
228229
CommandAst command => CurrentDocument.FindFunctionDefinition(command)
229230
?? throw new TargetSymbolNotFoundException("The command to rename does not have a function definition. Renaming a function is only supported when the function is defined within the same scope"),
230-
_ => throw new Exception("Unsupported AST type encountered")
231+
_ => throw new Exception($"Unsupported AST type {target.GetType()} encountered")
231232
};
232233

233234
OldName = functionDef.Name;
@@ -286,7 +287,6 @@ private bool ShouldRename(Ast candidate)
286287
// If the command is defined in the same parent scope as the function
287288
return command.HasParent(target.Parent);
288289

289-
290290
// If we get this far, we hit an edge case
291291
throw new InvalidOperationException("ShouldRename for a function could not determine the viability of a rename. This is a bug and you should file an issue.");
292292
}

0 commit comments

Comments
 (0)