nvim-lua / diagnostic-nvim

A wrapper for neovim built in LSP diagnosis config
Apache License 2.0
219 stars 15 forks source link

Always use popup instead of virtual text #29

Open nhooyr opened 4 years ago

nhooyr commented 4 years ago

Would be nice to have an option for this.

At the moment the popup only shows up jump.

haorenW1025 commented 4 years ago

You can easily do this with

autocmd CursorHold * lua vim.lsp.util.show_line_diagnostics()
nhooyr commented 4 years ago

Thank you!

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

lithammer commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

nhooyr commented 4 years ago

Positive. My update time is 100ms

nhooyr commented 4 years ago

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 commented 4 years ago

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

nhooyr commented 4 years ago

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

nhooyr commented 4 years ago

Yup works now!

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

nhooyr commented 4 years ago

Guess the answer is somewhere in this plugin!

lithammer commented 4 years ago

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 commented 4 years ago

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()
desprit commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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})