Skip to content

Commit f31c1c2

Browse files
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.
1 parent 687026f commit f31c1c2

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/PowerShellEditorServices/Services/Symbols/Vistors/AstOperations.cs

+29
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using Microsoft.Extensions.Logging;
1515
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
1616
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
17+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
1718

1819
namespace Microsoft.PowerShell.EditorServices.Services.Symbols
1920
{
@@ -92,6 +93,34 @@ await executionService.ExecuteDelegateAsync(
9293
(pwsh, _) =>
9394
{
9495
stopwatch.Start();
96+
97+
// If the current runspace is not out of process, then we call TabExpansion2 so
98+
// that we have the ability to issue pipeline stop requests on cancellation.
99+
if (executionService is PsesInternalHost psesInternalHost
100+
&& !psesInternalHost.Runspace.RunspaceIsRemote)
101+
{
102+
IReadOnlyList<CommandCompletion> completionResults = new SynchronousPowerShellTask<CommandCompletion>(
103+
logger,
104+
psesInternalHost,
105+
new PSCommand()
106+
.AddCommand("TabExpansion2")
107+
.AddParameter("ast", scriptAst)
108+
.AddParameter("tokens", currentTokens)
109+
.AddParameter("positionOfCursor", cursorPosition),
110+
executionOptions: null,
111+
cancellationToken)
112+
.ExecuteAndGetResult(cancellationToken);
113+
114+
if (completionResults is { Count: > 0 })
115+
{
116+
commandCompletion = completionResults[0];
117+
}
118+
119+
return;
120+
}
121+
122+
// If the current runspace is out of process, we can't call TabExpansion2
123+
// because the output will be serialized.
95124
commandCompletion = CommandCompletion.CompleteInput(
96125
scriptAst,
97126
currentTokens,

0 commit comments

Comments
 (0)