handlers.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. local ok, vim_diag = pcall(require, "vim.diagnostic")
  21. if ok then
  22. -- FIX: why can't we just use vim.diagnostic.get(buf_id)?
  23. config.signs = true
  24. for i, diagnostic in ipairs(diagnostics) do
  25. local rng = diagnostic.range
  26. diagnostics[i].lnum = rng["start"].line
  27. diagnostics[i].end_lnum = rng["end"].line
  28. diagnostics[i].col = rng["start"].character
  29. diagnostics[i].end_col = rng["end"].character
  30. end
  31. local namespace = vim.lsp.diagnostic.get_namespace(ctx.client_id)
  32. vim_diag.set(namespace, bufnr, diagnostics, config)
  33. if not vim.api.nvim_buf_is_loaded(bufnr) then
  34. return
  35. end
  36. vim_diag.show(namespace, bufnr, diagnostics, config)
  37. else
  38. vim.lsp.diagnostic.save(diagnostics, bufnr, ctx.client_id)
  39. if not vim.api.nvim_buf_is_loaded(bufnr) then
  40. return
  41. end
  42. vim.lsp.diagnostic.display(diagnostics, bufnr, ctx.client_id, config)
  43. end
  44. end
  45. else
  46. vim.lsp.handlers["textDocument/publishDiagnostics"] = function(_, _, params, client_id, _)
  47. local uri = params.uri
  48. local bufnr = vim.uri_to_bufnr(uri)
  49. if not bufnr then
  50. return
  51. end
  52. local diagnostics = params.diagnostics
  53. vim.lsp.diagnostic.save(diagnostics, bufnr, client_id)
  54. if not vim.api.nvim_buf_is_loaded(bufnr) then
  55. return
  56. end
  57. vim.lsp.diagnostic.display(diagnostics, bufnr, client_id, config)
  58. end
  59. end
  60. vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
  61. border = lvim.lsp.popup_border,
  62. })
  63. vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, {
  64. border = lvim.lsp.popup_border,
  65. })
  66. end
  67. local function split_by_chunk(text, chunkSize)
  68. local s = {}
  69. for i = 1, #text, chunkSize do
  70. s[#s + 1] = text:sub(i, i + chunkSize - 1)
  71. end
  72. return s
  73. end
  74. function M.show_line_diagnostics()
  75. -- TODO: replace all this with vim.diagnostic.show_position_diagnostics()
  76. local diagnostics = vim.lsp.diagnostic.get_line_diagnostics()
  77. local severity_highlight = {
  78. "LspDiagnosticsFloatingError",
  79. "LspDiagnosticsFloatingWarning",
  80. "LspDiagnosticsFloatingInformation",
  81. "LspDiagnosticsFloatingHint",
  82. }
  83. local ok, vim_diag = pcall(require, "vim.diagnostic")
  84. if ok then
  85. local buf_id = vim.api.nvim_win_get_buf(0)
  86. local win_id = vim.api.nvim_get_current_win()
  87. local cursor_position = vim.api.nvim_win_get_cursor(win_id)
  88. severity_highlight = {
  89. "DiagnosticFloatingError",
  90. "DiagnosticFloatingWarn",
  91. "DiagnosticFloatingInfo",
  92. "DiagnosticFloatingHint",
  93. }
  94. diagnostics = vim_diag.get(buf_id, { lnum = cursor_position[1] - 1 })
  95. end
  96. local lines = {}
  97. local max_width = vim.fn.winwidth(0) - 5
  98. local height = #diagnostics
  99. local width = 0
  100. local opts = {}
  101. local close_events = { "CursorMoved", "CursorMovedI", "BufHidden", "InsertCharPre" }
  102. if height == 0 then
  103. return
  104. end
  105. local bufnr = vim.api.nvim_create_buf(false, true)
  106. local diag_message
  107. table.sort(diagnostics, function(a, b)
  108. return a.severity < b.severity
  109. end)
  110. local hash = {}
  111. local diagnostics_no_dupes = {}
  112. for _, v in ipairs(diagnostics) do
  113. if not hash[v["message"]] then
  114. diagnostics_no_dupes[#diagnostics_no_dupes + 1] = v -- you could print here instead of saving to result table if you wanted
  115. hash[v["message"]] = true
  116. end
  117. end
  118. -- print(vim.inspect(diagnostics_no_dupes))
  119. for i, diagnostic in ipairs(diagnostics_no_dupes) do
  120. local source = diagnostic.source
  121. diag_message = diagnostic.message:gsub("[\n\r]", " ")
  122. if source then
  123. if string.find(source, "/") then
  124. source = string.sub(diagnostic.source, string.find(diagnostic.source, "([%w-_]+)$"))
  125. end
  126. diag_message = string.format("%d. %s: %s", i, source, diag_message)
  127. else
  128. diag_message = string.format("%d. %s", i, diag_message)
  129. end
  130. if diagnostic.code then
  131. diag_message = string.format("%s [%s]", diag_message, diagnostic.code)
  132. end
  133. local msgs = split_by_chunk(diag_message, max_width)
  134. for _, diag in ipairs(msgs) do
  135. table.insert(lines, { message = diag, severity = diagnostic.severity })
  136. width = math.max(diag:len(), width)
  137. end
  138. end
  139. height = #lines
  140. opts = vim.lsp.util.make_floating_popup_options(width, height, opts)
  141. opts["style"] = "minimal"
  142. opts["border"] = "rounded"
  143. opts["focusable"] = true
  144. vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
  145. local winnr = vim.api.nvim_open_win(bufnr, false, opts)
  146. vim.api.nvim_win_set_option(winnr, "winblend", 0)
  147. vim.api.nvim_buf_set_var(bufnr, "lsp_floating_window", winnr)
  148. for i, diag in ipairs(lines) do
  149. vim.api.nvim_buf_set_lines(bufnr, i - 1, i - 1, 0, { diag.message })
  150. vim.api.nvim_buf_add_highlight(bufnr, -1, severity_highlight[diag.severity], i - 1, 0, diag.message:len())
  151. end
  152. vim.api.nvim_command(
  153. "autocmd QuitPre <buffer> ++nested ++once lua pcall(vim.api.nvim_win_close, " .. winnr .. ", true)"
  154. )
  155. vim.lsp.util.close_preview_autocmd(close_events, winnr)
  156. end
  157. return M