Skip to content

Commit 92ae8f4

Browse files
committed
Broke up Process node into 3 sub functions for readability
1 parent 9a74a1b commit 92ae8f4

File tree

1 file changed

+114
-100
lines changed

1 file changed

+114
-100
lines changed

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

Lines changed: 114 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -256,115 +256,129 @@ public void ProcessNode(Ast node)
256256
switch (node)
257257
{
258258
case CommandAst commandAst:
259-
// Is the Target Variable a Parameter and is this commandAst the target function
260-
if (isParam && commandAst.GetCommandName()?.ToLower() == TargetFunction?.Name.ToLower())
261-
{
262-
// Check to see if this is a splatted call to the target function.
263-
Ast Splatted = null;
264-
foreach (Ast element in commandAst.CommandElements)
265-
{
266-
if (element is VariableExpressionAst varAst && varAst.Splatted)
267-
{
268-
Splatted = varAst;
269-
break;
270-
}
271-
}
272-
if (Splatted != null)
273-
{
274-
NewSplattedModification(Splatted);
275-
}
276-
else
277-
{
278-
// The Target Variable is a Parameter and the commandAst is the Target Function
279-
ShouldRename = true;
280-
}
281-
}
259+
ProcessCommandAst(commandAst);
282260
break;
283261
case CommandParameterAst commandParameterAst:
262+
ProcessCommandParameterAst(commandParameterAst);
263+
break;
264+
case VariableExpressionAst variableExpressionAst:
265+
ProcessVariableExpressionAst(variableExpressionAst);
266+
break;
267+
}
268+
}
284269

285-
if (commandParameterAst.ParameterName.ToLower() == OldName.ToLower())
270+
private void ProcessCommandAst(CommandAst commandAst)
271+
{
272+
// Is the Target Variable a Parameter and is this commandAst the target function
273+
if (isParam && commandAst.GetCommandName()?.ToLower() == TargetFunction?.Name.ToLower())
274+
{
275+
// Check to see if this is a splatted call to the target function.
276+
Ast Splatted = null;
277+
foreach (Ast element in commandAst.CommandElements)
278+
{
279+
if (element is VariableExpressionAst varAst && varAst.Splatted)
286280
{
287-
if (commandParameterAst.Extent.StartLineNumber == StartLineNumber &&
288-
commandParameterAst.Extent.StartColumnNumber == StartColumnNumber)
289-
{
290-
ShouldRename = true;
291-
}
281+
Splatted = varAst;
282+
break;
283+
}
284+
}
285+
if (Splatted != null)
286+
{
287+
NewSplattedModification(Splatted);
288+
}
289+
else
290+
{
291+
// The Target Variable is a Parameter and the commandAst is the Target Function
292+
ShouldRename = true;
293+
}
294+
}
295+
}
292296

293-
if (TargetFunction != null && commandParameterAst.Parent is CommandAst commandAst &&
294-
commandAst.GetCommandName().ToLower() == TargetFunction.Name.ToLower() && isParam && ShouldRename)
295-
{
296-
TextChange Change = new()
297-
{
298-
NewText = NewName.Contains("-") ? NewName : "-" + NewName,
299-
StartLine = commandParameterAst.Extent.StartLineNumber - 1,
300-
StartColumn = commandParameterAst.Extent.StartColumnNumber - 1,
301-
EndLine = commandParameterAst.Extent.StartLineNumber - 1,
302-
EndColumn = commandParameterAst.Extent.StartColumnNumber + OldName.Length,
303-
};
304-
Modifications.Add(Change);
305-
}
306-
else
307-
{
308-
ShouldRename = false;
309-
}
297+
private void ProcessVariableExpressionAst(VariableExpressionAst variableExpressionAst)
298+
{
299+
if (variableExpressionAst.VariablePath.UserPath.ToLower() == OldName.ToLower())
300+
{
301+
// Is this the Target Variable
302+
if (variableExpressionAst.Extent.StartColumnNumber == StartColumnNumber &&
303+
variableExpressionAst.Extent.StartLineNumber == StartLineNumber)
304+
{
305+
ShouldRename = true;
306+
TargetVariableAst = variableExpressionAst;
307+
}
308+
// Is this a Command Ast within scope
309+
else if (variableExpressionAst.Parent is CommandAst commandAst)
310+
{
311+
if (WithinTargetsScope(TargetVariableAst, commandAst))
312+
{
313+
ShouldRename = true;
310314
}
311-
break;
312-
case VariableExpressionAst variableExpressionAst:
313-
if (variableExpressionAst.VariablePath.UserPath.ToLower() == OldName.ToLower())
315+
}
316+
// Is this a Variable Assignment thats not within scope
317+
else if (variableExpressionAst.Parent is AssignmentStatementAst assignment &&
318+
assignment.Operator == TokenKind.Equals)
319+
{
320+
if (!WithinTargetsScope(TargetVariableAst, variableExpressionAst))
314321
{
315-
// Is this the Target Variable
316-
if (variableExpressionAst.Extent.StartColumnNumber == StartColumnNumber &&
317-
variableExpressionAst.Extent.StartLineNumber == StartLineNumber)
318-
{
319-
ShouldRename = true;
320-
TargetVariableAst = variableExpressionAst;
321-
}
322-
// Is this a Command Ast within scope
323-
else if (variableExpressionAst.Parent is CommandAst commandAst)
324-
{
325-
if (WithinTargetsScope(TargetVariableAst, commandAst))
326-
{
327-
ShouldRename = true;
328-
}
329-
}
330-
// Is this a Variable Assignment thats not within scope
331-
else if (variableExpressionAst.Parent is AssignmentStatementAst assignment &&
332-
assignment.Operator == TokenKind.Equals)
333-
{
334-
if (!WithinTargetsScope(TargetVariableAst, variableExpressionAst))
335-
{
336-
ShouldRename = false;
337-
}
338-
339-
}
340-
// Else is the variable within scope
341-
else
342-
{
343-
ShouldRename = WithinTargetsScope(TargetVariableAst, variableExpressionAst);
344-
}
345-
if (ShouldRename)
346-
{
347-
// have some modifications to account for the dollar sign prefix powershell uses for variables
348-
TextChange Change = new()
349-
{
350-
NewText = NewName.Contains("$") ? NewName : "$" + NewName,
351-
StartLine = variableExpressionAst.Extent.StartLineNumber - 1,
352-
StartColumn = variableExpressionAst.Extent.StartColumnNumber - 1,
353-
EndLine = variableExpressionAst.Extent.StartLineNumber - 1,
354-
EndColumn = variableExpressionAst.Extent.StartColumnNumber + OldName.Length,
355-
};
356-
// If the variables parent is a parameterAst Add a modification
357-
if (variableExpressionAst.Parent is ParameterAst paramAst && !AliasSet)
358-
{
359-
TextChange aliasChange = NewParameterAliasChange(variableExpressionAst, paramAst);
360-
Modifications.Add(aliasChange);
361-
AliasSet = true;
362-
}
363-
Modifications.Add(Change);
322+
ShouldRename = false;
323+
}
364324

365-
}
325+
}
326+
// Else is the variable within scope
327+
else
328+
{
329+
ShouldRename = WithinTargetsScope(TargetVariableAst, variableExpressionAst);
330+
}
331+
if (ShouldRename)
332+
{
333+
// have some modifications to account for the dollar sign prefix powershell uses for variables
334+
TextChange Change = new()
335+
{
336+
NewText = NewName.Contains("$") ? NewName : "$" + NewName,
337+
StartLine = variableExpressionAst.Extent.StartLineNumber - 1,
338+
StartColumn = variableExpressionAst.Extent.StartColumnNumber - 1,
339+
EndLine = variableExpressionAst.Extent.StartLineNumber - 1,
340+
EndColumn = variableExpressionAst.Extent.StartColumnNumber + OldName.Length,
341+
};
342+
// If the variables parent is a parameterAst Add a modification
343+
if (variableExpressionAst.Parent is ParameterAst paramAst && !AliasSet)
344+
{
345+
TextChange aliasChange = NewParameterAliasChange(variableExpressionAst, paramAst);
346+
Modifications.Add(aliasChange);
347+
AliasSet = true;
366348
}
367-
break;
349+
Modifications.Add(Change);
350+
351+
}
352+
}
353+
}
354+
355+
private void ProcessCommandParameterAst(CommandParameterAst commandParameterAst)
356+
{
357+
if (commandParameterAst.ParameterName.ToLower() == OldName.ToLower())
358+
{
359+
if (commandParameterAst.Extent.StartLineNumber == StartLineNumber &&
360+
commandParameterAst.Extent.StartColumnNumber == StartColumnNumber)
361+
{
362+
ShouldRename = true;
363+
}
364+
365+
if (TargetFunction != null && commandParameterAst.Parent is CommandAst commandAst &&
366+
commandAst.GetCommandName().ToLower() == TargetFunction.Name.ToLower() && isParam && ShouldRename)
367+
{
368+
TextChange Change = new()
369+
{
370+
NewText = NewName.Contains("-") ? NewName : "-" + NewName,
371+
StartLine = commandParameterAst.Extent.StartLineNumber - 1,
372+
StartColumn = commandParameterAst.Extent.StartColumnNumber - 1,
373+
EndLine = commandParameterAst.Extent.StartLineNumber - 1,
374+
EndColumn = commandParameterAst.Extent.StartColumnNumber + OldName.Length,
375+
};
376+
Modifications.Add(Change);
377+
}
378+
else
379+
{
380+
ShouldRename = false;
381+
}
368382
}
369383
}
370384

0 commit comments

Comments
 (0)