Skip to content

Commit 4e651e0

Browse files
Count ${Function:My-Function} as a function reference (#1993)
1 parent a13eff2 commit 4e651e0

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

src/PowerShellEditorServices/Services/Symbols/Visitors/SymbolVisitor.cs

+15
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,21 @@ public override AstVisitAction VisitVariableExpression(VariableExpressionAst var
9595
return AstVisitAction.Continue;
9696
}
9797

98+
// Count $Function:MyFunction as function references.
99+
if (variableExpressionAst.VariablePath.IsDriveQualified
100+
&& variableExpressionAst.VariablePath.DriveName.Equals("Function", StringComparison.OrdinalIgnoreCase))
101+
{
102+
return _action(new SymbolReference(
103+
SymbolType.Function,
104+
"fn " + VisitorUtils.GetUnqualifiedVariableName(variableExpressionAst.VariablePath),
105+
"$" + variableExpressionAst.VariablePath.UserPath,
106+
variableExpressionAst.Extent,
107+
variableExpressionAst.Extent,
108+
_file,
109+
false
110+
));
111+
}
112+
98113
// TODO: Consider tracking unscoped variable references only when they're declared within
99114
// the same function definition.
100115
return _action(new SymbolReference(

test/PowerShellEditorServices.Test.Shared/References/SimpleFile.ps1

+2
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ Write-Host
1818
Get-ChildItem
1919

2020
My-Alias
21+
22+
Invoke-Command -ScriptBlock ${Function:My-Function}

test/PowerShellEditorServices.Test/Language/SymbolsServiceTests.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ public async Task FindsReferencesOnFunction()
223223
Assert.Equal("My-Function", i.Name);
224224
Assert.Equal(SymbolType.Function, i.Type);
225225
Assert.False(i.IsDeclaration);
226+
},
227+
(i) =>
228+
{
229+
Assert.Equal("fn My-Function", i.Id);
230+
Assert.Equal("$Function:My-Function", i.Name);
231+
Assert.Equal(SymbolType.Function, i.Type);
232+
Assert.False(i.IsDeclaration);
226233
});
227234
}
228235

@@ -246,7 +253,8 @@ await psesHost.ExecutePSCommandAsync(
246253
{
247254
AssertIsRegion(i.NameRegion, 20, 1, 20, 9);
248255
Assert.Equal("fn My-Alias", i.Id);
249-
});
256+
},
257+
(i) => AssertIsRegion(i.NameRegion, 22, 29, 22, 52));
250258
}
251259

252260
[Fact]
@@ -324,6 +332,13 @@ public void FindsOccurrencesOnFunction()
324332
Assert.Equal("fn My-Function", i.Id);
325333
Assert.Equal(SymbolType.Function, i.Type);
326334
Assert.False(i.IsDeclaration);
335+
},
336+
(i) =>
337+
{
338+
Assert.Equal("fn My-Function", i.Id);
339+
Assert.Equal("$Function:My-Function", i.Name);
340+
Assert.Equal(SymbolType.Function, i.Type);
341+
Assert.False(i.IsDeclaration);
327342
});
328343
}
329344

0 commit comments

Comments
 (0)