Skip to content

Commit 677c7c5

Browse files
Fix cancellation for completions and add Hover cancellation (#1248)
* better logging for a couple handlers * hover refactor and cancellation * added lock for completionItem/resolve * rev to stable omnisharp version
1 parent b891581 commit 677c7c5

File tree

6 files changed

+60
-26
lines changed

6 files changed

+60
-26
lines changed

src/PowerShellEditorServices/PowerShellEditorServices.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
4040
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="3.1.2" />
4141
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.2" />
42-
<PackageReference Include="OmniSharp.Extensions.LanguageServer" Version="0.16.0-*" />
42+
<PackageReference Include="OmniSharp.Extensions.LanguageServer" Version="0.16.0" />
4343
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1" />
4444
<PackageReference Include="Serilog" Version="2.9.0" />
4545
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
@@ -49,7 +49,7 @@
4949
<PackageReference Include="System.Security.Principal" Version="4.3.0" />
5050
<PackageReference Include="System.Security.Principal.Windows" Version="4.7.0" />
5151
<PackageReference Include="UnixConsoleEcho" Version="0.1.0" />
52-
<PackageReference Include="OmniSharp.Extensions.DebugAdapter.Server" Version="0.16.0-*" />
52+
<PackageReference Include="OmniSharp.Extensions.DebugAdapter.Server" Version="0.16.0" />
5353
</ItemGroup>
5454

5555
<ItemGroup>

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

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public async Task<CommandOrCodeActionContainer> Handle(CodeActionParams request,
6262
{
6363
if (cancellationToken.IsCancellationRequested)
6464
{
65+
_logger.LogDebug("CodeAction request canceled at range: {0}", request.Range);
6566
return Array.Empty<CommandOrCodeAction>();
6667
}
6768

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

+39-13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ internal class CompletionHandler : ICompletionHandler, ICompletionResolveHandler
2525
{
2626
const int DefaultWaitTimeoutMilliseconds = 5000;
2727
private readonly SemaphoreSlim _completionLock = AsyncUtils.CreateSimpleLockingSemaphore();
28+
private readonly SemaphoreSlim _completionResolveLock = AsyncUtils.CreateSimpleLockingSemaphore();
2829

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

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

70-
await _completionLock.WaitAsync().ConfigureAwait(false);
71+
try
72+
{
73+
await _completionLock.WaitAsync(cancellationToken).ConfigureAwait(false);
74+
}
75+
catch (OperationCanceledException)
76+
{
77+
_logger.LogDebug("Completion request canceled for file: {0}", request.TextDocument.Uri);
78+
return Array.Empty<CompletionItem>();
79+
}
7180

7281
try
7382
{
@@ -116,22 +125,39 @@ public async Task<CompletionItem> Handle(CompletionItem request, CancellationTok
116125
return request;
117126
}
118127

119-
// Get the documentation for the function
120-
CommandInfo commandInfo =
121-
await CommandHelpers.GetCommandInfoAsync(
122-
request.Label,
123-
_powerShellContextService).ConfigureAwait(false);
128+
try
129+
{
130+
await _completionResolveLock.WaitAsync(cancellationToken).ConfigureAwait(false);
131+
}
132+
catch (OperationCanceledException)
133+
{
134+
_logger.LogDebug("CompletionItemResolve request canceled for item: {0}", request.Label);
135+
return request;
136+
}
124137

125-
if (commandInfo != null)
138+
try
126139
{
127-
request.Documentation =
128-
await CommandHelpers.GetCommandSynopsisAsync(
129-
commandInfo,
140+
// Get the documentation for the function
141+
CommandInfo commandInfo =
142+
await CommandHelpers.GetCommandInfoAsync(
143+
request.Label,
130144
_powerShellContextService).ConfigureAwait(false);
131-
}
132145

133-
// Send back the updated CompletionItem
134-
return request;
146+
if (commandInfo != null)
147+
{
148+
request.Documentation =
149+
await CommandHelpers.GetCommandSynopsisAsync(
150+
commandInfo,
151+
_powerShellContextService).ConfigureAwait(false);
152+
}
153+
154+
// Send back the updated CompletionItem
155+
return request;
156+
}
157+
finally
158+
{
159+
_completionResolveLock.Release();
160+
}
135161
}
136162

137163
public void SetCapability(CompletionCapability capability)

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

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public Task<Container<FoldingRange>> Handle(FoldingRangeRequestParam request, Ca
4343
{
4444
if (cancellationToken.IsCancellationRequested)
4545
{
46+
_logger.LogDebug("FoldingRange request canceled for file: {0}", request.TextDocument.Uri);
4647
return Task.FromResult(new Container<FoldingRange>());
4748
}
4849

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

+16-10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public HoverRegistrationOptions GetRegistrationOptions()
4848

4949
public async Task<Hover> Handle(HoverParams request, CancellationToken cancellationToken)
5050
{
51+
if (cancellationToken.IsCancellationRequested)
52+
{
53+
_logger.LogDebug("Hover request canceled for file: {0}", request.TextDocument.Uri);
54+
return new Hover();
55+
}
56+
5157
ScriptFile scriptFile = _workspaceService.GetFile(request.TextDocument.Uri);
5258

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

59-
List<MarkedString> symbolInfo = new List<MarkedString>();
60-
Range symbolRange = null;
61-
62-
if (symbolDetails != null)
65+
if (symbolDetails == null)
6366
{
64-
symbolInfo.Add(new MarkedString("PowerShell", symbolDetails.DisplayString));
67+
return new Hover();
68+
}
6569

66-
if (!string.IsNullOrEmpty(symbolDetails.Documentation))
67-
{
68-
symbolInfo.Add(new MarkedString("markdown", symbolDetails.Documentation));
69-
}
70+
List<MarkedString> symbolInfo = new List<MarkedString>();
71+
symbolInfo.Add(new MarkedString("PowerShell", symbolDetails.DisplayString));
7072

71-
symbolRange = GetRangeFromScriptRegion(symbolDetails.SymbolReference.ScriptRegion);
73+
if (!string.IsNullOrEmpty(symbolDetails.Documentation))
74+
{
75+
symbolInfo.Add(new MarkedString("markdown", symbolDetails.Documentation));
7276
}
7377

78+
Range symbolRange = GetRangeFromScriptRegion(symbolDetails.SymbolReference.ScriptRegion);
79+
7480
return new Hover
7581
{
7682
Contents = new MarkedStringsOrMarkupContent(symbolInfo),

test/PowerShellEditorServices.Test.E2E/PowerShellEditorServices.Test.E2E.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.2" />
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
1212
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
13-
<PackageReference Include="OmniSharp.Extensions.LanguageClient" Version="0.16.0-*" />
13+
<PackageReference Include="OmniSharp.Extensions.LanguageClient" Version="0.16.0" />
1414
<PackageReference Include="xunit" Version="2.4.0" />
1515
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
1616
</ItemGroup>

0 commit comments

Comments
 (0)