utils.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 augroup_exist, _ = pcall(vim.api.nvim_get_autocmds, {
  81. group = "lsp_document_highlight",
  82. })
  83. if not augroup_exist then
  84. vim.api.nvim_create_augroup("lsp_document_highlight", {})
  85. end
  86. vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
  87. group = "lsp_document_highlight",
  88. buffer = bufnr,
  89. callback = vim.lsp.buf.document_highlight,
  90. })
  91. vim.api.nvim_create_autocmd("CursorMoved", {
  92. group = "lsp_document_highlight",
  93. buffer = bufnr,
  94. callback = vim.lsp.buf.clear_references,
  95. })
  96. end
  97. function M.setup_codelens_refresh(client, bufnr)
  98. local status_ok, codelens_supported = pcall(function()
  99. return client.supports_method "textDocument/codeLens"
  100. end)
  101. if not status_ok or not codelens_supported then
  102. return
  103. end
  104. local augroup_exist, _ = pcall(vim.api.nvim_get_autocmds, {
  105. group = "lsp_code_lens_refresh",
  106. })
  107. if not augroup_exist then
  108. vim.api.nvim_create_augroup("lsp_code_lens_refresh", {})
  109. end
  110. vim.api.nvim_create_autocmd({ "BufEnter", "InsertLeave" }, {
  111. group = "lsp_code_lens_refresh",
  112. buffer = bufnr,
  113. callback = vim.lsp.codelens.refresh,
  114. })
  115. end
  116. ---filter passed to vim.lsp.buf.format
  117. ---always selects null-ls if it's available and caches the value per buffer
  118. ---@param client table client attached to a buffer
  119. ---@return boolean if client matches
  120. function M.format_filter(client)
  121. local filetype = vim.bo.filetype
  122. local n = require "null-ls"
  123. local s = require "null-ls.sources"
  124. local method = n.methods.FORMATTING
  125. local avalable_formatters = s.get_available(filetype, method)
  126. if #avalable_formatters > 0 then
  127. return client.name == "null-ls"
  128. elseif client.supports_method "textDocument/formatting" then
  129. return true
  130. else
  131. return false
  132. end
  133. end
  134. ---Provide vim.lsp.buf.format for nvim <0.8
  135. ---@param opts table
  136. function M.format(opts)
  137. opts = opts or {}
  138. opts.filter = opts.filter or 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. ---@type table|nil
  144. local clients = vim.lsp.get_active_clients {
  145. id = opts.id,
  146. bufnr = bufnr,
  147. name = opts.name,
  148. }
  149. if opts.filter then
  150. clients = vim.tbl_filter(opts.filter, clients)
  151. end
  152. clients = vim.tbl_filter(function(client)
  153. return client.supports_method "textDocument/formatting"
  154. end, clients)
  155. if #clients == 0 then
  156. vim.notify_once "[LSP] Format request failed, no matching language servers."
  157. end
  158. local timeout_ms = opts.timeout_ms or 1000
  159. for _, client in pairs(clients) do
  160. local params = vim.lsp.util.make_formatting_params(opts.formatting_options)
  161. local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, bufnr)
  162. if result and result.result then
  163. vim.lsp.util.apply_text_edits(result.result, bufnr, client.offset_encoding)
  164. elseif err then
  165. vim.notify(string.format("[LSP][%s] %s", client.name, err), vim.log.levels.WARN)
  166. end
  167. end
  168. end
  169. return M