You’ve Been Using the WRONG LSP for C# in Neovim


More and more .NET developers are starting to notice the advantages of using Neovim as their main code editor. But let’s be honest most of us don’t want to waste time configuring endless settings. We want the things and we wanted now. I mean, we just want a great editor, an enjoyable coding experience, and maximum efficiency.

That’s why most developers choose the most straightforward way to set up their LSP inside Neovim: Omnisharp LSP.

Although the OmniSharp LSP has been good enough for developing inside Neovim, it has never quite matched the experience of the LSP used by VS Code or Visual Studio. However, things have changed, now you can use Roslyn LSP, which provides the same core functionality offered by VS Code and Visual Studio.

In this article I show you how to install it in just four steps. We’ll need the Lazy package manager and a basic understanding of how to install a plugin, which you can find here:

https://lazy.folke.io/installation

1. Install Mason

Mason is a plugin that allows you to easily install the LSPs you need. To install it, you just need to add the plugin:

{
    "mason-org/mason.nvim",
     opts = {}
}

Unfortunately, there is still no official mason register for Roslyn, but there are few non oficial registers that we can use.

We need to modify the previous code and set it up like this:

{
    "mason-org/mason.nvim",
    config = function()
        require('mason').setup({
            registries = {
                'github:Crashdummyy/mason-registry', -- this contains the register for Roslyn
                'github:mason-org/mason-registry', 
            },
        })
    end
}

2. Install Roslyn LSP

Inside Neovim, you just need to execute this command in normal mode:

:MasonInstall Roslyn

3. Install seblyng/roslyn.nvim plugin

Right now seblying plugin is the best alternative to avoid writting configuration. You just need to add this plugin

{
    "seblyng/roslyn.nvim",
    enabled = true,
    ft = "cs",
    config = function()
        vim.lsp.config("roslyn", {
            on_attach = function()
                print("This will run when the server attaches!")
            end,
            settings = {
              -- Check settings in https://github.com/seblyng/roslyn.nvim
            },

        })
        local roslyn = require("roslyn");
        roslyn.setup();
    end
}

4. Check your configuration

Lets create a proyect to test the configuration.

dotnet new console --name roslyn

Now enter enter to neovim

nvim roslyn/Program.cs

Check if lsp is enabled running this command

:LspInfo

You should see something like

vim.lsp: Active Clients ~
- roslyn (id: 1)
  - Version: ? (no serverInfo.version response)
  - Root directory: ~/roslyn
  - Command: { "roslyn", "--logLevel=Information", "--extensionLogDirectory=/home/ricardo/.local/state/nvim", "--stdio" }
  - Settings: {}
  - Attached buffers: 1

Links