OmniSharp / csharp-language-server-protocol

Language Server Protocol in C#
MIT License
528 stars 104 forks source link

Minimal example of server with Microsoft.Extensions.DependencyInjection #1276

Open marcinjahn opened 5 months ago

marcinjahn commented 5 months ago

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?

CppCXY commented 5 months ago

You can refer to my project. https://github.com/CppCXY/EmmyLuaAnalyzer/blob/master/EmmyLua.LanguageServer/LanguageServer.cs