utils.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_active_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_active_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. require("mason-registry").refresh()
  50. local _, supported_servers = pcall(function()
  51. return require("mason-lspconfig").get_available_servers(filter)
  52. end)
  53. return supported_servers or {}
  54. end
  55. ---Get all supported filetypes by nvim-lsp-installer
  56. ---@return string[] supported filestypes as a list of strings
  57. function M.get_all_supported_filetypes()
  58. local status_ok, filetype_server_map = pcall(require, "mason-lspconfig.mappings.filetype")
  59. if not status_ok then
  60. return {}
  61. end
  62. return vim.tbl_keys(filetype_server_map or {})
  63. end
  64. function M.setup_document_highlight(client, bufnr)
  65. if lvim.builtin.illuminate.active then
  66. Log:debug "skipping setup for document_highlight, illuminate already active"
  67. return
  68. end
  69. local status_ok, highlight_supported = pcall(function()
  70. return client.supports_method "textDocument/documentHighlight"
  71. end)
  72. if not status_ok or not highlight_supported then
  73. return
  74. end
  75. local group = "lsp_document_highlight"
  76. local hl_events = { "CursorHold", "CursorHoldI" }
  77. local ok, hl_autocmds = pcall(vim.api.nvim_get_autocmds, {
  78. group = group,
  79. buffer = bufnr,
  80. event = hl_events,
  81. })
  82. if ok and #hl_autocmds > 0 then
  83. return
  84. end
  85. vim.api.nvim_create_augroup(group, { clear = false })
  86. vim.api.nvim_create_autocmd(hl_events, {
  87. group = group,
  88. buffer = bufnr,
  89. callback = vim.lsp.buf.document_highlight,
  90. })
  91. vim.api.nvim_create_autocmd("CursorMoved", {
  92. group = group,
  93. buffer = bufnr,
  94. callback = vim.lsp.buf.clear_references,
  95. })
  96. end
  97. function M.setup_document_symbols(client, bufnr)
  98. vim.g.navic_silence = false -- can be set to true to suppress error
  99. local symbols_supported = client.supports_method "textDocument/documentSymbol"
  100. if not symbols_supported then
  101. Log:debug("skipping setup for document_symbols, method not supported by " .. client.name)
  102. return
  103. end
  104. local status_ok, navic = pcall(require, "nvim-navic")
  105. if status_ok then
  106. navic.attach(client, bufnr)
  107. end
  108. end
  109. function M.setup_codelens_refresh(client, bufnr)
  110. local status_ok, codelens_supported = pcall(function()
  111. return client.supports_method "textDocument/codeLens"
  112. end)
  113. if not status_ok or not codelens_supported then
  114. return
  115. end
  116. local group = "lsp_code_lens_refresh"
  117. local cl_events = { "BufEnter", "InsertLeave" }
  118. local ok, cl_autocmds = pcall(vim.api.nvim_get_autocmds, {
  119. group = group,
  120. buffer = bufnr,
  121. event = cl_events,
  122. })
  123. if ok and #cl_autocmds > 0 then
  124. return
  125. end
  126. vim.api.nvim_create_augroup(group, { clear = false })
  127. vim.api.nvim_create_autocmd(cl_events, {
  128. group = group,
  129. buffer = bufnr,
  130. callback = vim.lsp.codelens.refresh,
  131. })
  132. end
  133. ---filter passed to vim.lsp.buf.format
  134. ---always selects null-ls if it's available and caches the value per buffer
  135. ---@param client table client attached to a buffer
  136. ---@return boolean if client matches
  137. function M.format_filter(client)
  138. local filetype = vim.bo.filetype
  139. local n = require "null-ls"
  140. local s = require "null-ls.sources"
  141. local method = n.methods.FORMATTING
  142. local available_formatters = s.get_available(filetype, method)
  143. if #available_formatters > 0 then
  144. return client.name == "null-ls"
  145. elseif client.supports_method "textDocument/formatting" then
  146. return true
  147. else
  148. return false
  149. end
  150. end
  151. ---Simple wrapper for vim.lsp.buf.format() to provide defaults
  152. ---@param opts table|nil
  153. function M.format(opts)
  154. opts = opts or {}
  155. opts.filter = opts.filter or M.format_filter
  156. return vim.lsp.buf.format(opts)
  157. end
  158. return M