OmniSharp / OmniSharp/csharp-language-server-protocol

Minimal example of server with Microsoft.Extensions.DependencyInjection

Open
#1,276 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
638
Forks
109
Avg merge
1m
Merged PRs (30d)
2

Description

I wanted to create a simple LSP server. I looked at https://github.com/OmniSharp/csharp-language-server-protocol/tree/master/sample/SampleServer. The example works alright, I'm able to communicate with the server from neovim.
I tried to create something myself, however, I wanted to use the Microsoft.Extensions.DependencyInjection to manage dependencies of my app, including the ILanguageServer. Here's a simple Program.cs I created:

Log.Logger = new LoggerConfiguration()
    .Enrich.FromLogContext()
    .WriteTo.File(AppDomain.CurrentDomain.BaseDirectory + "log.txt", rollingInterval: RollingInterval.Day)
    .MinimumLevel.Verbose()
    .CreateLogger();

Log.Logger.Information("STARTING");

IObserver<WorkDoneProgressReport> workDone = null!;

var serviceCollection = new ServiceCollection();
serviceCollection
    .AddLogging(logging => logging
        .ClearProviders()
        .AddSerilog(Log.Logger)
        .AddLanguageProtocolLogging()
        .SetMinimumLevel(LogLevel.Debug)
    )
    .AddLanguageServer(options => options
        .WithInput(Console.OpenStandardInput())
        .WithOutput(Console.OpenStandardOutput())
        .OnInitialize(
            async (server, request, token) =>
            {
                var logger = server.Services.GetRequiredService<ILogger<Program>>();
                logger.LogInformation("INITIALIZING");

                var manager = server.WorkDoneManager.For(
                    request, new WorkDoneProgressBegin
                    {
                        Title = "Server is starting...",
                        Percentage = 10,
                    }
                );
                workDone = manager;

                await Task.Delay(2000).ConfigureAwait(false);

                manager.OnNext(
                    new WorkDoneProgressReport
                    {
                        Percentage = 20,
                        Message = "loading in progress"
                    }
                );
            }
        )
        .OnStarted(
            async (languageServer, token) =>
            {
                using var manager = await languageServer.WorkDoneManager
                    .Create(new WorkDoneProgressBegin { Title = "Doing some work..." })
                    .ConfigureAwait(false);

                manager.OnNext(new WorkDoneProgressReport { Message = "doing things..." });
                manager.OnNext(new WorkDoneProgressReport { Message = "doing things... 1234" });
                manager.OnNext(new WorkDoneProgressReport { Message = "doing things... 56789" });

                var logger = languageServer.Services.GetRequiredService<ILogger<Program>>();

                logger.LogInformation("STARTED");
            }
        )
    );

var provider = serviceCollection.BuildServiceProvider();

var server = provider.GetRequiredService<ILanguageServer>();
await server.Initialize(CancellationToken.None); // I tried with or without this

await server.WaitForExit;

I never get to the "INITIALIZING" part, it's like the server doesn't even react to initialization request. The example is very similar to what the official sample provided. Could someone point out what I'm doing wrong?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Compare the reported Program.cs with sample/SampleServer, then trace the AddLanguageServer and ILanguageServer initialization entry points. A useful result would be a documented minimal Microsoft.Extensions.DependencyInjection example that reaches the OnInitialize callback and explains the required setup.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
developer-experience, documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.