JorgeFD
JorgeFD

Reputation: 21

Use nVim as an editor for c# language version 10

I'm trying to configure nvim as an replacement for VSCode an VS2022. I've managed to make autocompletion to work, but it seems to be compiling my code using an older language version (I'm using language version 10), so my code it's been marked with many errors, although it compiles correctly with 'dotnet build'.

I'm not sure if thats a problem with Roslyn or LSP so not sure where should I look for more information.

I've tried with both omnisharp and csharp-ls as LSP servers (installed through nvim-lsp-installer) an both report the same errors.

The plugins i'm using are:

    Plug 'mfussenegger/nvim-dap'
    Plug 'williamboman/nvim-lsp-installer'
    Plug 'neovim/nvim-lspconfig'
    Plug 'nvim-lua/completion-nvim'
    Plug 'hrsh7th/nvim-cmp' 
    Plug 'hrsh7th/cmp-nvim-lsp' 
    Plug 'OmniSharp/omnisharp-vim'
    Plug 'dense-analysis/ale'
    Plug 'mattn/emmet-vim'
    Plug 'tpope/vim-commentary'
    Plug 'Yggdroot/indentLine'
    Plug 'vim-airline/vim-airline'
    Plug 'scrooloose/nerdtree'
    Plug 'ryanoasis/vim-devicons'
    Plug 'nvim-lua/plenary.nvim'
    Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}
    Plug 'nvim-telescope/telescope.nvim'

This is the configuration of the lsp servers:

require'lspconfig'.csharp_ls.setup{}
require'lspconfig'.omnisharp.setup {
    capabilities = require('cmp_nvim_lsp').update_capabilities(vim.lsp.protocol.make_client_capabilities()),
      on_attach = function(_, bufnr)
          vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
     end,
       cmd = { "~/.cache/omnisharp-vim/omnisharp-roslyn/run", "--languageserver" , "--hostPID", tostring(pid) },
}

The following images shows that with both charp-ls and with omnisharp I get an error at the var keyword in line 3, because it's not recognising the feature of C# 9 that allows to write top level statements. I tryed to explicitly specify the LangVersion at the csproj file, but the results where the same.

[Error not recongnising new main style in csharp-ls1 Error not recongnising new main style in omnisharp

Upvotes: 0

Views: 2315

Answers (1)

JorgeFD
JorgeFD

Reputation: 21

I finally found the problem, it seems like there are two services analysing the code.

I added the following line to ensure that only Omnisharp is writing compiler errors to the buffer:

let g:ale_linters = { 'cs': ['OmniSharp'] }

Also, I added the following line, restarted Vim and reinstalled Omnisharp to ensure I had the version with the latest featurest, although this made no difference until I added the line explained before:

let g:OmniSharp_server_use_net6 = 1

Upvotes: 1

Related Questions