Skip to content

Commit de8f559

Browse files
committed
1 parent a8b0458 commit de8f559

File tree

10 files changed

+143
-25
lines changed

10 files changed

+143
-25
lines changed

samples/Client/Client.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<Import Project="../Samples.props" />
99

1010
<ItemGroup>
11-
<PackageReference Include="OmniSharp.Extensions.JsonRpc" Version="0.1.3" />
12-
<PackageReference Include="OmniSharp.Extensions.LanguageServerProtocol" Version="0.1.3" />
11+
<PackageReference Include="OmniSharp.Extensions.JsonRpc" Version="0.4.0" />
12+
<PackageReference Include="OmniSharp.Extensions.LanguageServerProtocol" Version="0.4.0" />
1313
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
1414
<PackageReference Include="Serilog" Version="2.5.0" />
1515
<PackageReference Include="Serilog.Sinks.Debug" Version="1.0.0" />

samples/Common/Common.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<Import Project="../Samples.props" />
88

99
<ItemGroup>
10-
<PackageReference Include="OmniSharp.Extensions.JsonRpc" Version="0.1.3" />
11-
<PackageReference Include="OmniSharp.Extensions.LanguageServerProtocol" Version="0.1.3" />
10+
<PackageReference Include="OmniSharp.Extensions.JsonRpc" Version="0.4.0" />
11+
<PackageReference Include="OmniSharp.Extensions.LanguageServerProtocol" Version="0.4.0" />
1212
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
1313
<PackageReference Include="Serilog" Version="2.5.0" />
1414
</ItemGroup>

samples/Common/HoverHandler.cs

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using OmniSharp.Extensions.LanguageServer.Protocol;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using OmniSharp.Extensions.LanguageServer.Capabilities.Client;
6+
using OmniSharp.Extensions.LanguageServer.Models;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
10+
namespace Common
11+
{
12+
/// <summary>
13+
/// Handler for LSP "textDocument/hover" requests.
14+
/// </summary>
15+
public class HoverHandler
16+
: IHoverHandler
17+
{
18+
/// <summary>
19+
/// Create a new <see cref="HoverHandler"/>.
20+
/// </summary>
21+
public HoverHandler()
22+
{
23+
}
24+
25+
/// <summary>
26+
/// Registration options for the hover handler.
27+
/// </summary>
28+
public TextDocumentRegistrationOptions TextDocumentRegistrationOptions { get; } = new TextDocumentRegistrationOptions
29+
{
30+
DocumentSelector = new DocumentSelector(
31+
new DocumentFilter
32+
{
33+
Language = "xml",
34+
Pattern = "**/*.csproj"
35+
}
36+
)
37+
};
38+
39+
/// <summary>
40+
/// The client's hover capabilities.
41+
/// </summary>
42+
public HoverCapability Capabilities { get; private set; }
43+
44+
/// <summary>
45+
/// Handle a hover request.
46+
/// </summary>
47+
/// <param name="request">
48+
/// The hover request.
49+
/// </param>
50+
/// <param name="token">
51+
/// A cancellation token that can be used to cancel the request.
52+
/// </param>
53+
/// <returns>
54+
/// The <see cref="Hover"/>, or <c>null</c> if no hover information is available at the target document position.
55+
/// </returns>
56+
public Task<Hover> Handle(TextDocumentPositionParams request, CancellationToken token)
57+
{
58+
return Task.FromResult(new Hover
59+
{
60+
Range = new Range(
61+
start: request.Position,
62+
end: request.Position
63+
),
64+
Contents = new MarkedStringContainer(
65+
$"Hover for {request.Position.Line + 1},{request.Position.Character + 1} in '{request.TextDocument.Uri}'."
66+
)
67+
});
68+
}
69+
70+
/// <summary>
71+
/// Get registration options for the hover handler.
72+
/// </summary>
73+
/// <returns>
74+
/// The <see cref="TextDocumentRegistrationOptions"/>.
75+
/// </returns>
76+
public TextDocumentRegistrationOptions GetRegistrationOptions() => TextDocumentRegistrationOptions;
77+
78+
/// <summary>
79+
/// Called to provide information about the client's hover capabilities.
80+
/// </summary>
81+
/// <param name="capabilities">
82+
/// The client's hover capabilities.
83+
/// </param>
84+
public void SetCapability(HoverCapability capabilities)
85+
{
86+
Capabilities = capabilities;
87+
}
88+
}
89+
}

samples/Server/Program.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Threading;
99
using System.Threading.Tasks;
1010

11+
using MSLogging = Microsoft.Extensions.Logging;
12+
1113
namespace Server
1214
{
1315
/// <summary>
@@ -57,7 +59,11 @@ static async Task AsyncMain()
5759
{
5860
Log.Information("Initialising language server...");
5961

60-
LanguageServer languageServer = new LanguageServer(input: Console.OpenStandardInput(2048), output: Console.OpenStandardOutput(2048));
62+
LanguageServer languageServer = new LanguageServer(
63+
input: Console.OpenStandardInput(2048),
64+
output: Console.OpenStandardOutput(2048),
65+
loggerFactory: new MSLogging.LoggerFactory().AddSerilog(Log.Logger.ForContext<LanguageServer>())
66+
);
6167
languageServer.AddHandler(
6268
new ConfigurationHandler()
6369
);

samples/Server/Server.csproj

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
<Import Project="../Samples.props" />
99

1010
<ItemGroup>
11-
<PackageReference Include="OmniSharp.Extensions.JsonRpc" Version="0.1.3" />
12-
<PackageReference Include="OmniSharp.Extensions.LanguageServerProtocol" Version="0.1.3" />
11+
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
12+
<PackageReference Include="OmniSharp.Extensions.JsonRpc" Version="0.4.0" />
13+
<PackageReference Include="OmniSharp.Extensions.LanguageServerProtocol" Version="0.4.0" />
1314
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
1415
<PackageReference Include="Serilog" Version="2.5.0" />
16+
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />
1517
<PackageReference Include="Serilog.Sinks.Debug" Version="1.0.0" />
1618
<PackageReference Include="Serilog.Sinks.Seq" Version="3.4.0" />
1719
<PackageReference Include="System.Reactive" Version="3.1.1" />

samples/SingleProcess/Program.cs

+22-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Threading.Tasks;
1717

1818
using LanguageClient = LSP.Client.LanguageClient;
19+
using MSLogging = Microsoft.Extensions.Logging;
1920

2021
namespace SingleProcess
2122
{
@@ -119,17 +120,20 @@ static async Task RunLanguageClient(NamedPipeServerProcess serverProcess)
119120
);
120121
});
121122

122-
await client.Initialize(workspaceRoot: @"C:\Foo");
123+
JObject settings = new JObject(
124+
new JProperty("setting1", true),
125+
new JProperty("setting2", "Hello")
126+
);
127+
128+
await client.Initialize(
129+
workspaceRoot: @"C:\Foo",
130+
initializationOptions: settings
131+
);
123132

124133
Log.Information("Client started.");
125134

126135
// Update server configuration.
127-
client.Workspace.DidChangeConfiguration(
128-
new JObject(
129-
new JProperty("setting1", true),
130-
new JProperty("setting2", "Hello")
131-
)
132-
);
136+
client.Workspace.DidChangeConfiguration(settings);
133137

134138
// Invoke our custom handler.
135139
await client.SendRequest("dummy", new DummyParams
@@ -165,7 +169,9 @@ static async Task RunLanguageServer(Stream input, Stream output)
165169

166170
Log.Information("Initialising language server...");
167171

168-
LanguageServer languageServer = new LanguageServer(input, output);
172+
LanguageServer languageServer = new LanguageServer(input, output,
173+
loggerFactory: new MSLogging.LoggerFactory().AddSerilog(Log.Logger.ForContext<LanguageServer>())
174+
);
169175

170176
languageServer.AddHandler(
171177
new ConfigurationHandler()
@@ -174,6 +180,14 @@ static async Task RunLanguageServer(Stream input, Stream output)
174180
new DummyHandler(languageServer)
175181
);
176182

183+
languageServer.OnInitialize(parameters =>
184+
{
185+
JToken options = parameters.InitializationOptions as JToken;
186+
Log.Information("Server received initialisation options: {Options}", options?.ToString(Newtonsoft.Json.Formatting.None));
187+
188+
return Task.CompletedTask;
189+
});
190+
177191
Log.Information("Starting language server...");
178192
languageServer.Shutdown += shutdownRequested =>
179193
{

samples/SingleProcess/SingleProcess.csproj

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
<Import Project="../Samples.props" />
99

1010
<ItemGroup>
11-
<PackageReference Include="OmniSharp.Extensions.JsonRpc" Version="0.1.3" />
12-
<PackageReference Include="OmniSharp.Extensions.LanguageServerProtocol" Version="0.1.3" />
11+
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
12+
<PackageReference Include="OmniSharp.Extensions.JsonRpc" Version="0.4.0" />
13+
<PackageReference Include="OmniSharp.Extensions.LanguageServerProtocol" Version="0.4.0" />
1314
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
1415
<PackageReference Include="Serilog" Version="2.5.0" />
16+
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />
1517
<PackageReference Include="Serilog.Sinks.Debug" Version="1.0.0" />
1618
<PackageReference Include="Serilog.Sinks.Seq" Version="3.4.0" />
1719
<PackageReference Include="System.Reactive" Version="3.1.1" />

src/LSP.Client/LSP.Client.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<Import Project="../Common.props" />
77

88
<ItemGroup>
9-
<PackageReference Include="OmniSharp.Extensions.JsonRpc" Version="0.1.3" />
10-
<PackageReference Include="OmniSharp.Extensions.LanguageServerProtocol" Version="0.1.3" />
9+
<PackageReference Include="OmniSharp.Extensions.JsonRpc" Version="0.4.0" />
10+
<PackageReference Include="OmniSharp.Extensions.LanguageServerProtocol" Version="0.4.0" />
1111
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
1212
<PackageReference Include="Serilog" Version="2.5.0" />
1313
<PackageReference Include="System.Reactive" Version="3.1.1" />

src/LSP.Client/LanguageClient.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace LSP.Client
1313
using Dispatcher;
1414
using Handlers;
1515
using Logging;
16+
using Newtonsoft.Json.Linq;
1617
using Processes;
1718
using Protocol;
1819

@@ -211,18 +212,21 @@ public Task HasShutdown
211212
/// <param name="workspaceRoot">
212213
/// The workspace root.
213214
/// </param>
215+
/// <param name="initializationOptions">
216+
/// An optional <see cref="Object"/> representing additional options to send to the server.
217+
/// </param>
214218
/// <param name="cancellationToken">
215219
/// An optional <see cref="CancellationToken"/> that can be used to cancel the operation.
216220
/// </param>
217221
/// <returns>
218222
/// A <see cref="Task"/> representing initialisation.
219223
/// </returns>
220224
/// <exception cref="InvalidOperationException">
221-
/// <see cref="Initialize(string, CancellationToken)"/> has already been called.
225+
/// <see cref="Initialize(string, object, CancellationToken)"/> has already been called.
222226
///
223-
/// <see cref="Initialize(string, CancellationToken)"/> can only be called once per <see cref="LanguageClient"/>; if you have called <see cref="Shutdown"/>, you will need to use a new <see cref="LanguageClient"/>.
227+
/// <see cref="Initialize(string, object, CancellationToken)"/> can only be called once per <see cref="LanguageClient"/>; if you have called <see cref="Shutdown"/>, you will need to use a new <see cref="LanguageClient"/>.
224228
/// </exception>
225-
public async Task Initialize(string workspaceRoot, CancellationToken cancellationToken = default(CancellationToken))
229+
public async Task Initialize(string workspaceRoot, object initializationOptions = null, CancellationToken cancellationToken = default(CancellationToken))
226230
{
227231
if (IsInitialized)
228232
throw new InvalidOperationException("Client has already been initialised.");
@@ -235,7 +239,8 @@ public Task HasShutdown
235239
{
236240
RootPath = workspaceRoot,
237241
Capabilities = ClientCapabilities,
238-
ProcessId = Process.GetCurrentProcess().Id
242+
ProcessId = Process.GetCurrentProcess().Id,
243+
InitializationOptions = initializationOptions
239244
};
240245

241246
Log.Verbose("Sending 'initialize' message to language server...");

test/LSP.Client.Tests/LSP.Client.Tests.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<Import Project="../TestCommon.props" />
88

99
<ItemGroup>
10-
<PackageReference Include="OmniSharp.Extensions.JsonRpc" Version="0.1.3" />
11-
<PackageReference Include="OmniSharp.Extensions.LanguageServerProtocol" Version="0.1.3" />
10+
<PackageReference Include="OmniSharp.Extensions.JsonRpc" Version="0.4.0" />
11+
<PackageReference Include="OmniSharp.Extensions.LanguageServerProtocol" Version="0.4.0" />
1212
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
1313
<PackageReference Include="Serilog" Version="2.5.0" />
1414
<PackageReference Include="Serilog.Sinks.Debug" Version="1.0.0" />

0 commit comments

Comments
 (0)