handlers.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. float = lvim.lsp.diagnostics.float,
  12. }
  13. if vim.fn.has "nvim-0.6" == 1 then
  14. vim.diagnostic.config(config)
  15. else
  16. vim.lsp.handlers["textDocument/publishDiagnostics"] = function(_, _, params, client_id, _)
  17. local uri = params.uri
  18. local bufnr = vim.uri_to_bufnr(uri)
  19. if not bufnr then
  20. return
  21. end
  22. local diagnostics = params.diagnostics
  23. vim.lsp.diagnostic.save(diagnostics, bufnr, client_id)
  24. if not vim.api.nvim_buf_is_loaded(bufnr) then
  25. return
  26. end
  27. vim.lsp.diagnostic.display(diagnostics, bufnr, client_id, config)
  28. end
  29. vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
  30. border = lvim.lsp.popup_border,
  31. })
  32. vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, {
  33. border = lvim.lsp.popup_border,
  34. })
  35. end
  36. end
  37. function M.show_line_diagnostics()
  38. if vim.fn.has "nvim-0.6" == 1 then
  39. return vim.diagnostic.open_float(0, { scope = "line" })
  40. end
  41. local function split_by_chunk(text, chunkSize)
  42. local s = {}
  43. for i = 1, #text, chunkSize do
  44. s[#s + 1] = text:sub(i, i + chunkSize - 1)
  45. end
  46. return s
  47. end
  48. local diagnostics = vim.lsp.diagnostic.get_line_diagnostics()
  49. local severity_highlight = {
  50. "LspDiagnosticsFloatingError",
  51. "LspDiagnosticsFloatingWarning",
  52. "LspDiagnosticsFloatingInformation",
  53. "LspDiagnosticsFloatingHint",
  54. }
  55. local ok, vim_diag = pcall(require, "vim.diagnostic")
  56. if ok then
  57. local buf_id = vim.api.nvim_win_get_buf(0)
  58. local win_id = vim.api.nvim_get_current_win()
  59. local cursor_position = vim.api.nvim_win_get_cursor(win_id)
  60. severity_highlight = {
  61. "DiagnosticFloatingError",
  62. "DiagnosticFloatingWarn",
  63. "DiagnosticFloatingInfo",
  64. "DiagnosticFloatingHint",
  65. }
  66. diagnostics = vim_diag.get(buf_id, { lnum = cursor_position[1] - 1 })
  67. end
  68. local lines = {}
  69. local max_width = vim.fn.winwidth(0) - 5
  70. local height = #diagnostics
  71. local width = 0
  72. local opts = {}
  73. local close_events = { "CursorMoved", "CursorMovedI", "BufHidden", "InsertCharPre" }
  74. if height == 0 then
  75. return
  76. end
  77. local bufnr = vim.api.nvim_create_buf(false, true)
  78. local diag_message
  79. table.sort(diagnostics, function(a, b)
  80. return a.severity < b.severity
  81. end)
  82. local hash = {}
  83. local diagnostics_no_dupes = {}
  84. for _, v in ipairs(diagnostics) do
  85. if not hash[v["message"]] then
  86. diagnostics_no_dupes[#diagnostics_no_dupes + 1] = v -- you could print here instead of saving to result table if you wanted
  87. hash[v["message"]] = true
  88. end
  89. end
  90. -- print(vim.inspect(diagnostics_no_dupes))
  91. for i, diagnostic in ipairs(diagnostics_no_dupes) do
  92. local source = diagnostic.source
  93. diag_message = diagnostic.message:gsub("[\n\r]", " ")
  94. if source then
  95. if string.find(source, "/") then
  96. source = string.sub(diagnostic.source, string.find(diagnostic.source, "([%w-_]+)$"))
  97. end
  98. diag_message = string.format("%d. %s: %s", i, source, diag_message)
  99. else
  100. diag_message = string.format("%d. %s", i, diag_message)
  101. end
  102. if diagnostic.code then
  103. diag_message = string.format("%s [%s]", diag_message, diagnostic.code)
  104. end
  105. local msgs = split_by_chunk(diag_message, max_width)
  106. for _, diag in ipairs(msgs) do
  107. table.insert(lines, { message = diag, severity = diagnostic.severity })
  108. width = math.max(diag:len(), width)
  109. end
  110. end
  111. height = #lines
  112. opts = vim.lsp.util.make_floating_popup_options(width, height, opts)
  113. opts["style"] = "minimal"
  114. opts["border"] = "rounded"
  115. opts["focusable"] = true
  116. vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
  117. local winnr = vim.api.nvim_open_win(bufnr, false, opts)
  118. vim.api.nvim_win_set_option(winnr, "winblend", 0)
  119. vim.api.nvim_buf_set_var(bufnr, "lsp_floating_window", winnr)
  120. for i, diag in ipairs(lines) do
  121. vim.api.nvim_buf_set_lines(bufnr, i - 1, i - 1, 0, { diag.message })
  122. vim.api.nvim_buf_add_highlight(bufnr, -1, severity_highlight[diag.severity], i - 1, 0, diag.message:len())
  123. end
  124. vim.api.nvim_command(
  125. "autocmd QuitPre <buffer> ++nested ++once lua pcall(vim.api.nvim_win_close, " .. winnr .. ", true)"
  126. )
  127. vim.lsp.util.close_preview_autocmd(close_events, winnr)
  128. end
  129. return M