handlers.lua 3.9 KB

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