简体   繁体   中英

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.

[ 无法识别 csharp-ls1 中的新主要样式时出错 在omnisharp中无法识别新的主要风格时出错

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM