handlers.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. -- Set Default Prefix.
  2. -- Note: You can set a prefix per lsp server in the lv-globals.lua file
  3. local M = {}
  4. function M.setup()
  5. vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
  6. virtual_text = lvim.lsp.diagnostics.virtual_text,
  7. signs = lvim.lsp.diagnostics.signs.active,
  8. underline = lvim.lsp.document_highlight,
  9. })
  10. vim.lsp.handlers["textDocument/publishDiagnostics"] = function(_, _, params, client_id, _)
  11. local config = { -- your config
  12. virtual_text = lvim.lsp.diagnostics.virtual_text,
  13. signs = lvim.lsp.diagnostics.signs,
  14. underline = lvim.lsp.diagnostics.underline,
  15. update_in_insert = lvim.lsp.diagnostics.update_in_insert,
  16. severity_sort = lvim.lsp.diagnostics.severity_sort,
  17. }
  18. local uri = params.uri
  19. local bufnr = vim.uri_to_bufnr(uri)
  20. if not bufnr then
  21. return
  22. end
  23. local diagnostics = params.diagnostics
  24. vim.lsp.diagnostic.save(diagnostics, bufnr, client_id)
  25. if not vim.api.nvim_buf_is_loaded(bufnr) then
  26. return
  27. end
  28. vim.lsp.diagnostic.display(diagnostics, bufnr, client_id, config)
  29. end
  30. vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
  31. border = lvim.lsp.popup_border,
  32. })
  33. vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, {
  34. border = lvim.lsp.popup_border,
  35. })
  36. end
  37. function M.show_line_diagnostics()
  38. local diagnostics = vim.lsp.diagnostic.get_line_diagnostics()
  39. local diags = vim.deepcopy(diagnostics)
  40. local height = #diagnostics
  41. local width = 0
  42. local opts = {}
  43. local close_events = { "CursorMoved", "CursorMovedI", "BufHidden", "InsertCharPre" }
  44. local diagnostic_severities = {
  45. "Error",
  46. "Warning",
  47. "Information",
  48. "Hint",
  49. }
  50. if height == 0 then
  51. return
  52. end
  53. local bufnr = vim.api.nvim_create_buf(false, true)
  54. for i, diagnostic in ipairs(diagnostics) do
  55. local source = diagnostic.source
  56. if source then
  57. if string.find(source, "/") then
  58. source = string.sub(diagnostic.source, string.find(diagnostic.source, "([%w-_]+)$"))
  59. end
  60. diags[i].message = string.format("%s: %s", source, diagnostic.message)
  61. else
  62. diags[i].message = string.format("%s", diagnostic.message)
  63. end
  64. if diagnostic.code then
  65. diags[i].message = string.format("%s [%s]", diags[i].message, diagnostic.code)
  66. end
  67. if diags[i].message:len() > width then
  68. width = string.len(diags[i].message)
  69. end
  70. end
  71. opts = vim.lsp.util.make_floating_popup_options(width, height, opts)
  72. opts["style"] = "minimal"
  73. opts["border"] = "rounded"
  74. vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
  75. local winnr = vim.api.nvim_open_win(bufnr, false, opts)
  76. vim.api.nvim_win_set_option(winnr, "winblend", 0)
  77. vim.api.nvim_buf_set_var(bufnr, "lsp_floating_window", winnr)
  78. for i, diag in ipairs(diags) do
  79. vim.api.nvim_buf_set_lines(bufnr, i - 1, i - 1, 0, { diag.message })
  80. vim.api.nvim_buf_add_highlight(
  81. bufnr,
  82. -1,
  83. "LspDiagnosticsFloating" .. diagnostic_severities[diag.severity],
  84. i - 1,
  85. 0,
  86. diag.message:len()
  87. )
  88. end
  89. vim.api.nvim_command(
  90. "autocmd QuitPre <buffer> ++nested ++once lua pcall(vim.api.nvim_win_close, " .. winnr .. ", true)"
  91. )
  92. vim.lsp.util.close_preview_autocmd(close_events, winnr)
  93. end
  94. return M