Skip to content

Commit fc244ba

Browse files
Adds a test for running Language Server. Fixes a small issue with initialization
1 parent 3638384 commit fc244ba

File tree

9 files changed

+90
-16
lines changed

9 files changed

+90
-16
lines changed

src/Protocol/Document/Server/IPrepareRenameHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ namespace OmniSharp.Extensions.LanguageServer.Protocol.Server
88
{
99
using static DocumentNames;
1010
[Serial, Method(PrepareRename)]
11-
public interface IPrepareRenameHandler : IJsonRpcRequestHandler<PrepareRenameParams, RangeOrPlaceholderRange>, ICapability<RenameCapability> { }
11+
public interface IPrepareRenameHandler : IJsonRpcRequestHandler<PrepareRenameParams, RangeOrPlaceholderRange>, IRegistration<object>, ICapability<RenameCapability> { }
1212
}

src/Protocol/Workspace/Client/IWorkspaceFoldersHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
namespace OmniSharp.Extensions.LanguageServer.Protocol.Client
77
{
8-
[Parallel, Method(WorkspaceNames.ApplyEdit)]
8+
[Parallel, Method(WorkspaceNames.WorkspaceFolders)]
99
public interface IWorkspaceFoldersHandler : IJsonRpcRequestHandler<WorkspaceFolderParams, Container<WorkspaceFolder>> { }
1010
}

src/Protocol/Workspace/Server/IDidChangeWorkspaceFoldersHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ namespace OmniSharp.Extensions.LanguageServer.Protocol.Server
99
using static WorkspaceNames;
1010

1111
[Parallel, Method(DidChangeWorkspaceFolders)]
12-
public interface IDidChangeWorkspaceFoldersHandler : IJsonRpcNotificationHandler<DidChangeWorkspaceFoldersParams>, ICapability<DidChangeWorkspaceFolderCapability> { }
12+
public interface IDidChangeWorkspaceFoldersHandler : IJsonRpcNotificationHandler<DidChangeWorkspaceFoldersParams>, ICapability<DidChangeWorkspaceFolderCapability>, IRegistration<object> { }
1313
}

src/Protocol/Workspace/Server/IWorkspaceSymbolsHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ namespace OmniSharp.Extensions.LanguageServer.Protocol.Server
99
using static WorkspaceNames;
1010

1111
[Parallel, Method(WorkspaceSymbol)]
12-
public interface IWorkspaceSymbolsHandler : IJsonRpcRequestHandler<WorkspaceSymbolParams, WorkspaceSymbolInformationContainer>, ICapability<WorkspaceSymbolCapability> { }
12+
public interface IWorkspaceSymbolsHandler : IJsonRpcRequestHandler<WorkspaceSymbolParams, WorkspaceSymbolInformationContainer>, ICapability<WorkspaceSymbolCapability>, IRegistration<object> { }
1313
}

src/Server/LanguageServer.cs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,23 @@ public class LanguageServer : ILanguageServer, IInitializeHandler, IInitializedH
4747
private SupportedCapabilities _supportedCapabilities;
4848

4949
public static Task<ILanguageServer> From(Action<LanguageServerOptions> optionsAction)
50+
{
51+
return From(optionsAction, CancellationToken.None);
52+
}
53+
54+
public static Task<ILanguageServer> From(LanguageServerOptions options)
55+
{
56+
return From(options, CancellationToken.None);
57+
}
58+
59+
public static Task<ILanguageServer> From(Action<LanguageServerOptions> optionsAction, CancellationToken token)
5060
{
5161
var options = new LanguageServerOptions();
5262
optionsAction(options);
5363
return From(options);
5464
}
5565

56-
public static async Task<ILanguageServer> From(LanguageServerOptions options)
66+
public static async Task<ILanguageServer> From(LanguageServerOptions options, CancellationToken token)
5767
{
5868
var server = new LanguageServer(
5969
options.Input,
@@ -72,7 +82,7 @@ public static async Task<ILanguageServer> From(LanguageServerOptions options)
7282
if (options.AddDefaultLoggingProvider)
7383
options.LoggerFactory.AddProvider(new LanguageServerLoggerProvider(server));
7484

75-
await server.Initialize();
85+
await server.Initialize(token);
7686

7787
return server;
7888
}
@@ -224,11 +234,21 @@ private IDisposable RegisterHandlers(LspHandlerDescriptorDisposable handlerDispo
224234
}
225235
}
226236

227-
private async Task Initialize()
237+
private async Task Initialize(CancellationToken token)
228238
{
229239
_connection.Open();
230-
231-
await _initializeComplete;
240+
try
241+
{
242+
await _initializeComplete.ToTask(token);
243+
}
244+
catch (TaskCanceledException e)
245+
{
246+
_initializeComplete.OnError(e);
247+
}
248+
catch (Exception e)
249+
{
250+
_initializeComplete.OnError(e);
251+
}
232252
}
233253

234254
async Task<InitializeResult> IRequestHandler<InitializeParams, InitializeResult>.Handle(InitializeParams request, CancellationToken token)
@@ -245,25 +265,25 @@ async Task<InitializeResult> IRequestHandler<InitializeParams, InitializeResult>
245265
_clientVersion = request.Capabilities.GetClientVersion();
246266
_serializer.SetClientCapabilities(_clientVersion.Value, request.Capabilities);
247267

248-
var supportedCapabilites = new List<ISupports>();
268+
var supportedCapabilities = new List<ISupports>();
249269
if (_clientVersion == ClientVersion.Lsp3)
250270
{
251271
if (request.Capabilities.TextDocument != null)
252272
{
253-
supportedCapabilites.AddRange(
273+
supportedCapabilities.AddRange(
254274
LspHandlerDescriptorHelpers.GetSupportedCapabilities(request.Capabilities.TextDocument)
255275
);
256276
}
257277

258278
if (request.Capabilities.Workspace != null)
259279
{
260-
supportedCapabilites.AddRange(
280+
supportedCapabilities.AddRange(
261281
LspHandlerDescriptorHelpers.GetSupportedCapabilities(request.Capabilities.Workspace)
262282
);
263283
}
264284
}
265285

266-
_supportedCapabilities.Add(supportedCapabilites);
286+
_supportedCapabilities.Add(supportedCapabilities);
267287

268288
AddHandlers(_serviceProvider.GetServices<IJsonRpcHandler>().ToArray());
269289

src/Server/LspRequestRouter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public async Task RouteNotification(IHandlerDescriptor descriptor, Notification
104104
else
105105
{
106106
_logger.LogDebug("Converting params for Notification {Method} to {Type}", notification.Method, descriptor.Params.FullName);
107-
var @params = notification.Params.ToObject(descriptor.Params, _serializer.JsonSerializer);
107+
var @params = (notification.Params ?? new JObject()).ToObject(descriptor.Params, _serializer.JsonSerializer);
108108

109109
await MediatRHandlers.HandleNotification(mediator, descriptor, @params ?? EmptyRequest.Instance, CancellationToken.None);
110110
}

test/Lsp.Tests/JsonFixtureAttribute.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.IO;
43
using System.Linq;

test/Lsp.Tests/LanguageServerTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Microsoft.Extensions.Logging;
6+
using NSubstitute;
7+
using OmniSharp.Extensions.LanguageServer.Client;
8+
using OmniSharp.Extensions.LanguageServer.Client.Processes;
9+
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
10+
using OmniSharp.Extensions.LanguageServer.Server;
11+
using Xunit;
12+
using Xunit.Abstractions;
13+
14+
namespace Lsp.Tests
15+
{
16+
public class LanguageServerTests : AutoTestBase
17+
{
18+
public LanguageServerTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
19+
{
20+
}
21+
22+
[Fact]
23+
public async Task Works_With_IWorkspaceSymbolsHandler()
24+
{
25+
var process = new NamedPipeServerProcess(Guid.NewGuid().ToString("N"), LoggerFactory);
26+
await process.Start();
27+
var client = new LanguageClient(LoggerFactory, process);
28+
29+
var handler = Substitute.For<IWorkspaceSymbolsHandler>();
30+
var cts = new CancellationTokenSource();
31+
cts.CancelAfter(1000 * 60 * 5);
32+
33+
var serverStart = LanguageServer.From(x => x
34+
//.WithHandler(handler)
35+
.WithInput(process.ClientOutputStream)
36+
.WithOutput(process.ClientInputStream)
37+
.WithLoggerFactory(LoggerFactory)
38+
.AddDefaultLoggingProvider()
39+
.WithMinimumLogLevel(LogLevel.Trace),
40+
cts.Token
41+
);
42+
43+
await Task.WhenAll(
44+
client.Initialize(
45+
Directory.GetCurrentDirectory(),
46+
new object(),
47+
cts.Token),
48+
serverStart
49+
);
50+
var server = await serverStart;
51+
server.AddHandlers(handler);
52+
}
53+
}
54+
}

test/Lsp.Tests/Lsp.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>netcoreapp2.1</TargetFramework>
44
<WarningsAsErrors>true</WarningsAsErrors>
@@ -9,6 +9,7 @@
99
<EmbeddedResource Include="**\*.json" />
1010
</ItemGroup>
1111
<ItemGroup>
12+
<ProjectReference Include="..\..\src\Client\Client.csproj" />
1213
<ProjectReference Include="..\..\src\Server\Server.csproj" />
1314
<Compile Include="..\JsonRpc.Tests\AutoNSubstitute\*.cs" />
1415
</ItemGroup>

0 commit comments

Comments
 (0)