Skip to content

Commit 3c3d96c

Browse files
committed
extracted method of LookForParentOfType from GetFuncDecFromCommAst
1 parent b4a5647 commit 3c3d96c

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public IterativeFunctionRename(string OldName, string NewName, int StartLineNumb
3131
this.StartColumnNumber = StartColumnNumber;
3232
this.ScriptAst = ScriptAst;
3333

34-
Ast Node = FunctionRename.GetAstNodeByLineAndColumn(OldName, StartLineNumber, StartColumnNumber, ScriptAst);
34+
Ast Node = GetAstNodeByLineAndColumn(OldName, StartLineNumber, StartColumnNumber, ScriptAst);
3535
if (Node != null)
3636
{
3737
if (Node is FunctionDefinitionAst FuncDef)
@@ -218,7 +218,6 @@ ast is CommandAst CommDef &&
218218
CommDef.GetCommandName().ToLower() == OldName.ToLower();
219219
}, true);
220220
}
221-
222221
return result;
223222
}
224223

@@ -248,12 +247,7 @@ public static FunctionDefinitionAst GetFunctionDefByCommandAst(string OldName, i
248247
{
249248
return FunctionDefinitions[0];
250249
}
251-
// Sort function definitions
252-
//FunctionDefinitions.Sort((a, b) =>
253-
//{
254-
// return b.Extent.EndColumnNumber + b.Extent.EndLineNumber -
255-
// a.Extent.EndLineNumber + a.Extent.EndColumnNumber;
256-
//});
250+
257251
// Determine which function definition is the right one
258252
FunctionDefinitionAst CorrectDefinition = null;
259253
for (int i = FunctionDefinitions.Count - 1; i >= 0; i--)
@@ -262,14 +256,8 @@ public static FunctionDefinitionAst GetFunctionDefByCommandAst(string OldName, i
262256

263257
Ast parent = element.Parent;
264258
// walk backwards till we hit a functiondefinition if any
265-
while (null != parent)
266-
{
267-
if (parent is FunctionDefinitionAst)
268-
{
269-
break;
270-
}
271-
parent = parent.Parent;
272-
}
259+
parent = Utilities.LookForParentOfType<FunctionDefinitionAst>(parent);
260+
273261
// we have hit the global scope of the script file
274262
if (null == parent)
275263
{

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ namespace Microsoft.PowerShell.EditorServices.Refactoring
99
{
1010
internal class Utilities
1111
{
12+
13+
public static Ast LookForParentOfType<T>(Ast ast)
14+
{
15+
Ast parent = ast.Parent;
16+
// walk backwards till we hit a parent of the specified type or return null
17+
while (null != parent)
18+
{
19+
if (typeof(T) == parent.GetType())
20+
{
21+
return parent;
22+
}
23+
parent = parent.Parent;
24+
}
25+
return null;
26+
27+
}
1228
public static Ast GetAst(int StartLineNumber, int StartColumnNumber, Ast Ast)
1329
{
1430
Ast token = null;

0 commit comments

Comments
 (0)