handlers.lua 4.6 KB

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