utils.lua 5.1 KB

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