utils.lua 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 string[] supported filestypes as a list of strings
  48. function M.get_supported_filetypes(server_name)
  49. local status_ok, config = pcall(require, ("lspconfig.server_configurations.%s"):format(server_name))
  50. if not status_ok then
  51. return {}
  52. end
  53. return config.default_config.filetypes or {}
  54. end
  55. ---Get supported servers per filetype
  56. ---@param filter { filetype: string | string[] }?: (optional) Used to filter the list of server names.
  57. ---@return string[] list of names of supported servers
  58. function M.get_supported_servers(filter)
  59. local _, supported_servers = pcall(function()
  60. return require("mason-lspconfig").get_available_servers(filter)
  61. end)
  62. return supported_servers or {}
  63. end
  64. ---Get all supported filetypes by nvim-lsp-installer
  65. ---@return string[] supported filestypes as a list of strings
  66. function M.get_all_supported_filetypes()
  67. local status_ok, filetype_server_map = pcall(require, "mason-lspconfig.mappings.filetype")
  68. if not status_ok then
  69. return {}
  70. end
  71. return vim.tbl_keys(filetype_server_map or {})
  72. end
  73. function M.setup_document_highlight(client, bufnr)
  74. local status_ok, highlight_supported = pcall(function()
  75. return client.supports_method "textDocument/documentHighlight"
  76. end)
  77. if not status_ok or not highlight_supported then
  78. return
  79. end
  80. local group = "lsp_document_highlight"
  81. local hl_events = { "CursorHold", "CursorHoldI" }
  82. local ok, hl_autocmds = pcall(vim.api.nvim_get_autocmds, {
  83. group = group,
  84. buffer = bufnr,
  85. event = hl_events,
  86. })
  87. if ok and #hl_autocmds > 0 then
  88. return
  89. end
  90. vim.api.nvim_create_augroup(group, { clear = false })
  91. vim.api.nvim_create_autocmd(hl_events, {
  92. group = group,
  93. buffer = bufnr,
  94. callback = vim.lsp.buf.document_highlight,
  95. })
  96. vim.api.nvim_create_autocmd("CursorMoved", {
  97. group = group,
  98. buffer = bufnr,
  99. callback = vim.lsp.buf.clear_references,
  100. })
  101. end
  102. function M.setup_codelens_refresh(client, bufnr)
  103. local status_ok, codelens_supported = pcall(function()
  104. return client.supports_method "textDocument/codeLens"
  105. end)
  106. if not status_ok or not codelens_supported then
  107. return
  108. end
  109. local group = "lsp_code_lens_refresh"
  110. local cl_events = { "BufEnter", "InsertLeave" }
  111. local ok, cl_autocmds = pcall(vim.api.nvim_get_autocmds, {
  112. group = group,
  113. buffer = bufnr,
  114. event = cl_events,
  115. })
  116. if ok and #cl_autocmds > 0 then
  117. return
  118. end
  119. vim.api.nvim_create_augroup(group, { clear = false })
  120. vim.api.nvim_create_autocmd(cl_events, {
  121. group = group,
  122. buffer = bufnr,
  123. callback = vim.lsp.codelens.refresh,
  124. })
  125. end
  126. ---filter passed to vim.lsp.buf.format
  127. ---always selects null-ls if it's available and caches the value per buffer
  128. ---@param client table client attached to a buffer
  129. ---@return boolean if client matches
  130. function M.format_filter(client)
  131. local filetype = vim.bo.filetype
  132. local n = require "null-ls"
  133. local s = require "null-ls.sources"
  134. local method = n.methods.FORMATTING
  135. local avalable_formatters = s.get_available(filetype, method)
  136. if #avalable_formatters > 0 then
  137. return client.name == "null-ls"
  138. elseif client.supports_method "textDocument/formatting" then
  139. return true
  140. else
  141. return false
  142. end
  143. end
  144. ---Provide vim.lsp.buf.format for nvim <0.8
  145. ---@param opts table
  146. function M.format(opts)
  147. opts = opts or {}
  148. opts.filter = opts.filter or M.format_filter
  149. if vim.lsp.buf.format then
  150. return vim.lsp.buf.format(opts)
  151. end
  152. local bufnr = opts.bufnr or vim.api.nvim_get_current_buf()
  153. ---@type table|nil
  154. local clients = vim.lsp.get_active_clients {
  155. id = opts.id,
  156. bufnr = bufnr,
  157. name = opts.name,
  158. }
  159. if opts.filter then
  160. clients = vim.tbl_filter(opts.filter, clients)
  161. end
  162. clients = vim.tbl_filter(function(client)
  163. return client.supports_method "textDocument/formatting"
  164. end, clients)
  165. if #clients == 0 then
  166. vim.notify_once "[LSP] Format request failed, no matching language servers."
  167. end
  168. local timeout_ms = opts.timeout_ms or 1000
  169. for _, client in pairs(clients) do
  170. local params = vim.lsp.util.make_formatting_params(opts.formatting_options)
  171. local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, bufnr)
  172. if result and result.result then
  173. vim.lsp.util.apply_text_edits(result.result, bufnr, client.offset_encoding)
  174. elseif err then
  175. vim.notify(string.format("[LSP][%s] %s", client.name, err), vim.log.levels.WARN)
  176. end
  177. end
  178. end
  179. return M