utils.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. local M = {}
  2. local tbl = require "lvim.utils.table"
  3. function M.is_client_active(name)
  4. local clients = vim.lsp.get_active_clients()
  5. return tbl.find_first(clients, function(client)
  6. return client.name == name
  7. end)
  8. end
  9. function M.get_active_clients_by_ft(filetype)
  10. local matches = {}
  11. local clients = vim.lsp.get_active_clients()
  12. for _, client in pairs(clients) do
  13. local supported_filetypes = client.config.filetypes or {}
  14. if client.name ~= "null-ls" and vim.tbl_contains(supported_filetypes, filetype) then
  15. table.insert(matches, client)
  16. end
  17. end
  18. return matches
  19. end
  20. function M.get_client_capabilities(client_id)
  21. local client
  22. if not client_id then
  23. local buf_clients = vim.lsp.buf_get_clients()
  24. for _, buf_client in pairs(buf_clients) do
  25. if buf_client.name ~= "null-ls" then
  26. client = buf_client
  27. break
  28. end
  29. end
  30. else
  31. client = vim.lsp.get_client_by_id(tonumber(client_id))
  32. end
  33. if not client then
  34. error "Unable to determine client_id"
  35. return
  36. end
  37. local enabled_caps = {}
  38. for capability, status in pairs(client.server_capabilities or client.resolved_capabilities) do
  39. if status == true then
  40. table.insert(enabled_caps, capability)
  41. end
  42. end
  43. return enabled_caps
  44. end
  45. ---Get supported filetypes per server
  46. ---@param server_name string can be any server supported by nvim-lsp-installer
  47. ---@return table supported filestypes as a list of strings
  48. function M.get_supported_filetypes(server_name)
  49. local status_ok, lsp_installer_servers = pcall(require, "nvim-lsp-installer.servers")
  50. if not status_ok then
  51. return {}
  52. end
  53. local server_available, requested_server = lsp_installer_servers.get_server(server_name)
  54. if not server_available then
  55. return {}
  56. end
  57. return requested_server:get_supported_filetypes()
  58. end
  59. ---Get supported servers per filetype
  60. ---@param filetype string
  61. ---@return table list of names of supported servers
  62. function M.get_supported_servers_per_filetype(filetype)
  63. local filetype_server_map = require "nvim-lsp-installer._generated.filetype_map"
  64. return filetype_server_map[filetype]
  65. end
  66. ---Get all supported filetypes by nvim-lsp-installer
  67. ---@return table supported filestypes as a list of strings
  68. function M.get_all_supported_filetypes()
  69. local status_ok, lsp_installer_filetypes = pcall(require, "nvim-lsp-installer._generated.filetype_map")
  70. if not status_ok then
  71. return {}
  72. end
  73. return vim.tbl_keys(lsp_installer_filetypes or {})
  74. end
  75. function M.setup_document_highlight(client, bufnr)
  76. local status_ok, highlight_supported = pcall(function()
  77. return client.supports_method "textDocument/documentHighlight"
  78. end)
  79. if not status_ok or not highlight_supported then
  80. return
  81. end
  82. local augroup_exist, _ = pcall(vim.api.nvim_get_autocmds, {
  83. group = "lsp_document_highlight",
  84. })
  85. if not augroup_exist then
  86. vim.api.nvim_create_augroup("lsp_document_highlight", {})
  87. end
  88. vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
  89. group = "lsp_document_highlight",
  90. buffer = bufnr,
  91. callback = vim.lsp.buf.document_highlight,
  92. })
  93. vim.api.nvim_create_autocmd("CursorMoved", {
  94. group = "lsp_document_highlight",
  95. buffer = bufnr,
  96. callback = vim.lsp.buf.clear_references,
  97. })
  98. end
  99. function M.setup_codelens_refresh(client, bufnr)
  100. local status_ok, codelens_supported = pcall(function()
  101. return client.supports_method "textDocument/codeLens"
  102. end)
  103. if not status_ok or not codelens_supported then
  104. return
  105. end
  106. local augroup_exist, _ = pcall(vim.api.nvim_get_autocmds, {
  107. group = "lsp_code_lens_refresh",
  108. })
  109. if not augroup_exist then
  110. vim.api.nvim_create_augroup("lsp_code_lens_refresh", {})
  111. end
  112. vim.api.nvim_create_autocmd({ "BufEnter", "InsertLeave" }, {
  113. group = "lsp_code_lens_refresh",
  114. buffer = bufnr,
  115. callback = vim.lsp.codelens.refresh,
  116. })
  117. end
  118. ---filter passed to vim.lsp.buf.format
  119. ---gives higher priority to null-ls
  120. ---@param clients table clients attached to a buffer
  121. ---@return table chosen clients
  122. function M.format_filter(clients)
  123. return vim.tbl_filter(function(client)
  124. local status_ok, formatting_supported = pcall(function()
  125. return client.supports_method "textDocument/formatting"
  126. end)
  127. -- give higher prio to null-ls
  128. if status_ok and formatting_supported and client.name == "null-ls" then
  129. return "null-ls"
  130. else
  131. return status_ok and formatting_supported and client.name
  132. end
  133. end, clients)
  134. end
  135. ---Provide vim.lsp.buf.format for nvim <0.8
  136. ---@param opts table
  137. function M.format(opts)
  138. opts = opts or { filter = M.format_filter }
  139. if vim.lsp.buf.format then
  140. return vim.lsp.buf.format(opts)
  141. end
  142. local bufnr = opts.bufnr or vim.api.nvim_get_current_buf()
  143. local clients = vim.lsp.buf_get_clients(bufnr)
  144. if opts.filter then
  145. clients = opts.filter(clients)
  146. elseif opts.id then
  147. clients = vim.tbl_filter(function(client)
  148. return client.id == opts.id
  149. end, clients)
  150. elseif opts.name then
  151. clients = vim.tbl_filter(function(client)
  152. return client.name == opts.name
  153. end, clients)
  154. end
  155. clients = vim.tbl_filter(function(client)
  156. return client.supports_method "textDocument/formatting"
  157. end, clients)
  158. if #clients == 0 then
  159. vim.notify_once "[LSP] Format request failed, no matching language servers."
  160. end
  161. local timeout_ms = opts.timeout_ms or 1000
  162. for _, client in pairs(clients) do
  163. local params = vim.lsp.util.make_formatting_params(opts.formatting_options)
  164. local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, bufnr)
  165. if result and result.result then
  166. vim.lsp.util.apply_text_edits(result.result, bufnr, client.offset_encoding)
  167. elseif err then
  168. vim.notify(string.format("[LSP][%s] %s", client.name, err), vim.log.levels.WARN)
  169. end
  170. end
  171. end
  172. return M