Skip to content

Enable resolution of an alias to its function definition #1662

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

Merged
merged 1 commit into from
Jan 13, 2022
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 @@ -51,8 +51,8 @@ internal static class CommandHelpers

private static readonly ConcurrentDictionary<string, CommandInfo> s_commandInfoCache = new();
private static readonly ConcurrentDictionary<string, string> s_synopsisCache = new();
private static readonly ConcurrentDictionary<string, List<string>> s_cmdletToAliasCache = new(System.StringComparer.OrdinalIgnoreCase);
private static readonly ConcurrentDictionary<string, string> s_aliasToCmdletCache = new(System.StringComparer.OrdinalIgnoreCase);
internal static readonly ConcurrentDictionary<string, List<string>> s_cmdletToAliasCache = new(System.StringComparer.OrdinalIgnoreCase);
internal static readonly ConcurrentDictionary<string, string> s_aliasToCmdletCache = new(System.StringComparer.OrdinalIgnoreCase);

/// <summary>
/// Gets the CommandInfo instance for a command with a particular name.
Expand Down
14 changes: 14 additions & 0 deletions src/PowerShellEditorServices/Services/Symbols/SymbolsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,20 @@ public async Task<SymbolReference> GetDefinitionOfSymbolAsync(
Validate.IsNotNull(nameof(sourceFile), sourceFile);
Validate.IsNotNull(nameof(foundSymbol), foundSymbol);

// If symbol is an alias, resolve it.
(Dictionary<string, List<string>> _, Dictionary<string, string> aliasToCmdlets) =
await CommandHelpers.GetAliasesAsync(_executionService).ConfigureAwait(false);

if (aliasToCmdlets.ContainsKey(foundSymbol.SymbolName))
{
foundSymbol = new SymbolReference(
foundSymbol.SymbolType,
aliasToCmdlets[foundSymbol.SymbolName],
foundSymbol.ScriptRegion,
foundSymbol.FilePath,
foundSymbol.SourceLine);
}

ScriptFile[] referencedFiles =
_workspaceService.ExpandScriptReferences(
sourceFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
File = functionDefinitionAst.Extent.File
};

// We compare to the SymbolName instead of its text because it may have been resolved
// from an alias.
if (symbolRef.SymbolType.Equals(SymbolType.Function) &&
nameExtent.Text.Equals(symbolRef.ScriptRegion.Text, StringComparison.CurrentCultureIgnoreCase))
nameExtent.Text.Equals(symbolRef.SymbolName, StringComparison.CurrentCultureIgnoreCase))
{
this.FoundDeclaration =
new SymbolReference(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.PowerShell.EditorServices.Services.TextDocument;

namespace Microsoft.PowerShell.EditorServices.Test.Shared.Definition
{
public static class FindsFunctionDefinitionOfAliasData
{
public static readonly ScriptRegion SourceDetails = new(
file: TestUtilities.NormalizePath("References/SimpleFile.ps1"),
text: string.Empty,
startLineNumber: 20,
startColumnNumber: 4,
startOffset: 0,
endLineNumber: 0,
endColumnNumber: 0,
endOffset: 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ gci
dir
Write-Host
Get-ChildItem

My-Alias
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility;
using Microsoft.PowerShell.EditorServices.Services.Symbols;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Test.Shared;
Expand Down Expand Up @@ -44,6 +47,8 @@ public SymbolsServiceTests()
public void Dispose()
{
psesHost.StopAsync().GetAwaiter().GetResult();
CommandHelpers.s_cmdletToAliasCache.Clear();
CommandHelpers.s_aliasToCmdletCache.Clear();
GC.SuppressFinalize(this);
}

Expand Down Expand Up @@ -126,14 +131,43 @@ public async Task FindsFunctionDefinition()
}

[Fact]
public async Task FindsReferencesOnFunction()
public async Task FindsFunctionDefinitionForAlias()
{
// TODO: Eventually we should get the alises through the AST instead of relying on them
// being defined in the runspace.
await psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript("Set-Alias -Name My-Alias -Value My-Function"),
CancellationToken.None).ConfigureAwait(true);

SymbolReference definitionResult = await GetDefinition(FindsFunctionDefinitionOfAliasData.SourceDetails).ConfigureAwait(true);
Assert.Equal(1, definitionResult.ScriptRegion.StartLineNumber);
Assert.Equal(10, definitionResult.ScriptRegion.StartColumnNumber);
Assert.Equal("My-Function", definitionResult.SymbolName);
}

[Fact]
public async Task FindsReferencesOnFunction()
{
List<SymbolReference> referencesResult = await GetReferences(FindsReferencesOnFunctionData.SourceDetails).ConfigureAwait(true);
Assert.Equal(3, referencesResult.Count);
Assert.Equal(1, referencesResult[0].ScriptRegion.StartLineNumber);
Assert.Equal(10, referencesResult[0].ScriptRegion.StartColumnNumber);
}

[Fact]
public async Task FindsReferencesOnFunctionIncludingAliases()
{
// TODO: Same as in FindsFunctionDefinitionForAlias.
await psesHost.ExecutePSCommandAsync(
new PSCommand().AddScript("Set-Alias -Name My-Alias -Value My-Function"),
CancellationToken.None).ConfigureAwait(true);

List<SymbolReference> referencesResult = await GetReferences(FindsReferencesOnFunctionData.SourceDetails).ConfigureAwait(true);
Assert.Equal(4, referencesResult.Count);
Assert.Equal(1, referencesResult[0].ScriptRegion.StartLineNumber);
Assert.Equal(10, referencesResult[0].ScriptRegion.StartColumnNumber);
}

[Fact]
public async Task FindsFunctionDefinitionInDotSourceReference()
{
Expand Down