0

I am trying to implement the example of a LSP client: https://learn.microsoft.com/en-us/visualstudio/extensibility/adding-an-lsp-extension?view=vs-2019#create-a-simple-language-client

When I use the code given on this page, I get an error.

1>A:\develop\Training\VSIXProject1\VSIXProject2\BarLanguageClient.cs(17,38,17,53): error CS0535: 'BarLanguageClient' does not implement interface member 'ILanguageClient.OnServerInitializeFailedAsync(ILanguageClientInitializationInfo)'
1>A:\develop\Training\VSIXProject1\VSIXProject2\BarLanguageClient.cs(17,38,17,53): error CS0535: 'BarLanguageClient' does not implement interface member 'ILanguageClient.ShowNotificationOnInitializeFailed'

It seems that ILanguageClient has changed. When I use the old version of Nuget package (https://www.nuget.org/packages/Microsoft.VisualStudio.LanguageServer.Client/16.10.1220/) it compiles. But when executing my ILanguageClient derived class is not created. Here is my Code:

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.LanguageServer.Client;
using Microsoft.VisualStudio.Threading;
using Microsoft.VisualStudio.Utilities;

namespace VSIXProject2
{
    [ContentType("reni")]
    [Export(typeof(ILanguageClient))]
    public class BarLanguageClient : ILanguageClient
    {
        public BarLanguageClient() { Debugger.Launch();}
        public string Name => "Bar Language Extension";

        public IEnumerable<string> ConfigurationSections => null;

        public object InitializationOptions => null;

        public IEnumerable<string> FilesToWatch => null;

        public event AsyncEventHandler<EventArgs> StartAsync;
        public event AsyncEventHandler<EventArgs> StopAsync;

        public async Task<Connection> ActivateAsync(CancellationToken token)
        {
            await Task.Yield();

            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Server", @"MockLanguageServer.exe");
            info.Arguments = "bar";
            info.RedirectStandardInput = true;
            info.RedirectStandardOutput = true;
            info.UseShellExecute = false;
            info.CreateNoWindow = true;

            Process process = new Process();
            process.StartInfo = info;

            if (process.Start())
            {
                return new Connection(process.StandardOutput.BaseStream, process.StandardInput.BaseStream);
            }

            return null;
        }

        public async Task OnLoadedAsync()
        {
            await StartAsync.InvokeAsync(this, EventArgs.Empty);
        }

        public Task OnServerInitializeFailedAsync(Exception e)
        {
            return Task.CompletedTask;
        }

        public Task OnServerInitializedAsync()
        {
            return Task.CompletedTask;
        }
    }
}

This is my content type definition:

using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Utilities;

namespace VSIXProject2
{
    public class ReniContentDefinition
    {
        [Export]
        [Name("reni")]
        [BaseDefinition("code")]
        internal static ContentTypeDefinition ReniContentTypeDefinition;

        [Export]
        [FileExtension(".reni")]
        [ContentType("reni")]
        internal static FileExtensionToContentTypeDefinition ReniFileExtensionDefinition;
    }
}

When I press F5 on the VSIX-project another Visual Studio is openend with commandline parameters "/rootsuffix Exp". I select a folder that contains some files with extension ".reni". I open such a file. I would expect that my ILanguageClient-derived class is instantiated then. But the "Debugger.Launch()" I put inside the ctor is obviously not reached.

After some investigation I wonder if it is a problem with different versions of ILanguageClient. My current version of VS2019 is Version 16.11.7. I guess the Microsoft.VisualStudio.LanguageServer.Client where ILanguageClient is contained in also has this version. The nuget packages for Microsoft.VisualStudio.LanguageServer.Client are available for 16.10 and for 17.x. Not for 16.11.7.

I can't imagine how that can work at all.

Harald Hoyer
  • 515
  • 4
  • 14
  • What do you mean by "error" and "not created"? What _does_ happen? – CodeCaster Nov 11 '21 at 14:09
  • Edited my text. Is it sufficient now? – Harald Hoyer Nov 11 '21 at 15:13
  • Thanks! Looks like the code sample was not updated to reflect the VS2022 changes (17.0.5158). You need to implement that interface if you want to support VS2022. Everything about extensions is different in VS2022, they don't support multi-targeting yet AFAIK. So decide which VS version you want to support, and build for that. – CodeCaster Nov 11 '21 at 15:23
  • I want to get it running under VS2019. But even when using the version before (16.10.1220) it does not work. – Harald Hoyer Nov 11 '21 at 16:34
  • Change the link to "Add a Language Server Protocol extension" into the VS2019-edition – Harald Hoyer Nov 11 '21 at 20:00
  • Looks like info.FileName = Path.Combine(……. **@"MockLanguageServer.exe"** ) uses MockLanguageServer.exe, does it need to be modified? – Tianyu Nov 17 '21 at 07:09

0 Answers0