utils.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. local M = {}
  2. local tbl = require "lvim.utils.table"
  3. local Log = require "lvim.core.log"
  4. function M.is_client_active(name)
  5. local clients = vim.lsp.get_clients()
  6. return tbl.find_first(clients, function(client)
  7. return client.name == name
  8. end)
  9. end
  10. function M.get_active_clients_by_ft(filetype)
  11. local matches = {}
  12. local clients = vim.lsp.get_clients()
  13. for _, client in pairs(clients) do
  14. local supported_filetypes = client.config.filetypes or {}
  15. if client.name ~= "null-ls" and vim.tbl_contains(supported_filetypes, filetype) then
  16. table.insert(matches, client)
  17. end
  18. end
  19. return matches
  20. end
  21. function M.get_client_capabilities(client_id)
  22. local client = vim.lsp.get_client_by_id(tonumber(client_id))
  23. if not client then
  24. Log:warn("Unable to determine client from client_id: " .. client_id)
  25. return
  26. end
  27. local enabled_caps = {}
  28. for capability, status in pairs(client.server_capabilities) do
  29. if status == true then
  30. table.insert(enabled_caps, capability)
  31. end
  32. end
  33. return enabled_caps
  34. end
  35. ---Get supported filetypes per server
  36. ---@param server_name string can be any server supported by nvim-lsp-installer
  37. ---@return string[] supported filestypes as a list of strings
  38. function M.get_supported_filetypes(server_name)
  39. local status_ok, config = pcall(require, ("lspconfig.server_configurations.%s"):format(server_name))
  40. if not status_ok then
  41. return {}
  42. end
  43. return config.default_config.filetypes or {}
  44. end
  45. ---Get supported servers per filetype
  46. ---@param filter { filetype: string | string[] }?: (optional) Used to filter the list of server names.
  47. ---@return string[] list of names of supported servers
  48. function M.get_supported_servers(filter)
  49. -- force synchronous mode, see: |mason-registry.refresh()|
  50. require("mason-registry").refresh()
  51. require("mason-registry").get_all_packages()
  52. local _, supported_servers = pcall(function()
  53. return require("mason-lspconfig").get_available_servers(filter)
  54. end)
  55. return supported_servers or {}
  56. end
  57. ---Get all supported filetypes by nvim-lsp-installer
  58. ---@return string[] supported filestypes as a list of strings
  59. function M.get_all_supported_filetypes()
  60. local status_ok, filetype_server_map = pcall(require, "mason-lspconfig.mappings.filetype")
  61. if not status_ok then
  62. return {}
  63. end
  64. return vim.tbl_keys(filetype_server_map or {})
  65. end
  66. function M.setup_document_highlight(client, bufnr)
  67. if lvim.builtin.illuminate.active then
  68. Log:debug "skipping setup for document_highlight, illuminate already active"
  69. return
  70. end
  71. local status_ok, highlight_supported = pcall(function()
  72. return client.supports_method "textDocument/documentHighlight"
  73. end)
  74. if not status_ok or not highlight_supported then
  75. return
  76. end
  77. local group = "lsp_document_highlight"
  78. local hl_events = { "CursorHold", "CursorHoldI" }
  79. local ok, hl_autocmds = pcall(vim.api.nvim_get_autocmds, {
  80. group = group,
  81. buffer = bufnr,
  82. event = hl_events,
  83. })
  84. if ok and #hl_autocmds > 0 then
  85. return
  86. end
  87. vim.api.nvim_create_augroup(group, { clear = false })
  88. vim.api.nvim_create_autocmd(hl_events, {
  89. group = group,
  90. buffer = bufnr,
  91. callback = vim.lsp.buf.document_highlight,
  92. })
  93. vim.api.nvim_create_autocmd("CursorMoved", {
  94. group = group,
  95. buffer = bufnr,
  96. callback = vim.lsp.buf.clear_references,
  97. })
  98. end
  99. function M.setup_document_symbols(client, bufnr)
  100. vim.g.navic_silence = false -- can be set to true to suppress error
  101. local symbols_supported = client.supports_method "textDocument/documentSymbol"
  102. if not symbols_supported then
  103. Log:debug("skipping setup for document_symbols, method not supported by " .. client.name)
  104. return
  105. end
  106. local status_ok, navic = pcall(require, "nvim-navic")
  107. if status_ok then
  108. navic.attach(client, bufnr)
  109. end
  110. end
  111. function M.setup_codelens_refresh(client, bufnr)
  112. local status_ok, codelens_supported = pcall(function()
  113. return client.supports_method "textDocument/codeLens"
  114. end)
  115. if not status_ok or not codelens_supported then
  116. return
  117. end
  118. local group = "lsp_code_lens_refresh"
  119. local cl_events = { "BufEnter", "InsertLeave" }
  120. local ok, cl_autocmds = pcall(vim.api.nvim_get_autocmds, {
  121. group = group,
  122. buffer = bufnr,
  123. event = cl_events,
  124. })
  125. if ok and #cl_autocmds > 0 then
  126. return
  127. end
  128. vim.api.nvim_create_augroup(group, { clear = false })
  129. vim.api.nvim_create_autocmd(cl_events, {
  130. group = group,
  131. buffer = bufnr,
  132. callback = function()
  133. vim.lsp.codelens.refresh { bufnr = bufnr }
  134. end,
  135. })
  136. end
  137. ---filter passed to vim.lsp.buf.format
  138. ---always selects null-ls if it's available and caches the value per buffer
  139. ---@param client table client attached to a buffer
  140. ---@return boolean if client matches
  141. function M.format_filter(client)
  142. local filetype = vim.bo.filetype
  143. local n = require "null-ls"
  144. local s = require "null-ls.sources"
  145. local method = n.methods.FORMATTING
  146. local available_formatters = s.get_available(filetype, method)
  147. if #available_formatters > 0 then
  148. return client.name == "null-ls"
  149. elseif client.supports_method "textDocument/formatting" then
  150. return true
  151. else
  152. return false
  153. end
  154. end
  155. ---Simple wrapper for vim.lsp.buf.format() to provide defaults
  156. ---@param opts table|nil
  157. function M.format(opts)
  158. opts = opts or {}
  159. opts.filter = opts.filter or M.format_filter
  160. return vim.lsp.buf.format(opts)
  161. end
  162. return M