Skip to content

Commit 912392e

Browse files
committed
Replace 5 second cancellation token with handler's token
1 parent 7fdef20 commit 912392e

File tree

2 files changed

+17
-22
lines changed

2 files changed

+17
-22
lines changed

src/PowerShellEditorServices/Services/TextDocument/Handlers/CompletionHandler.cs

+14-21
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
2525
// TODO: Use ABCs.
2626
internal class PsesCompletionHandler : ICompletionHandler, ICompletionResolveHandler
2727
{
28-
private const int DefaultWaitTimeoutMilliseconds = 5000;
2928
private readonly ILogger _logger;
3029
private readonly IRunspaceContext _runspaceContext;
3130
private readonly IInternalPowerShellExecutionService _executionService;
@@ -60,14 +59,11 @@ public async Task<CompletionList> Handle(CompletionParams request, CancellationT
6059
int cursorColumn = request.Position.Character + 1;
6160

6261
ScriptFile scriptFile = _workspaceService.GetFile(request.TextDocument.Uri);
63-
64-
if (cancellationToken.IsCancellationRequested)
65-
{
66-
_logger.LogDebug("Completion request canceled for file: {0}", request.TextDocument.Uri);
67-
return Array.Empty<CompletionItem>();
68-
}
69-
70-
IEnumerable<CompletionItem> completionResults = await GetCompletionsInFileAsync(scriptFile, cursorLine, cursorColumn).ConfigureAwait(false);
62+
IEnumerable<CompletionItem> completionResults = await GetCompletionsInFileAsync(
63+
scriptFile,
64+
cursorLine,
65+
cursorColumn,
66+
cancellationToken).ConfigureAwait(false);
7167

7268
return new CompletionList(completionResults);
7369
}
@@ -133,21 +129,18 @@ public void SetCapability(CompletionCapability capability, ClientCapabilities cl
133129
public async Task<IEnumerable<CompletionItem>> GetCompletionsInFileAsync(
134130
ScriptFile scriptFile,
135131
int lineNumber,
136-
int columnNumber)
132+
int columnNumber,
133+
CancellationToken cancellationToken)
137134
{
138135
Validate.IsNotNull(nameof(scriptFile), scriptFile);
139136

140-
CommandCompletion result = null;
141-
using (CancellationTokenSource cts = new(DefaultWaitTimeoutMilliseconds))
142-
{
143-
result = await AstOperations.GetCompletionsAsync(
144-
scriptFile.ScriptAst,
145-
scriptFile.ScriptTokens,
146-
scriptFile.GetOffsetAtPosition(lineNumber, columnNumber),
147-
_executionService,
148-
_logger,
149-
cts.Token).ConfigureAwait(false);
150-
}
137+
CommandCompletion result = await AstOperations.GetCompletionsAsync(
138+
scriptFile.ScriptAst,
139+
scriptFile.ScriptTokens,
140+
scriptFile.GetOffsetAtPosition(lineNumber, columnNumber),
141+
_executionService,
142+
_logger,
143+
cancellationToken).ConfigureAwait(false);
151144

152145
// Only calculate the replacement range if there are completions.
153146
BufferRange replacedRange = new(0, 0, 0, 0);

test/PowerShellEditorServices.Test/Language/CompletionHandlerTests.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Threading;
78
using System.Threading.Tasks;
89
using Microsoft.Extensions.Logging.Abstractions;
910
using Microsoft.PowerShell.EditorServices.Handlers;
@@ -45,7 +46,8 @@ private Task<IEnumerable<CompletionItem>> GetCompletionResultsAsync(ScriptRegion
4546
return completionHandler.GetCompletionsInFileAsync(
4647
GetScriptFile(scriptRegion),
4748
scriptRegion.StartLineNumber,
48-
scriptRegion.StartColumnNumber);
49+
scriptRegion.StartColumnNumber,
50+
CancellationToken.None);
4951
}
5052

5153
[Fact]

0 commit comments

Comments
 (0)