Skip to content

Fix cancellation for completions and add Hover cancellation #1248

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
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/PowerShellEditorServices/PowerShellEditorServices.csproj
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.16.0-*" />
<PackageReference Include="OmniSharp.Extensions.LanguageServer" Version="0.16.0" />
<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 All @@ -49,7 +49,7 @@
<PackageReference Include="System.Security.Principal" Version="4.3.0" />
<PackageReference Include="System.Security.Principal.Windows" Version="4.7.0" />
<PackageReference Include="UnixConsoleEcho" Version="0.1.0" />
<PackageReference Include="OmniSharp.Extensions.DebugAdapter.Server" Version="0.16.0-*" />
<PackageReference Include="OmniSharp.Extensions.DebugAdapter.Server" Version="0.16.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public async Task<CommandOrCodeActionContainer> Handle(CodeActionParams request,
{
if (cancellationToken.IsCancellationRequested)
{
_logger.LogDebug("CodeAction request canceled at range: {0}", request.Range);
return Array.Empty<CommandOrCodeAction>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal class CompletionHandler : ICompletionHandler, ICompletionResolveHandler
{
const int DefaultWaitTimeoutMilliseconds = 5000;
private readonly SemaphoreSlim _completionLock = AsyncUtils.CreateSimpleLockingSemaphore();
private readonly SemaphoreSlim _completionResolveLock = AsyncUtils.CreateSimpleLockingSemaphore();

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

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

await _completionLock.WaitAsync().ConfigureAwait(false);
try
{
await _completionLock.WaitAsync(cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
_logger.LogDebug("Completion request canceled for file: {0}", request.TextDocument.Uri);
return Array.Empty<CompletionItem>();
}

try
{
Expand Down Expand Up @@ -116,22 +125,39 @@ public async Task<CompletionItem> Handle(CompletionItem request, CancellationTok
return request;
}

// Get the documentation for the function
CommandInfo commandInfo =
await CommandHelpers.GetCommandInfoAsync(
request.Label,
_powerShellContextService).ConfigureAwait(false);
try
{
await _completionResolveLock.WaitAsync(cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
_logger.LogDebug("CompletionItemResolve request canceled for item: {0}", request.Label);
return request;
}

if (commandInfo != null)
try
{
request.Documentation =
await CommandHelpers.GetCommandSynopsisAsync(
commandInfo,
// Get the documentation for the function
CommandInfo commandInfo =
await CommandHelpers.GetCommandInfoAsync(
request.Label,
_powerShellContextService).ConfigureAwait(false);
}

// Send back the updated CompletionItem
return request;
if (commandInfo != null)
{
request.Documentation =
await CommandHelpers.GetCommandSynopsisAsync(
commandInfo,
_powerShellContextService).ConfigureAwait(false);
}

// Send back the updated CompletionItem
return request;
}
finally
{
_completionResolveLock.Release();
}
}

public void SetCapability(CompletionCapability capability)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public Task<Container<FoldingRange>> Handle(FoldingRangeRequestParam request, Ca
{
if (cancellationToken.IsCancellationRequested)
{
_logger.LogDebug("FoldingRange request canceled for file: {0}", request.TextDocument.Uri);
return Task.FromResult(new Container<FoldingRange>());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public HoverRegistrationOptions GetRegistrationOptions()

public async Task<Hover> Handle(HoverParams request, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
_logger.LogDebug("Hover request canceled for file: {0}", request.TextDocument.Uri);
return new Hover();
}

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

SymbolDetails symbolDetails =
Expand All @@ -56,21 +62,21 @@ await _symbolsService.FindSymbolDetailsAtLocationAsync(
(int) request.Position.Line + 1,
(int) request.Position.Character + 1).ConfigureAwait(false);

List<MarkedString> symbolInfo = new List<MarkedString>();
Range symbolRange = null;

if (symbolDetails != null)
if (symbolDetails == null)
{
symbolInfo.Add(new MarkedString("PowerShell", symbolDetails.DisplayString));
return new Hover();
}

if (!string.IsNullOrEmpty(symbolDetails.Documentation))
{
symbolInfo.Add(new MarkedString("markdown", symbolDetails.Documentation));
}
List<MarkedString> symbolInfo = new List<MarkedString>();
symbolInfo.Add(new MarkedString("PowerShell", symbolDetails.DisplayString));

symbolRange = GetRangeFromScriptRegion(symbolDetails.SymbolReference.ScriptRegion);
if (!string.IsNullOrEmpty(symbolDetails.Documentation))
{
symbolInfo.Add(new MarkedString("markdown", symbolDetails.Documentation));
}

Range symbolRange = GetRangeFromScriptRegion(symbolDetails.SymbolReference.ScriptRegion);

return new Hover
{
Contents = new MarkedStringsOrMarkupContent(symbolInfo),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="OmniSharp.Extensions.LanguageClient" Version="0.16.0-*" />
<PackageReference Include="OmniSharp.Extensions.LanguageClient" Version="0.16.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>
Expand Down