Skip to content
Prev Previous commit
Next Next commit
Make completion requests cancellable
That way any completion results from custom argument completers can be
cancelled if they take too long and the user keeps typing.
  • Loading branch information
SeeminglyScience committed May 17, 2022
commit f31c1c242fb3da14e310f65505b8db6ad3125004
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;

namespace Microsoft.PowerShell.EditorServices.Services.Symbols
{
Expand Down Expand Up @@ -92,6 +93,34 @@ await executionService.ExecuteDelegateAsync(
(pwsh, _) =>
{
stopwatch.Start();

// If the current runspace is not out of process, then we call TabExpansion2 so
// that we have the ability to issue pipeline stop requests on cancellation.
Comment on lines +92 to +93
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳

if (executionService is PsesInternalHost psesInternalHost
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof, I would happily remove the IExecutionService interface as it makes us do things like this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah either the sync task itself needs to reference IExecutionService or we need to strip it out. Or maybe there should be another interface that has state or w/e.

Maybe we hold off on tackling that until we get a source generator that handles the interface definitions for us? 🤷

&& !psesInternalHost.Runspace.RunspaceIsRemote)
{
IReadOnlyList<CommandCompletion> completionResults = new SynchronousPowerShellTask<CommandCompletion>(
logger,
psesInternalHost,
new PSCommand()
.AddCommand("TabExpansion2")
.AddParameter("ast", scriptAst)
.AddParameter("tokens", currentTokens)
.AddParameter("positionOfCursor", cursorPosition),
executionOptions: null,
cancellationToken)
.ExecuteAndGetResult(cancellationToken);

if (completionResults is { Count: > 0 })
{
commandCompletion = completionResults[0];
}

return;
}

// If the current runspace is out of process, we can't call TabExpansion2
// because the output will be serialized.
commandCompletion = CommandCompletion.CompleteInput(
scriptAst,
currentTokens,
Expand Down