Skip to content

Instantly share code, notes, and snippets.

@tintoy
Created December 5, 2018 22:19
Show Gist options
  • Save tintoy/a2ec9424d17fe9ef17db0621479a7b43 to your computer and use it in GitHub Desktop.
Save tintoy/a2ec9424d17fe9ef17db0621479a7b43 to your computer and use it in GitHub Desktop.
OmniSharp LSP client sample
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using OmniSharp.Extensions.LanguageServer.Client;
using OmniSharp.Extensions.LanguageServer.Client.Processes;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
namespace SampleClient
{
static class Program
{
public static readonly ILoggerFactory LoggerFactory = new LoggerFactory()
.AddConsole(LogLevel.Information)
.AddDebug(LogLevel.Debug);
static readonly ILogger Log = LoggerFactory.CreateLogger(typeof(Program));
static async Task Main()
{
try
{
var cancellationSource = new CancellationTokenSource();
cancellationSource.CancelAfter(
TimeSpan.FromSeconds(30)
);
// Tell the client how to start and connect to the server. In this case, we're using STDIO (but you can also use named pipes or build your own transport).
ServerProcess serverProcess = new StdioServerProcess(LoggerFactory, new ProcessStartInfo(@"C:\Temp\MyServer.exe") {
Arguments = "--my-arg=1 --another-arg=2" // you may or may not need to pass any command-line arguments to the server
});
using (var client = new LanguageClient(LoggerFactory, serverProcess))
{
Log.LogInformation("Initialising language server...");
// Tell the client to connect to the server and initialise it so it's ready to handle requests.
await client.Initialize(
workspaceRoot: @"C:\SomeDirectory",
initializationOptions: new { }, // If the server requires initialisation options, you pass them here.
cancellationToken: cancellationSource.Token
);
Log.LogInformation("Language server has been successfully initialised.");
// Make a request to the client.
// Note that, in LSP, line and column are 0-based.
Hover hover = await client.TextDocument.Hover(
filePath: @"C:\\MyFile.xml",
line: 7,
column: 3,
cancellationToken: cancellationSource.Token
);
if (hover != null)
{
Log.LogInformation("Got hover info at ({StartPosition})-({EndPosition}): {HoverContent}",
$"{hover.Range.Start.Line},{hover.Range.Start.Character}",
$"{hover.Range.End.Line},{hover.Range.End.Character}",
hover.Contents
);
}
else
Log.LogWarning("No hover info available at ({Line}, {Column}).", 7, 3);
Log.LogInformation("Shutting down server...");
await client.Shutdown();
Log.LogInformation("Waiting for server shutdown to complete...");
await client.HasShutdown;
Log.LogInformation("Server shutdown is complete.");
}
}
catch (Exception unexpectedError)
{
Log.LogError(unexpectedError, "Unexpected error: {ErrorMessage}", unexpectedError.Message);
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Client\Client.csproj" />
<ProjectReference Include="..\..\src\JsonRpc\JsonRpc.csproj" />
<ProjectReference Include="..\..\src\Protocol\Protocol.csproj" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment