Skip to content

Commit f9ff873

Browse files
author
Rob Holt
committed
Add tests, fix bugs
1 parent 35f95f3 commit f9ff873

File tree

6 files changed

+105
-10
lines changed

6 files changed

+105
-10
lines changed

src/PowerShellEditorServices.Engine/Hosting/EditorServicesHost.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Threading.Tasks;
1515
using Microsoft.Extensions.DependencyInjection;
1616
using Microsoft.Extensions.Logging;
17+
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
1718
using Serilog;
1819

1920
namespace Microsoft.PowerShell.EditorServices.Engine
@@ -225,6 +226,7 @@ public void StartLanguageService(
225226

226227
_logger.LogInformation($"LSP NamedPipe: {config.InOutPipeName}\nLSP OutPipe: {config.OutPipeName}");
227228

229+
_serviceCollection.AddSingleton<TextDocumentChangeRegistrationOptions>();
228230
_serviceCollection.AddSingleton<WorkspaceService>();
229231
_serviceCollection.AddSingleton<SymbolsService>();
230232
_serviceCollection.AddSingleton<ConfigurationService>();

src/PowerShellEditorServices.Engine/LanguageServer/OmnisharpLanguageServer.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Security.AccessControl;
99
using OmniSharp.Extensions.LanguageServer.Server;
1010
using PowerShellEditorServices.Engine.Services.Handlers;
11+
using Microsoft.PowerShell.EditorServices.TextDocument;
1112

1213
namespace Microsoft.PowerShell.EditorServices.Engine
1314
{
@@ -56,18 +57,26 @@ public async Task StartAsync()
5657
_configuration.OutNamedPipeName,
5758
out NamedPipeServerStream outNamedPipe);
5859

60+
ILogger logger = options.LoggerFactory.CreateLogger("OptionsStartup");
61+
62+
logger.LogInformation("Waiting for connection");
5963
namedPipe.WaitForConnection();
6064
if (outNamedPipe != null)
6165
{
6266
outNamedPipe.WaitForConnection();
6367
}
6468

69+
logger.LogInformation("Connected");
70+
6571
options.Input = namedPipe;
6672
options.Output = outNamedPipe ?? namedPipe;
6773

6874
options.LoggerFactory = _configuration.LoggerFactory;
6975
options.MinimumLogLevel = _configuration.MinimumLogLevel;
7076
options.Services = _configuration.Services;
77+
78+
logger.LogInformation("Adding handlers");
79+
7180
options
7281
.WithHandler<WorkspaceSymbolsHandler>()
7382
.WithHandler<TextDocumentHandler>()
@@ -77,7 +86,10 @@ public async Task StartAsync()
7786
.WithHandler<DocumentFormattingHandler>()
7887
.WithHandler<DocumentRangeFormattingHandler>()
7988
.WithHandler<ReferencesHandler>()
80-
.WithHandler<DocumentSymbolHandler>();
89+
.WithHandler<DocumentSymbolHandler>()
90+
.WithHandler<DocumentHighlightHandler>();
91+
92+
logger.LogInformation("Handlers added");
8193
});
8294

8395
_serverStart.SetResult(true);

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using Microsoft.Extensions.Logging;
77
using Microsoft.PowerShell.EditorServices.Symbols;
8+
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
89
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
910
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
1011
using PowerShellEditorServices.Engine.Utility;
@@ -14,7 +15,7 @@
1415

1516
namespace Microsoft.PowerShell.EditorServices.TextDocument
1617
{
17-
public class PsesDocumentHighlightHandler : DocumentHighlightHandler
18+
public class DocumentHighlightHandler : IDocumentHighlightHandler
1819
{
1920
private static readonly DocumentHighlightContainer s_emptyHighlightContainer = new DocumentHighlightContainer();
2021

@@ -24,28 +25,40 @@ public class PsesDocumentHighlightHandler : DocumentHighlightHandler
2425

2526
private readonly SymbolsService _symbolsService;
2627

27-
public PsesDocumentHighlightHandler(
28+
private readonly TextDocumentRegistrationOptions _registrationOptions;
29+
30+
private DocumentHighlightCapability _capability;
31+
32+
public DocumentHighlightHandler(
2833
ILoggerFactory loggerFactory,
2934
WorkspaceService workspaceService,
30-
SymbolsService symbolService,
31-
TextDocumentRegistrationOptions registrationOptions)
32-
: base(registrationOptions)
35+
SymbolsService symbolService)
3336
{
34-
_logger = loggerFactory.CreateLogger<DocumentHighlightHandler>();
37+
_logger = loggerFactory.CreateLogger<OmniSharp.Extensions.LanguageServer.Protocol.Server.DocumentHighlightHandler>();
3538
_workspaceService = workspaceService;
3639
_symbolsService = symbolService;
40+
_registrationOptions = new TextDocumentRegistrationOptions()
41+
{
42+
DocumentSelector = new DocumentSelector(new DocumentFilter() { Pattern = "**/*.ps*1" } )
43+
};
44+
_logger.LogInformation("highlight handler loaded");
3745
}
3846

39-
public override Task<DocumentHighlightContainer> Handle(
47+
public TextDocumentRegistrationOptions GetRegistrationOptions()
48+
{
49+
return _registrationOptions;
50+
}
51+
52+
public Task<DocumentHighlightContainer> Handle(
4053
DocumentHighlightParams request,
4154
CancellationToken cancellationToken)
4255
{
4356
ScriptFile scriptFile = _workspaceService.GetFile(PathUtils.FromUri(request.TextDocument.Uri));
4457

4558
IReadOnlyList<SymbolReference> symbolOccurrences = _symbolsService.FindOccurrencesInFile(
4659
scriptFile,
47-
(int)(request.Position.Line + 1),
48-
(int)(request.Position.Character + 1));
60+
(int)request.Position.Line,
61+
(int)request.Position.Character);
4962

5063
if (symbolOccurrences == null)
5164
{
@@ -64,5 +77,10 @@ public override Task<DocumentHighlightContainer> Handle(
6477

6578
return Task.FromResult(new DocumentHighlightContainer(highlights));
6679
}
80+
81+
public void SetCapability(DocumentHighlightCapability capability)
82+
{
83+
_capability = capability;
84+
}
6785
}
6886
}

test/Pester/EditorServices.Integration.Tests.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,34 @@ Get-Bar
299299
$response.Result[1].range.end.character | Should -BeExactly 7
300300
}
301301

302+
It "Can handle a textDocument/documentHighlight request" {
303+
$filePath = New-TestFile -Script @'
304+
Write-Host 'Hello!'
305+
306+
Write-Host 'Goodbye'
307+
'@
308+
309+
$documentHighlightParams = @{
310+
Client = $client
311+
Uri = ([uri]::new($filePath).AbsoluteUri)
312+
LineNumber = 3
313+
CharacterNumber = 1
314+
}
315+
$request = Send-LspDocumentHighlightRequest @documentHighlightParams
316+
317+
$response = Get-LspResponse -Client $client -Id $request.Id
318+
319+
$response.Result.Count | Should -BeExactly 2
320+
$response.Result[0].Range.Start.Line | Should -BeExactly 0
321+
$response.Result[0].Range.Start.Character | Should -BeExactly 0
322+
$response.Result[0].Range.End.Line | Should -BeExactly 0
323+
$response.Result[0].Range.End.Character | Should -BeExactly 10
324+
$response.Result[1].Range.Start.Line | Should -BeExactly 2
325+
$response.Result[1].Range.Start.Character | Should -BeExactly 0
326+
$response.Result[1].Range.End.Line | Should -BeExactly 2
327+
$response.Result[1].Range.End.Character | Should -BeExactly 10
328+
}
329+
302330
# This test MUST be last
303331
It "Shuts down the process properly" {
304332
$request = Send-LspShutdownRequest -Client $client

tools/PsesPsClient/PsesPsClient.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ FunctionsToExport = @(
7979
'Send-LspFormattingRequest',
8080
'Send-LspRangeFormattingRequest',
8181
'Send-LspDocumentSymbolRequest',
82+
'Send-LspDocumentHighlightRequest',
8283
'Send-LspReferencesRequest',
8384
'Send-LspShutdownRequest',
8485
'Get-LspNotification',

tools/PsesPsClient/PsesPsClient.psm1

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,40 @@ function Send-LspReferencesRequest
453453
return Send-LspRequest -Client $Client -Method 'textDocument/references' -Parameters $params
454454
}
455455

456+
function Send-LspDocumentHighlightRequest
457+
{
458+
[OutputType([PsesPsClient.LspRequest])]
459+
param(
460+
[Parameter(Position = 0, Mandatory)]
461+
[PsesPsClient.PsesLspClient]
462+
$Client,
463+
464+
[Parameter(Position = 1, Mandatory)]
465+
[string]
466+
$Uri,
467+
468+
[Parameter(Mandatory)]
469+
[int]
470+
$LineNumber,
471+
472+
[Parameter(Mandatory)]
473+
[int]
474+
$CharacterNumber
475+
)
476+
477+
$documentHighlightParams = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.TextDocumentPositionParams]@{
478+
TextDocument = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.TextDocumentIdentifier]@{
479+
Uri = $Uri
480+
}
481+
Position = [Microsoft.PowerShell.EditorServices.Protocol.LanguageServer.Position]@{
482+
Line = $LineNumber
483+
Character = $CharacterNumber
484+
}
485+
}
486+
487+
return Send-LspRequest -Client $Client -Method 'textDocument/documentHighlight' -Parameters $documentHighlightParams
488+
}
489+
456490
function Send-LspShutdownRequest
457491
{
458492
[OutputType([PsesPsClient.LspRequest])]

0 commit comments

Comments
 (0)