Skip to content

Commit f39fd47

Browse files
committed
formatting moved GetFunctionDefByCommandAst to Utilities
1 parent c8a96bc commit f39fd47

File tree

3 files changed

+65
-141
lines changed

3 files changed

+65
-141
lines changed

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

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -196,57 +196,5 @@ public void ProcessNode(Ast node, bool shouldRename)
196196
}
197197
Log.Add($"ShouldRename after proc: {shouldRename}");
198198
}
199-
200-
public static FunctionDefinitionAst GetFunctionDefByCommandAst(string OldName, int StartLineNumber, int StartColumnNumber, Ast ScriptFile)
201-
{
202-
// Look up the targetted object
203-
CommandAst TargetCommand = (CommandAst)ScriptFile.Find(ast =>
204-
{
205-
return ast is CommandAst CommDef &&
206-
CommDef.GetCommandName().ToLower() == OldName.ToLower() &&
207-
CommDef.Extent.StartLineNumber == StartLineNumber &&
208-
CommDef.Extent.StartColumnNumber == StartColumnNumber;
209-
}, true);
210-
211-
string FunctionName = TargetCommand.GetCommandName();
212-
213-
List<FunctionDefinitionAst> FunctionDefinitions = ScriptFile.FindAll(ast =>
214-
{
215-
return ast is FunctionDefinitionAst FuncDef &&
216-
FuncDef.Name.ToLower() == OldName.ToLower() &&
217-
(FuncDef.Extent.EndLineNumber < TargetCommand.Extent.StartLineNumber ||
218-
(FuncDef.Extent.EndColumnNumber <= TargetCommand.Extent.StartColumnNumber &&
219-
FuncDef.Extent.EndLineNumber <= TargetCommand.Extent.StartLineNumber));
220-
}, true).Cast<FunctionDefinitionAst>().ToList();
221-
// return the function def if we only have one match
222-
if (FunctionDefinitions.Count == 1)
223-
{
224-
return FunctionDefinitions[0];
225-
}
226-
227-
// Determine which function definition is the right one
228-
FunctionDefinitionAst CorrectDefinition = null;
229-
for (int i = FunctionDefinitions.Count - 1; i >= 0; i--)
230-
{
231-
FunctionDefinitionAst element = FunctionDefinitions[i];
232-
233-
Ast parent = element.Parent;
234-
// walk backwards till we hit a functiondefinition if any
235-
parent = Utilities.LookForParentOfType(parent,typeof(FunctionDefinitionAst));
236-
237-
// we have hit the global scope of the script file
238-
if (null == parent)
239-
{
240-
CorrectDefinition = element;
241-
break;
242-
}
243-
244-
if (TargetCommand.Parent == parent)
245-
{
246-
CorrectDefinition = (FunctionDefinitionAst)parent;
247-
}
248-
}
249-
return CorrectDefinition;
250-
}
251199
}
252200
}

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

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -459,93 +459,5 @@ internal TextChange NewParameterAliasChange(VariableExpressionAst variableExpres
459459
return aliasChange;
460460
}
461461

462-
public static Ast GetAstNodeByLineAndColumn(string OldName, int StartLineNumber, int StartColumnNumber, Ast ScriptFile)
463-
{
464-
Ast result = null;
465-
// Looking for a function
466-
result = ScriptFile.Find(ast =>
467-
{
468-
return ast.Extent.StartLineNumber == StartLineNumber &&
469-
ast.Extent.StartColumnNumber == StartColumnNumber &&
470-
ast is FunctionDefinitionAst FuncDef &&
471-
FuncDef.Name.ToLower() == OldName.ToLower();
472-
}, true);
473-
// Looking for a a Command call
474-
if (null == result)
475-
{
476-
result = ScriptFile.Find(ast =>
477-
{
478-
return ast.Extent.StartLineNumber == StartLineNumber &&
479-
ast.Extent.StartColumnNumber == StartColumnNumber &&
480-
ast is CommandAst CommDef &&
481-
CommDef.GetCommandName().ToLower() == OldName.ToLower();
482-
}, true);
483-
}
484-
485-
return result;
486-
}
487-
488-
public static FunctionDefinitionAst GetFunctionDefByCommandAst(string OldName, int StartLineNumber, int StartColumnNumber, Ast ScriptFile)
489-
{
490-
// Look up the targetted object
491-
CommandAst TargetCommand = (CommandAst)ScriptFile.Find(ast =>
492-
{
493-
return ast is CommandAst CommDef &&
494-
CommDef.GetCommandName().ToLower() == OldName.ToLower() &&
495-
CommDef.Extent.StartLineNumber == StartLineNumber &&
496-
CommDef.Extent.StartColumnNumber == StartColumnNumber;
497-
}, true);
498-
499-
string FunctionName = TargetCommand.GetCommandName();
500-
501-
List<FunctionDefinitionAst> FunctionDefinitions = ScriptFile.FindAll(ast =>
502-
{
503-
return ast is FunctionDefinitionAst FuncDef &&
504-
FuncDef.Name.ToLower() == OldName.ToLower() &&
505-
(FuncDef.Extent.EndLineNumber < TargetCommand.Extent.StartLineNumber ||
506-
(FuncDef.Extent.EndColumnNumber <= TargetCommand.Extent.StartColumnNumber &&
507-
FuncDef.Extent.EndLineNumber <= TargetCommand.Extent.StartLineNumber));
508-
}, true).Cast<FunctionDefinitionAst>().ToList();
509-
// return the function def if we only have one match
510-
if (FunctionDefinitions.Count == 1)
511-
{
512-
return FunctionDefinitions[0];
513-
}
514-
// Sort function definitions
515-
//FunctionDefinitions.Sort((a, b) =>
516-
//{
517-
// return b.Extent.EndColumnNumber + b.Extent.EndLineNumber -
518-
// a.Extent.EndLineNumber + a.Extent.EndColumnNumber;
519-
//});
520-
// Determine which function definition is the right one
521-
FunctionDefinitionAst CorrectDefinition = null;
522-
for (int i = FunctionDefinitions.Count - 1; i >= 0; i--)
523-
{
524-
FunctionDefinitionAst element = FunctionDefinitions[i];
525-
526-
Ast parent = element.Parent;
527-
// walk backwards till we hit a functiondefinition if any
528-
while (null != parent)
529-
{
530-
if (parent is FunctionDefinitionAst)
531-
{
532-
break;
533-
}
534-
parent = parent.Parent;
535-
}
536-
// we have hit the global scope of the script file
537-
if (null == parent)
538-
{
539-
CorrectDefinition = element;
540-
break;
541-
}
542-
543-
if (TargetCommand.Parent == parent)
544-
{
545-
CorrectDefinition = (FunctionDefinitionAst)parent;
546-
}
547-
}
548-
return CorrectDefinition;
549-
}
550462
}
551463
}

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

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.PowerShell.EditorServices.Refactoring
1111
internal class Utilities
1212
{
1313

14-
public static Ast GetAstNodeByLineAndColumn(int StartLineNumber, int StartColumnNumber, Ast ScriptAst,params Type[] type)
14+
public static Ast GetAstNodeByLineAndColumn(int StartLineNumber, int StartColumnNumber, Ast ScriptAst, params Type[] type)
1515
{
1616
Ast result = null;
1717
result = ScriptAst.Find(ast =>
@@ -42,6 +42,70 @@ public static Ast LookForParentOfType(Ast ast, params Type[] type)
4242
return null;
4343

4444
}
45+
46+
public static FunctionDefinitionAst GetFunctionDefByCommandAst(string OldName, int StartLineNumber, int StartColumnNumber, Ast ScriptFile)
47+
{
48+
// Look up the targetted object
49+
CommandAst TargetCommand = (CommandAst)Utilities.GetAstNodeByLineAndColumn(StartLineNumber, StartColumnNumber, ScriptFile
50+
, typeof(CommandAst));
51+
52+
if (TargetCommand.GetCommandName().ToLower() != OldName.ToLower())
53+
{
54+
TargetCommand = null;
55+
}
56+
57+
string FunctionName = TargetCommand.GetCommandName();
58+
59+
List<FunctionDefinitionAst> FunctionDefinitions = ScriptFile.FindAll(ast =>
60+
{
61+
return ast is FunctionDefinitionAst FuncDef &&
62+
FuncDef.Name.ToLower() == OldName.ToLower() &&
63+
(FuncDef.Extent.EndLineNumber < TargetCommand.Extent.StartLineNumber ||
64+
(FuncDef.Extent.EndColumnNumber <= TargetCommand.Extent.StartColumnNumber &&
65+
FuncDef.Extent.EndLineNumber <= TargetCommand.Extent.StartLineNumber));
66+
}, true).Cast<FunctionDefinitionAst>().ToList();
67+
// return the function def if we only have one match
68+
if (FunctionDefinitions.Count == 1)
69+
{
70+
return FunctionDefinitions[0];
71+
}
72+
// Sort function definitions
73+
//FunctionDefinitions.Sort((a, b) =>
74+
//{
75+
// return b.Extent.EndColumnNumber + b.Extent.EndLineNumber -
76+
// a.Extent.EndLineNumber + a.Extent.EndColumnNumber;
77+
//});
78+
// Determine which function definition is the right one
79+
FunctionDefinitionAst CorrectDefinition = null;
80+
for (int i = FunctionDefinitions.Count - 1; i >= 0; i--)
81+
{
82+
FunctionDefinitionAst element = FunctionDefinitions[i];
83+
84+
Ast parent = element.Parent;
85+
// walk backwards till we hit a functiondefinition if any
86+
while (null != parent)
87+
{
88+
if (parent is FunctionDefinitionAst)
89+
{
90+
break;
91+
}
92+
parent = parent.Parent;
93+
}
94+
// we have hit the global scope of the script file
95+
if (null == parent)
96+
{
97+
CorrectDefinition = element;
98+
break;
99+
}
100+
101+
if (TargetCommand.Parent == parent)
102+
{
103+
CorrectDefinition = (FunctionDefinitionAst)parent;
104+
}
105+
}
106+
return CorrectDefinition;
107+
}
108+
45109
public static Ast GetAst(int StartLineNumber, int StartColumnNumber, Ast Ast)
46110
{
47111
Ast token = null;

0 commit comments

Comments
 (0)