utils.lua 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. local M = {}
  2. function M.is_client_active(name)
  3. local clients = vim.lsp.get_active_clients()
  4. for _, client in pairs(clients) do
  5. if client.name == name then
  6. return true, client
  7. end
  8. end
  9. return false
  10. end
  11. function M.get_active_client_by_ft(filetype)
  12. local matches = {}
  13. local clients = vim.lsp.get_active_clients()
  14. for _, client in pairs(clients) do
  15. local supported_filetypes = client.config.filetypes or {}
  16. if client.name ~= "null-ls" and vim.tbl_contains(supported_filetypes, filetype) then
  17. table.insert(matches, client)
  18. end
  19. end
  20. return matches
  21. end
  22. function M.get_ls_capabilities(client_id)
  23. if not client_id then
  24. local buf_clients = vim.lsp.buf_get_clients()
  25. for _, buf_client in ipairs(buf_clients) do
  26. if buf_client.name ~= "null-ls" then
  27. client_id = buf_client.id
  28. break
  29. end
  30. end
  31. end
  32. if not client_id then
  33. error "Unable to determine client_id"
  34. return
  35. end
  36. local client = vim.lsp.get_client_by_id(tonumber(client_id))
  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. function M.get_supported_filetypes(server_name)
  46. -- print("got filetypes query request for: " .. server_name)
  47. local configs = require "lspconfig/configs"
  48. pcall(require, ("lspconfig/" .. server_name))
  49. for _, config in pairs(configs) do
  50. if config.name == server_name then
  51. return config.document_config.default_config.filetypes or {}
  52. end
  53. end
  54. end
  55. return M