OmniSharp / omnisharp-vim

Vim omnicompletion (intellisense) and more for C#
http://www.omnisharp.net
MIT License
1.7k stars 169 forks source link

Problem with coc-nvim #788

Closed JDECOMBE closed 2 years ago

JDECOMBE commented 2 years ago

Hi,

I've had some problem making omnisharp-vim and coc work together. Both of them seem to work fine (:OmniSharpGoToDefinition is working as expected), but for some reason, whenever trying to "call" a coc directive (coc-implementation for example), I get this error message:

[coc.nvim] Error on notification "jumpImplementation": implementation provider not found for current buffer
, your language server doesn't support it.

As I'm just starting working with (Neo)Vim, I may have setup something wrong.

Here are some parts of my init.vim that may be the source of the problem.

call plug#begin()
...
Plug 'OmniSharp/omnisharp-vim'
...
Plug 'neoclide/coc.nvim', { 'branch': 'release' }
...
call plug#end()

...

nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)
...

Since I stole that init.vim from someone else (credit goes to jdh) and I just added the Plug 'OmniSharp/omnisharp-vim' line, I don't really see where the problem could come from..

Thanks for your time !

nickspoons commented 2 years ago

That error is on the coc side, did doesn't have anything to do with this plugin.

Be aware that if you are configuring coc to handle LSP actions such as go-to definition find references etc., then coc and omnisharp-vim will both be running their own separate servers in the background. I don't know if coc automatically configures C# to use the OmniSharp-roslyn server, if it doesn't you might need to configure that part, that may be where your issue is.

nickspoons commented 2 years ago

If you just want to use coc for the auto-completion functionality with C#, then I'd recommend keeping your configuration as it is but override the coc mappings with their omnisharp equivalents in C# buffers:

augroup omnisharp_commands
  autocmd!

  autocmd FileType cs nmap <silent> <buffer> gd <Plug>(omnisharp_go_to_definition)
  " ... other omnisharp-vim mappings
augroup END

Making your mappings <buffer> local like this means that they only apply to C# buffers, so in other filetypes gd will still fall back to your coc mapping.

The coc autocompletion is in this case still using omnisharp-vim to handle the completions via the standard vim omnifunc, so there is only a single language server running (which omnisharp-vim is handling, not coc).

JDECOMBE commented 2 years ago

Thanks a lot for your response ! I'll override the bindings to directly use Omni Sharp in C# buffers then.

Cheers !

tankorsmash commented 1 year ago

@JDECOMBE Do you happen to have a list of the bindings you've made to support that? I'm running into the same issue now where I'm trying to jump to a definition via <Plug>(coc-definition) but it's throwing the same error as yours.

nickspoons commented 1 year ago

@tankorsmash the omnisharp equivalent of <Plug>(coc-definition) is what I included in my last comment.

The definitive list of all omnisharp <Plug>(...) mappings is here: https://github.com/OmniSharp/omnisharp-vim/blob/master/ftplugin/cs/OmniSharp.vim#L90-L127

JDECOMBE commented 1 year ago

My solution is to stop using omnisharp... I've switch to csharp-ls which, in my opinion is more reliable than omnisharp.

Once installed, here's what you need to add to your coc configuration file (:CocConfig):

{
  "languageserver": {
    "csharp-ls": {
      "command": "csharp-ls",
      "filetypes": ["cs"],
      "rootPatterns": ["*.csproj", ".vim/", ".git/", ".hg/"]
    }
  }
}

While looking into my coc configuration for you, I've found that I have csharpls-extended-lsp.nvim installed so you may want to look into it as well.

Sorry for not having a working configuration with omnisharp, hope this still helps you !

nickspoons commented 1 year ago

I've switch to csharp-ls which, in my opinion is more reliable than omnisharp.

Can you please give examples of what you mean by this?

JDECOMBE commented 1 year ago

I switched to csharp-ls about a year ago because I was working on several projects, each of which had a different .NET version (from .NET6 to .netstandard2.1) and I couldn't find a way to make omnisharp work with all of them at the same time. iirc, I had to toggle a global variable (something like g:omnisharp_use_dotnet_6) every time. Maybe that have been solved every since but I never found the need to stop using csharp-ls.

cachandlerdev commented 1 year ago

To anybody who comes across this thread at some point in the future and was looking for a ctrl+c ctrl+v solution, you have to override your coc keybindings with a call to the corresponding omnisharp functions, as nickspoons pointed out earlier.

More specifically, I solved the issue on my end by creating a ~/.config/nvim/after/plugin/coc.vim file and putting in the following code.

augroup omnisharp_commands
  autocmd!

  autocmd FileType cs nmap <silent> <buffer> gd <Plug>(omnisharp_go_to_definition)
  autocmd FileType cs nmap <silent> <buffer> gy <Plug>(omnisharp_go_to_type_definition)
  autocmd FileType cs nmap <silent> <buffer> gi <Plug>(omnisharp_find_implementations)
  autocmd FileType cs nmap <silent> <buffer> gr <Plug>(omnisharp_find_usages)
  autocmd FileType cs nmap <silent> <buffer> K  <Plug>(omnisharp_documentation)
  autocmd FileType cs nmap <silent> <buffer> <leader>f <Plug>(omnisharp_code_format)
  autocmd FileType cs nmap <silent> <buffer> <leader>ac <Plug>(omnisharp_code_actions)
  autocmd FileType cs nmap <silent> <buffer> <leader>re <Plug>(omnisharp_rename)
  " ... other omnisharp-vim mappings
augroup END

There are probably a few keybinds I forgot to add, but I'm not that familiar with vim yet and was going off the coc example configuration, so this seemed satisfactory for my needs. I thought I'd leave this here though because comparing the coc and omnisharp bindings to see which ones need to be replaced, and then figuring out where these settings are supposed to go, is not exactly an exciting process.

nickspoons commented 1 year ago

@BagelSeasoning234 if you're putting config in separate files (which is a good idea), it makes more sense in this case to use a ~/.config/nvim/after/ftplugin/cs.vim script, which is automatically sourced whenever you open a .cs file so doesn't need the autocmds, it just becomes:

nmap <silent> <buffer> gd <Plug>(omnisharp_go_to_definition)
nmap <silent> <buffer> gy <Plug>(omnisharp_go_to_type_definition)
nmap <silent> <buffer> gi <Plug>(omnisharp_find_implementations)
nmap <silent> <buffer> gr <Plug>(omnisharp_find_usages)
nmap <silent> <buffer> K  <Plug>(omnisharp_documentation)
nmap <silent> <buffer> <leader>f <Plug>(omnisharp_code_format)
nmap <silent> <buffer> <leader>ac <Plug>(omnisharp_code_actions)
nmap <silent> <buffer> <leader>re <Plug>(omnisharp_rename)
cachandlerdev commented 1 year ago

I see. Thanks! I only put the code there at first because it wasn't working when I directly added it to init.vim, and I saw someone else do something similar in another dotfiles repo.

nickspoons commented 1 year ago

The difference is that /plugin/ scripts are only sourced once, on startup, after .vimrc/init.vim. So in those you need autocmds to tell vim that the following applies to all .cs files. Whereas the /ftplugin/ scripts basically already have that autocmd Filetype cs logic baked in.

greyblake commented 11 months ago

@nickspoons @BagelSeasoning234 Thank you guys! After a long struggle I finally made it work on linux with Neovim + COC + OmniSharp + Roslyn + Mono.