Skip to content

Commit 41bf246

Browse files
Add cancellation for textDocument/completion, textDocument/codeAction, textDocument/folding (#1238)
* Add cancellation for completion,codeaction,folding * codacy * actually return * Needed type changes for Omnisharp * rob feedback and small refactor
1 parent 9af0823 commit 41bf246

14 files changed

+82
-52
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.15.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.15.0" />
52+
<PackageReference Include="OmniSharp.Extensions.DebugAdapter.Server" Version="0.16.0-*" />
5353
</ItemGroup>
5454

5555
<ItemGroup>

src/PowerShellEditorServices/Server/PsesDebugServer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public async Task StartAsync()
6666
_jsonRpcServer = await JsonRpcServer.From(options =>
6767
{
6868
options.Serializer = new DapProtocolSerializer();
69-
options.Reciever = new DapReciever();
69+
options.Receiver = new DapReceiver();
7070
options.LoggerFactory = _loggerFactory;
7171
ILogger logger = options.LoggerFactory.CreateLogger("DebugOptionsStartup");
7272

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,18 @@ public void SetCapability(CodeActionCapability capability)
6060

6161
public async Task<CommandOrCodeActionContainer> Handle(CodeActionParams request, CancellationToken cancellationToken)
6262
{
63+
if (cancellationToken.IsCancellationRequested)
64+
{
65+
return Array.Empty<CommandOrCodeAction>();
66+
}
67+
6368
// On Windows, VSCode still gives us file URIs like "file:///c%3a/...", so we need to escape them
6469
IReadOnlyDictionary<string, MarkerCorrection> corrections = await _analysisService.GetMostRecentCodeActionsForFileAsync(
6570
_workspaceService.GetFile(request.TextDocument.Uri)).ConfigureAwait(false);
6671

6772
if (corrections == null)
6873
{
69-
// TODO: Find out if we can cache this empty value
70-
return new CommandOrCodeActionContainer();
74+
return Array.Empty<CommandOrCodeAction>();
7175
}
7276

7377
var codeActions = new List<CommandOrCodeAction>();
@@ -100,7 +104,7 @@ public async Task<CommandOrCodeActionContainer> Handle(CodeActionParams request,
100104
{
101105
Uri = request.TextDocument.Uri
102106
},
103-
Edits = new Container<TextEdit>(correction.Edits.Select(ScriptRegion.ToTextEdit))
107+
Edits = new TextEditContainer(correction.Edits.Select(ScriptRegion.ToTextEdit))
104108
}))
105109
}
106110
});

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

+27-12
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
2424
internal class CompletionHandler : ICompletionHandler, ICompletionResolveHandler
2525
{
2626
const int DefaultWaitTimeoutMilliseconds = 5000;
27-
private readonly CompletionItem[] s_emptyCompletionResult = Array.Empty<CompletionItem>();
27+
private readonly SemaphoreSlim _completionLock = AsyncUtils.CreateSimpleLockingSemaphore();
2828

2929
private readonly ILogger _logger;
3030
private readonly PowerShellContextService _powerShellContextService;
@@ -67,24 +67,39 @@ public async Task<CompletionList> Handle(CompletionParams request, CancellationT
6767

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

70-
CompletionResults completionResults =
71-
await GetCompletionsInFileAsync(
72-
scriptFile,
73-
cursorLine,
74-
cursorColumn).ConfigureAwait(false);
70+
await _completionLock.WaitAsync().ConfigureAwait(false);
7571

76-
CompletionItem[] completionItems = s_emptyCompletionResult;
77-
78-
if (completionResults != null)
72+
try
7973
{
80-
completionItems = new CompletionItem[completionResults.Completions.Length];
74+
if (cancellationToken.IsCancellationRequested)
75+
{
76+
_logger.LogDebug("Completion request canceled for file: {0}", request.TextDocument.Uri);
77+
return Array.Empty<CompletionItem>();
78+
}
79+
80+
CompletionResults completionResults =
81+
await GetCompletionsInFileAsync(
82+
scriptFile,
83+
cursorLine,
84+
cursorColumn).ConfigureAwait(false);
85+
86+
if (completionResults == null)
87+
{
88+
return Array.Empty<CompletionItem>();
89+
}
90+
91+
CompletionItem[] completionItems = new CompletionItem[completionResults.Completions.Length];
8192
for (int i = 0; i < completionItems.Length; i++)
8293
{
8394
completionItems[i] = CreateCompletionItem(completionResults.Completions[i], completionResults.ReplacedRange, i + 1);
8495
}
85-
}
8696

87-
return new CompletionList(completionItems);
97+
return completionItems;
98+
}
99+
finally
100+
{
101+
_completionLock.Release();
102+
}
88103
}
89104

90105
public bool CanResolve(CompletionItem value)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public DefinitionHandler(
3535
_workspaceService = workspaceService;
3636
}
3737

38-
public TextDocumentRegistrationOptions GetRegistrationOptions()
38+
public DefinitionRegistrationOptions GetRegistrationOptions()
3939
{
40-
return new TextDocumentRegistrationOptions
40+
return new DefinitionRegistrationOptions
4141
{
4242
DocumentSelector = LspUtils.PowerShellDocumentSelector
4343
};

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

+6-9
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,25 @@ internal class DocumentHighlightHandler : IDocumentHighlightHandler
2727

2828
private readonly SymbolsService _symbolsService;
2929

30-
private readonly TextDocumentRegistrationOptions _registrationOptions;
31-
3230
private DocumentHighlightCapability _capability;
3331

3432
public DocumentHighlightHandler(
3533
ILoggerFactory loggerFactory,
3634
WorkspaceService workspaceService,
3735
SymbolsService symbolService)
3836
{
39-
_logger = loggerFactory.CreateLogger<OmniSharp.Extensions.LanguageServer.Protocol.Server.DocumentHighlightHandler>();
37+
_logger = loggerFactory.CreateLogger<DocumentHighlightHandler>();
4038
_workspaceService = workspaceService;
4139
_symbolsService = symbolService;
42-
_registrationOptions = new TextDocumentRegistrationOptions()
43-
{
44-
DocumentSelector = LspUtils.PowerShellDocumentSelector
45-
};
4640
_logger.LogInformation("highlight handler loaded");
4741
}
4842

49-
public TextDocumentRegistrationOptions GetRegistrationOptions()
43+
public DocumentHighlightRegistrationOptions GetRegistrationOptions()
5044
{
51-
return _registrationOptions;
45+
return new DocumentHighlightRegistrationOptions
46+
{
47+
DocumentSelector = LspUtils.PowerShellDocumentSelector
48+
};
5249
}
5350

5451
public Task<DocumentHighlightContainer> Handle(

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal class DocumentSymbolHandler : IDocumentSymbolHandler
3333

3434
public DocumentSymbolHandler(ILoggerFactory factory, ConfigurationService configurationService, WorkspaceService workspaceService)
3535
{
36-
_logger = factory.CreateLogger<FoldingRangeHandler>();
36+
_logger = factory.CreateLogger<DocumentSymbolHandler>();
3737
_workspaceService = workspaceService;
3838
_providers = new IDocumentSymbolProvider[]
3939
{
@@ -43,11 +43,11 @@ public DocumentSymbolHandler(ILoggerFactory factory, ConfigurationService config
4343
};
4444
}
4545

46-
public TextDocumentRegistrationOptions GetRegistrationOptions()
46+
public DocumentSymbolRegistrationOptions GetRegistrationOptions()
4747
{
48-
return new TextDocumentRegistrationOptions
48+
return new DocumentSymbolRegistrationOptions
4949
{
50-
DocumentSelector = LspUtils.PowerShellDocumentSelector,
50+
DocumentSelector = LspUtils.PowerShellDocumentSelector
5151
};
5252
}
5353

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,22 @@ public FoldingRangeHandler(ILoggerFactory factory, ConfigurationService configur
3030
_configurationService = configurationService;
3131
_workspaceService = workspaceService;
3232
}
33-
public TextDocumentRegistrationOptions GetRegistrationOptions()
33+
34+
public FoldingRangeRegistrationOptions GetRegistrationOptions()
3435
{
35-
return new TextDocumentRegistrationOptions()
36+
return new FoldingRangeRegistrationOptions
3637
{
37-
DocumentSelector = LspUtils.PowerShellDocumentSelector,
38+
DocumentSelector = LspUtils.PowerShellDocumentSelector
3839
};
3940
}
4041

4142
public Task<Container<FoldingRange>> Handle(FoldingRangeRequestParam request, CancellationToken cancellationToken)
4243
{
44+
if (cancellationToken.IsCancellationRequested)
45+
{
46+
return Task.FromResult(new Container<FoldingRange>());
47+
}
48+
4349
// TODO Should be using dynamic registrations
4450
if (!_configurationService.CurrentSettings.CodeFolding.Enable) { return null; }
4551

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Extensions.Logging;
99
using Microsoft.PowerShell.EditorServices.Services;
1010
using Microsoft.PowerShell.EditorServices.Utility;
11+
using OmniSharp.Extensions.LanguageServer.Protocol;
1112
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
1213
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
1314
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
@@ -37,9 +38,17 @@ public DocumentFormattingHandlers(
3738
_workspaceService = workspaceService;
3839
}
3940

40-
public TextDocumentRegistrationOptions GetRegistrationOptions()
41+
public DocumentFormattingRegistrationOptions GetRegistrationOptions()
4142
{
42-
return new TextDocumentRegistrationOptions
43+
return new DocumentFormattingRegistrationOptions
44+
{
45+
DocumentSelector = LspUtils.PowerShellDocumentSelector
46+
};
47+
}
48+
49+
DocumentRangeFormattingRegistrationOptions IRegistration<DocumentRangeFormattingRegistrationOptions>.GetRegistrationOptions()
50+
{
51+
return new DocumentRangeFormattingRegistrationOptions
4352
{
4453
DocumentSelector = LspUtils.PowerShellDocumentSelector
4554
};

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ public HoverHandler(
3838
_powerShellContextService = powerShellContextService;
3939
}
4040

41-
public TextDocumentRegistrationOptions GetRegistrationOptions()
41+
public HoverRegistrationOptions GetRegistrationOptions()
4242
{
43-
return new TextDocumentRegistrationOptions
43+
return new HoverRegistrationOptions
4444
{
45-
DocumentSelector = LspUtils.PowerShellDocumentSelector,
45+
DocumentSelector = LspUtils.PowerShellDocumentSelector
4646
};
4747
}
4848

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ReferencesHandler : IReferencesHandler
2222
private readonly ILogger _logger;
2323
private readonly SymbolsService _symbolsService;
2424
private readonly WorkspaceService _workspaceService;
25-
private ReferencesCapability _capability;
25+
private ReferenceCapability _capability;
2626

2727
public ReferencesHandler(ILoggerFactory factory, SymbolsService symbolsService, WorkspaceService workspaceService)
2828
{
@@ -31,9 +31,9 @@ public ReferencesHandler(ILoggerFactory factory, SymbolsService symbolsService,
3131
_workspaceService = workspaceService;
3232
}
3333

34-
public TextDocumentRegistrationOptions GetRegistrationOptions()
34+
public ReferenceRegistrationOptions GetRegistrationOptions()
3535
{
36-
return new TextDocumentRegistrationOptions
36+
return new ReferenceRegistrationOptions
3737
{
3838
DocumentSelector = LspUtils.PowerShellDocumentSelector
3939
};
@@ -72,7 +72,7 @@ public Task<LocationContainer> Handle(ReferenceParams request, CancellationToken
7272
return Task.FromResult(new LocationContainer(locations));
7373
}
7474

75-
public void SetCapability(ReferencesCapability capability)
75+
public void SetCapability(ReferenceCapability capability)
7676
{
7777
_capability = capability;
7878
}

src/PowerShellEditorServices/Services/Workspace/Handlers/WorkspaceSymbolsHandler.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ public WorkspaceSymbolsHandler(ILoggerFactory loggerFactory, SymbolsService symb
3232
_workspaceService = workspace;
3333
}
3434

35-
public object GetRegistrationOptions()
35+
public WorkspaceSymbolRegistrationOptions GetRegistrationOptions()
3636
{
37-
return null;
38-
// throw new NotImplementedException();
37+
return new WorkspaceSymbolRegistrationOptions();
3938
}
4039

41-
public Task<SymbolInformationContainer> Handle(WorkspaceSymbolParams request, CancellationToken cancellationToken)
40+
public Task<Container<SymbolInformation>> Handle(WorkspaceSymbolParams request, CancellationToken cancellationToken)
4241
{
4342
var symbols = new List<SymbolInformation>();
4443

@@ -75,7 +74,7 @@ public Task<SymbolInformationContainer> Handle(WorkspaceSymbolParams request, Ca
7574
}
7675
_logger.LogWarning("Logging in a handler works now.");
7776

78-
return Task.FromResult(new SymbolInformationContainer(symbols));
77+
return Task.FromResult(new Container<SymbolInformation>(symbols));
7978
}
8079

8180
public void SetCapability(WorkspaceSymbolCapability capability)

test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function CanSendWorkspaceSymbolRequest {
124124
}
125125
");
126126

127-
SymbolInformationContainer symbols = await LanguageClient.SendRequest<SymbolInformationContainer>(
127+
Container<SymbolInformation> symbols = await LanguageClient.SendRequest<Container<SymbolInformation>>(
128128
"workspace/symbol",
129129
new WorkspaceSymbolParams
130130
{

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.15.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)