Skip to content
This repository has been archived by the owner on Jul 6, 2021. It is now read-only.

Always use popup instead of virtual text #29

Open
nhooyr opened this issue Jun 3, 2020 · 16 comments
Open

Always use popup instead of virtual text #29

nhooyr opened this issue Jun 3, 2020 · 16 comments

Comments

@nhooyr
Copy link

nhooyr commented Jun 3, 2020

Would be nice to have an option for this.

At the moment the popup only shows up jump.

@haorenW1025
Copy link
Collaborator

You can easily do this with

autocmd CursorHold * lua vim.lsp.util.show_line_diagnostics()

@nhooyr
Copy link
Author

nhooyr commented Jun 3, 2020

Thank you!

Def should be an option I'd say. Much better experience imo than the virtual text.

@lithammer
Copy link

lithammer commented Jun 3, 2020

Expanding with a more realistic example for anyone else coming here (i.e. add it to the on_attach callback) so that it's only enabled for buffers with an actual language server.

local nvim_command = vim.api.nvim_command

local on_attach = function(client, bufnr)
  nvim_command('autocmd CursorHold <buffer> lua vim.lsp.util.show_line_diagnostics()')
end

nvim_lsp.foobar.setup { on_attach = on_attach }

@nhooyr
Copy link
Author

nhooyr commented Jun 3, 2020

So it works for me if I run lua vim.lsp.util.show_line_diagnostics() but doesn't work as in the autocmd for some reason.

My LSP configuration:

function! s:lsp() abort
  lua << EOF
  local lsp = require 'nvim_lsp'
  local diagnostic = require 'diagnostic'
  local nvim_command = vim.api.nvim_command

  lsp.gopls.setup{ on_attach = diagnostic.on_attach }
  lsp.tsserver.setup{ on_attach = diagnostic.on_attach }
  lsp.vimls.setup{ on_attach = diagnostic.on_attach }
EOF

  inoremap <silent> <M-f> <C-x><C-f>
  inoremap <silent> <M-g> <C-x><C-o>
  set completeopt=menuone,longest,noselect
  set pumheight=10

  function! s:b_lsp() abort
    nnoremap <silent> <buffer> gd    <cmd>lua vim.lsp.buf.declaration()<CR>
    nnoremap <silent> <buffer> <c-]> <cmd>lua vim.lsp.buf.definition()<CR>
    nnoremap <silent> <buffer> K     <cmd>lua vim.lsp.buf.hover()<CR>
    nnoremap <silent> <buffer> gD    <cmd>lua vim.lsp.buf.implementation()<CR>
    nnoremap <silent> <buffer> <c-k> <cmd>lua vim.lsp.buf.signature_help()<CR>
    nnoremap <silent> <buffer> 1gD   <cmd>lua vim.lsp.buf.type_definition()<CR>
    nnoremap <silent> <buffer> gr    <cmd>lua vim.lsp.buf.references()<CR>
    nnoremap <silent> <buffer> g0    <cmd>lua vim.lsp.buf.document_symbol()<CR>
    nnoremap <silent> <buffer> gW    <cmd>lua vim.lsp.buf.workspace_symbol()<CR>
    setlocal omnifunc=v:lua.vim.lsp.omnifunc
  endfunction

  augroup nhooyr_lsp
    autocmd!
    autocmd FileType go,vim,typescript* call s:b_lsp()
    autocmd CursorHold * lua vim.lsp.util.show_line_diagnostics()
  augroup END                                                     
endfunction  
call s:lsp()

@lithammer
Copy link

Are you sure though? Because CursorHold can be quite slow. It uses 'updatetime' which defaults to 4000ms.

@nhooyr
Copy link
Author

nhooyr commented Jun 3, 2020

Positive. My update time is 100ms

@nhooyr
Copy link
Author

nhooyr commented Jun 3, 2020

Switched to:

    autocmd CursorHold * echo 'meow' | lua vim.lsp.util.show_line_diagnostics()

I see the echo but no diagnostics. And manually running does show them.

@lithammer
Copy link

Try disabling diagnostic-nvim. Might be some conflict there.

@nhooyr
Copy link
Author

nhooyr commented Jun 3, 2020

Oh my bad, I thought it was part of the plugin.

@nhooyr
Copy link
Author

nhooyr commented Jun 3, 2020

Yup works now!

How do I disable the virtual text now from native lsp?

@nhooyr
Copy link
Author

nhooyr commented Jun 3, 2020

Guess the answer is somewhere in this plugin!

@lithammer
Copy link

lithammer commented Jun 3, 2020

Something like this maybe:

do
  local diagnostic_ns = vim.api.nvim_create_namespace('vim_lsp_diagnostics')
  local default_callback = vim.lsp.callbacks['textDocument/publishDiagnostics']

  vim.lsp.callbacks['textDocument/publishDiagnostics'] = function(...)
    default_callback(...)

    local _, _, result = ...
    local bufnr = vim.uri_to_bufnr(result.uri)
    api.nvim_buf_clear_namespace(bufnr, diagnostic_ns, 0, -1)
  end
end

You can use the default callback as inspiration: https://github.com/neovim/neovim/blob/60c581b35db439dd6b32cdc2ebe1a5aed933b44c/runtime/lua/vim/lsp/callbacks.lua#L67-L92

@nhooyr
Copy link
Author

nhooyr commented Jun 3, 2020

Works perfectly now. Not really sure what I did to fix it but must have been some user error.

function! s:lsp() abort
  lua << EOF
  local lsp = require 'nvim_lsp'
  local on_attach = function(client)
    require'diagnostic'.on_attach()
    require'completion'.on_attach()
  end

  lsp.gopls.setup{ on_attach = on_attach }
  lsp.tsserver.setup{ on_attach = on_attach }
  lsp.vimls.setup{ on_attach = on_attach }
EOF

  inoremap <silent> <M-x> <C-x>
  set completeopt=menuone,longest,noselect
  set pumheight=10

  let g:diagnostic_insert_delay = 1
  let g:completion_enable_snippet = 'Neosnippet'
  imap <C-k> <cmd>lua require'source'.nextCompletion()<CR>

  function! s:b_lsp() abort
    nnoremap <silent> <buffer> gd    <cmd>lua vim.lsp.buf.declaration()<CR>
    nnoremap <silent> <buffer> <C-]> <cmd>lua vim.lsp.buf.definition()<CR>
    nnoremap <silent> <buffer> K     <cmd>lua vim.lsp.buf.hover()<CR>
    nnoremap <silent> <buffer> gD    <cmd>lua vim.lsp.buf.implementation()<CR>
    nnoremap <silent> <buffer> <C-k> <cmd>lua vim.lsp.buf.signature_help()<CR>
    nnoremap <silent> <buffer> 1gD   <cmd>lua vim.lsp.buf.type_definition()<CR>
    nnoremap <silent> <buffer> gr    <cmd>lua vim.lsp.buf.references()<CR>
    nnoremap <silent> <buffer> g0    <cmd>lua vim.lsp.buf.document_symbol()<CR>
    nnoremap <silent> <buffer> gW    <cmd>lua vim.lsp.buf.workspace_symbol()<CR>
    setlocal omnifunc=v:lua.vim.lsp.omnifunc
  endfunction

  augroup lsp
    autocmd!
    autocmd FileType go,vim,typescript* call s:b_lsp()
    autocmd CursorHold * lua vim.lsp.util.show_line_diagnostics()
    autocmd BufEnter * lua require'completion'.on_attach()
  augroup END
endfunction
call s:lsp()

nhooyr added a commit to nhooyr/dotfiles that referenced this issue Jun 3, 2020
nhooyr added a commit to nhooyr/dotfiles that referenced this issue Jul 18, 2020
@desprit
Copy link

desprit commented Apr 14, 2021

Is it possible to show diagnostics window only if a popup isn't opened already?
When I hover a variable and do lua vim.lsp.buf.hover() I see the popup which after 300ms gets overridden by Diagnostics popup.

@ldelossa
Copy link

yes same concern as @desprit

Looks like diag messages were originally geared toward virtual text and handling a "popup only" display of diag messages is kinda rought.

Along with Desprit's concer, I use and to move around diag messages. This breaks if you begin to use CursorHold to have your messages pop up. Those key strokes will place you inside the actual pop-up menu, not allowing you to jump to the next diag message.

@jemag
Copy link

jemag commented Jun 20, 2021

This repository is deprecated by the way.

For your concern of CursorHold placing you inside the pop-up menu @ldelossa , you can use :
lua vim.lsp.diagnostic.show_line_diagnostics({focusable = false})

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants