Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
marcinjahn opened this issue Jun 30, 2024 · 1 comment
Open

Comments

@marcinjahn
Copy link

marcinjahn commented Jun 30, 2024

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
Copy link

CppCXY commented Jul 1, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants