utils.lua 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.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 table supported filestypes as a list of strings
  48. function M.get_supported_filetypes(server_name)
  49. local status_ok, lsp_installer_servers = pcall(require, "nvim-lsp-installer.servers")
  50. if not status_ok then
  51. return {}
  52. end
  53. local server_available, requested_server = lsp_installer_servers.get_server(server_name)
  54. if not server_available then
  55. return {}
  56. end
  57. return requested_server:get_supported_filetypes()
  58. end
  59. ---Get supported servers per filetype
  60. ---@param filetype string
  61. ---@return table list of names of supported servers
  62. function M.get_supported_servers_per_filetype(filetype)
  63. local filetype_server_map = require "nvim-lsp-installer._generated.filetype_map"
  64. return filetype_server_map[filetype]
  65. end
  66. ---Get all supported filetypes by nvim-lsp-installer
  67. ---@return table supported filestypes as a list of strings
  68. function M.get_all_supported_filetypes()
  69. local status_ok, lsp_installer_filetypes = pcall(require, "nvim-lsp-installer._generated.filetype_map")
  70. if not status_ok then
  71. return {}
  72. end
  73. return vim.tbl_keys(lsp_installer_filetypes or {})
  74. end
  75. function M.conditional_document_highlight(id)
  76. local client_ok, method_supported = pcall(function()
  77. return vim.lsp.get_client_by_id(id).resolved_capabilities.document_highlight
  78. end)
  79. if not client_ok or not method_supported then
  80. return
  81. end
  82. vim.lsp.buf.document_highlight()
  83. end
  84. return M