Skip to content

Add cancellation for textDocument/completion, textDocument/codeAction, textDocument/folding #1238

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
Show file tree
Hide file tree
Changes from 2 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 @@ -39,7 +39,7 @@
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="3.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.2" />
<PackageReference Include="OmniSharp.Extensions.LanguageServer" Version="0.15.0" />
<PackageReference Include="OmniSharp.Extensions.LanguageServer" Version="0.15.1-*" />
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public void SetCapability(CodeActionCapability capability)

public async Task<CommandOrCodeActionContainer> Handle(CodeActionParams request, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return new List<CommandOrCodeAction>();
}

// On Windows, VSCode still gives us file URIs like "file:///c%3a/...", so we need to escape them
IReadOnlyDictionary<string, MarkerCorrection> corrections = await _analysisService.GetMostRecentCodeActionsForFileAsync(
_workspaceService.GetFile(request.TextDocument.Uri)).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
internal class CompletionHandler : ICompletionHandler, ICompletionResolveHandler
{
const int DefaultWaitTimeoutMilliseconds = 5000;
private readonly CompletionItem[] s_emptyCompletionResult = Array.Empty<CompletionItem>();
private static readonly CompletionItem[] s_emptyCompletionResult = Array.Empty<CompletionItem>();
private readonly SemaphoreSlim _completionLock = AsyncUtils.CreateSimpleLockingSemaphore();

private readonly ILogger _logger;
private readonly PowerShellContextService _powerShellContextService;
Expand Down Expand Up @@ -67,24 +68,39 @@ public async Task<CompletionList> Handle(CompletionParams request, CancellationT

ScriptFile scriptFile = _workspaceService.GetFile(request.TextDocument.Uri);

CompletionResults completionResults =
await GetCompletionsInFileAsync(
scriptFile,
cursorLine,
cursorColumn).ConfigureAwait(false);
await _completionLock.WaitAsync().ConfigureAwait(false);

CompletionItem[] completionItems = s_emptyCompletionResult;

if (completionResults != null)
try
{
completionItems = new CompletionItem[completionResults.Completions.Length];
for (int i = 0; i < completionItems.Length; i++)
if (cancellationToken.IsCancellationRequested)
{
completionItems[i] = CreateCompletionItem(completionResults.Completions[i], completionResults.ReplacedRange, i + 1);
_logger.LogDebug("Completion request canceled for file: {0}", request.TextDocument.Uri);
return new CompletionList(s_emptyCompletionResult);
}
}

return new CompletionList(completionItems);
CompletionResults completionResults =
await GetCompletionsInFileAsync(
scriptFile,
cursorLine,
cursorColumn).ConfigureAwait(false);

CompletionItem[] completionItems = s_emptyCompletionResult;

if (completionResults != null)
{
completionItems = new CompletionItem[completionResults.Completions.Length];
for (int i = 0; i < completionItems.Length; i++)
{
completionItems[i] = CreateCompletionItem(completionResults.Completions[i], completionResults.ReplacedRange, i + 1);
}
}

return new CompletionList(completionItems);
}
finally
{
_completionLock.Release();
}
}

public bool CanResolve(CompletionItem value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public TextDocumentRegistrationOptions GetRegistrationOptions()

public Task<Container<FoldingRange>> Handle(FoldingRangeRequestParam request, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
Task.FromResult(new Container<FoldingRange>());
}

// TODO Should be using dynamic registrations
if (!_configurationService.CurrentSettings.CodeFolding.Enable) { return null; }

Expand Down