utils.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. ---always selects null-ls if it's available and caches the value per buffer
  120. ---@param client table client attached to a buffer
  121. ---@return boolean if client matches
  122. function M.format_filter(client)
  123. local filetype = vim.bo.filetype
  124. local n = require "null-ls"
  125. local s = require "null-ls.sources"
  126. local method = n.methods.FORMATTING
  127. local avalable_formatters = s.get_available(filetype, method)
  128. if #avalable_formatters > 0 then
  129. return client.name == "null-ls"
  130. elseif client.supports_method "textDocument/formatting" then
  131. return true
  132. else
  133. return false
  134. end
  135. end
  136. ---Provide vim.lsp.buf.format for nvim <0.8
  137. ---@param opts table
  138. function M.format(opts)
  139. opts = opts or {}
  140. opts.filter = opts.filter or M.format_filter
  141. if vim.lsp.buf.format then
  142. return vim.lsp.buf.format(opts)
  143. end
  144. local bufnr = opts.bufnr or vim.api.nvim_get_current_buf()
  145. ---@type table|nil
  146. local clients = vim.lsp.get_active_clients {
  147. id = opts.id,
  148. bufnr = bufnr,
  149. name = opts.name,
  150. }
  151. if opts.filter then
  152. clients = vim.tbl_filter(opts.filter, clients)
  153. end
  154. clients = vim.tbl_filter(function(client)
  155. return client.supports_method "textDocument/formatting"
  156. end, clients)
  157. if #clients == 0 then
  158. vim.notify_once "[LSP] Format request failed, no matching language servers."
  159. end
  160. local timeout_ms = opts.timeout_ms or 1000
  161. for _, client in pairs(clients) do
  162. local params = vim.lsp.util.make_formatting_params(opts.formatting_options)
  163. local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, bufnr)
  164. if result and result.result then
  165. vim.lsp.util.apply_text_edits(result.result, bufnr, client.offset_encoding)
  166. elseif err then
  167. vim.notify(string.format("[LSP][%s] %s", client.name, err), vim.log.levels.WARN)
  168. end
  169. end
  170. end
  171. return M