yegappan / lsp

Language Server Protocol (LSP) plugin for Vim9
MIT License
474 stars 55 forks source link

Adding lsp servers through VimEnter autocmd with Plug fails for files loaded from command line #98

Closed normen closed 7 months ago

normen commented 1 year ago

For files that are opened from command line (e.g. vim src/main.cpp) the LSP isn't properly loaded when using Plug and the VimEnter autocommand. After vim is opened the LSP isn't active. When re-loading the file with :e the LSP is activated. I suppose the VimEnter autocommand is executed too late in this case.

Edit: Adding through lsp#lsp#AddServer() works btw.

yegappan commented 1 year ago

I have committed 13219be58dfbf6a676eccab48eee16a193ada97b to address this issue. Let me know if this addresses the problem.

normen commented 1 year ago

Hm, no it still doesn't work when using the VimEnter hook 🤔

yegappan commented 1 year ago

Which language server are you using? Can you attach the VimEnter command that you are using?

normen commented 1 year ago

Theres two that I manually add through the hook. The rest are added through my own lsp-settings-adapter plugin which also uses the lsp#lsp#Addserver directly.

    let lspServers = [
          \     #{
          \  filetype: ['c', 'cpp'],
          \  path: 'ccls',
          \      }
          \   ]
    "autocmd VimEnter * call LspAddServer(lspServers)
    call lsp#lsp#AddServer(lspServers)

  let lspServers = [
        \     #{
        \  filetype: ['openscad'],
        \  path: $HOME.'/.cargo/bin/openscad-lsp',
        \  args: ['--stdio']
        \      }
        \   ]
  "autocmd VimEnter * call LspAddServer(lspServers)
  call lsp#lsp#AddServer(lspServers)
normen commented 1 year ago

By the way the vim-lsp plugin solves this by having its own Hook for the autocommand, you add LSPs like this:

    au User lsp_setup call lsp#register_server({
          \ 'name': 'ccls',
          ....
          \ })
Shane-XB-Qian commented 1 year ago

1, maybe that lsp server could not startup in 10m (10/1000s), that's the default/hardcode value in this lsp client. 2, to check if that lsp server was using async or not, sometime: cInfo.items[cInfo.selected].user_data.label != cItem.label in CompletionResolveReply() would be err e715. update: #112

// perhaps you can try to debug a bit to get more detail information.

saccarosium commented 8 months ago

Very late, but I do this by using a timer:

let s:lsp_settings = {
      \ 'autoComplete': v:false,
      \ 'completionMatcher': 'icase',
      \ 'ignoreMissingServer': v:true,
      \ 'showSignature': v:false,
      \}

let s:lsp_servers = [
      \{
      \ 'filetype': ['c', 'cpp', 'objc', 'objcpp'],
      \ 'path': 'clangd',
      \ 'args': ['--background-index']
      \},
      \{
      \ 'filetype': 'python',
      \ 'path': 'jedi-language-server',
      \ 'args': [],
      \},
      \{
      \ 'filetype': 'python',
      \ 'path': 'ruff-lsp',
      \ 'args': [],
      \},
      \{
      \ 'filetype': 'rust',
      \ 'path': 'rust-analyzer',
      \ 'args': [],
      \ 'syncInit': v:true,
      \},
      \]

function! s:LspSetup(...) abort
  packadd lsp
  call LspOptionsSet(s:lsp_settings)
  call LspAddServer(s:lsp_servers)
endfunction

function! s:OnLspBuffer() abort
  setlocal signcolumn=yes
  nmap <buffer> gd :LspGotoDefinition<CR>
  nmap <buffer> gl :LspDiagCurrent<CR>
  nmap <buffer> gr :LspShowReferences<CR>
  nmap <buffer> K :LspHover<CR>
  nmap <buffer> <leader>r :LspRename<CR>
  nmap <buffer> <leader>a :LspCodeAction<CR>
endfunction

augroup lsp_init
  au!
  autocmd VimEnter * ++once call timer_start(0, 's:LspSetup')
  autocmd User LspAttached call s:OnLspBuffer()
augroup END

But probably using something like what is used in vim-lsp would be more user friendly.

yegappan commented 7 months ago

Very late, but I do this by using a timer:

let s:lsp_settings = {
      \ 'autoComplete': v:false,
      \ 'completionMatcher': 'icase',
      \ 'ignoreMissingServer': v:true,
      \ 'showSignature': v:false,
      \}

let s:lsp_servers = [
      \{
      \ 'filetype': ['c', 'cpp', 'objc', 'objcpp'],
      \ 'path': 'clangd',
      \ 'args': ['--background-index']
      \},
      \{
      \ 'filetype': 'python',
      \ 'path': 'jedi-language-server',
      \ 'args': [],
      \},
      \{
      \ 'filetype': 'python',
      \ 'path': 'ruff-lsp',
      \ 'args': [],
      \},
      \{
      \ 'filetype': 'rust',
      \ 'path': 'rust-analyzer',
      \ 'args': [],
      \ 'syncInit': v:true,
      \},
      \]

function! s:LspSetup(...) abort
  packadd lsp
  call LspOptionsSet(s:lsp_settings)
  call LspAddServer(s:lsp_servers)
endfunction

function! s:OnLspBuffer() abort
  setlocal signcolumn=yes
  nmap <buffer> gd :LspGotoDefinition<CR>
  nmap <buffer> gl :LspDiagCurrent<CR>
  nmap <buffer> gr :LspShowReferences<CR>
  nmap <buffer> K :LspHover<CR>
  nmap <buffer> <leader>r :LspRename<CR>
  nmap <buffer> <leader>a :LspCodeAction<CR>
endfunction

augroup lsp_init
  au!
  autocmd VimEnter * ++once call timer_start(0, 's:LspSetup')
  autocmd User LspAttached call s:OnLspBuffer()
augroup END

But probably using something like what is used in vim-lsp would be more user friendly.

I tried reproducing this problem by using "Plug" to load the LSP plugin and registering the Rust language server. When I opened a file from the command line, the language server was started properly for the file and the functionality worked. Do you have a list of steps to reproduce this issue (include the name of the language server used, any related .vimrc settings/configuration and the command line used to start Vim)?

saccarosium commented 7 months ago

Sorry, it wasn't a bug report. Was a possible solution.

yegappan commented 7 months ago

I have committed https://github.com/yegappan/lsp/commit/87189faf0bd4a2f974408df2d8c261dbfc3348dd to add support for the LspSetup User autocmd. Can you try using that instead of VimEnter?

saccarosium commented 7 months ago

On my end works great.