handlers.lua 3.6 KB

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