Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip scope from function references #1990

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)

return _action(new SymbolReference(
SymbolType.Function,
"fn " + CommandHelpers.StripModuleQualification(commandName, out _),
"fn " + VisitorUtils.GetUnqualifiedFunctionName(CommandHelpers.StripModuleQualification(commandName, out _)),
commandName,
commandAst.CommandElements[0].Extent,
commandAst.Extent,
Expand All @@ -64,7 +64,7 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
IScriptExtent nameExtent = VisitorUtils.GetNameExtent(functionDefinitionAst);
return _action(new SymbolReference(
symbolType,
"fn " + functionDefinitionAst.Name,
"fn " + VisitorUtils.GetUnqualifiedFunctionName(functionDefinitionAst.Name),
VisitorUtils.GetFunctionDisplayName(functionDefinitionAst),
nameExtent,
functionDefinitionAst.Extent,
Expand Down
14 changes: 14 additions & 0 deletions src/PowerShellEditorServices/Utility/VisitorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ internal static class VisitorUtils
return PSESSymbols.AstOperations.TryGetInferredValue(expandableStringExpressionAst, out string value) ? value : null;
}

// Strip the qualification, if there is any, so script:my-function is a reference of my-function etc.
internal static string GetUnqualifiedFunctionName(string name)
{
foreach (string scope in new string[] { "private:", "script:", "global:", "local:" })
{
if (name.StartsWith(scope, StringComparison.OrdinalIgnoreCase))
{
return name.Substring(scope.Length);
}
}

return name;
}

// Strip the qualification, if there is any, so $var is a reference of $script:var etc.
internal static string GetUnqualifiedVariableName(VariablePath variablePath)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $Script:ScriptVar2 = 2

"`$Script:ScriptVar2 is $Script:ScriptVar2"

function AFunction {}
function script:AFunction {}

filter AFilter {$_}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,8 +738,9 @@ public void FindsSymbolsInFile()

SymbolReference symbol = symbols.First(i => i.Type == SymbolType.Function);
Assert.Equal("fn AFunction", symbol.Id);
Assert.Equal("function AFunction ()", symbol.Name);
Assert.Equal("function script:AFunction ()", symbol.Name);
Assert.True(symbol.IsDeclaration);
Assert.Equal(2, GetOccurrences(symbol.NameRegion).Count());

symbol = symbols.First(i => i.Id == "fn AFilter");
Assert.Equal("filter AFilter ()", symbol.Name);
Expand Down