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

Update ExpandAliasHandler to use latest PowerShell parser #2108

Open
2 tasks done
andyleejordan opened this issue Dec 5, 2023 · 3 comments
Open
2 tasks done

Update ExpandAliasHandler to use latest PowerShell parser #2108

andyleejordan opened this issue Dec 5, 2023 · 3 comments
Labels
Area-Script Analysis Issue-Enhancement A feature request (enhancement).

Comments

@andyleejordan
Copy link
Member

andyleejordan commented Dec 5, 2023

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all issues to ensure it has not already been requested.

Summary

In the following code we (repeatedly) define an internal PowerShell script that uses the original parser to expand the alias.

public async Task<ExpandAliasResult> Handle(ExpandAliasParams request, CancellationToken cancellationToken)
{
const string script = @"
function __Expand-Alias {
[System.Diagnostics.DebuggerHidden()]
param($targetScript)
[ref]$errors=$null
$tokens = [System.Management.Automation.PsParser]::Tokenize($targetScript, $errors).Where({$_.type -eq 'command'}) |
Sort-Object Start -Descending
foreach ($token in $tokens) {
$definition=(Get-Command ('`'+$token.Content) -CommandType Alias -ErrorAction SilentlyContinue).Definition
if($definition) {
$lhs=$targetScript.Substring(0, $token.Start)
$rhs=$targetScript.Substring($token.Start + $token.Length)
$targetScript=$lhs + $definition + $rhs
}
}
$targetScript
}";
// TODO: Refactor to not rerun the function definition every time.
PSCommand psCommand = new();
psCommand
.AddScript(script)
.AddStatement()
.AddCommand("__Expand-Alias")
.AddArgument(request.Text);
System.Collections.Generic.IReadOnlyList<string> result = await _executionService.ExecutePSCommandAsync<string>(psCommand, cancellationToken).ConfigureAwait(false);
return new ExpandAliasResult
{
Text = result[0]
};
}

Proposed Design

This should be updated to use the new parser directly from the PowerShell APIs in C# and return a proper edit (potentially deprecating the need for an entirely separate custom request).

@andyleejordan andyleejordan added Issue-Enhancement A feature request (enhancement). Needs: Triage Maintainer attention needed! labels Dec 5, 2023
@andyleejordan
Copy link
Member Author

FYI @SeeminglyScience

@andyleejordan
Copy link
Member Author

andyleejordan commented Dec 5, 2023

Actually, are we certain that useLocalScope is defaulting to true? I was looking at the code and that doesn't appear to be the case. The second argument is isScript.

@SydneyhSmith SydneyhSmith added Area-Script Analysis and removed Needs: Triage Maintainer attention needed! labels Feb 22, 2024
@andyleejordan
Copy link
Member Author

Related PowerShell/vscode-powershell#338

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Script Analysis Issue-Enhancement A feature request (enhancement).
Projects
None yet
Development

No branches or pull requests

2 participants