utils.lua 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. if not client_id then
  22. local buf_clients = vim.lsp.buf_get_clients()
  23. for _, buf_client in ipairs(buf_clients) do
  24. if buf_client.name ~= "null-ls" then
  25. client_id = buf_client.id
  26. break
  27. end
  28. end
  29. end
  30. if not client_id then
  31. error "Unable to determine client_id"
  32. return
  33. end
  34. local client = vim.lsp.get_client_by_id(tonumber(client_id))
  35. local enabled_caps = {}
  36. for capability, status in pairs(client.resolved_capabilities) do
  37. if status == true then
  38. table.insert(enabled_caps, capability)
  39. end
  40. end
  41. return enabled_caps
  42. end
  43. function M.get_supported_filetypes(server_name)
  44. -- print("got filetypes query request for: " .. server_name)
  45. local configs = require "lspconfig/configs"
  46. pcall(require, ("lspconfig/" .. server_name))
  47. for _, config in pairs(configs) do
  48. if config.name == server_name then
  49. return config.document_config.default_config.filetypes or {}
  50. end
  51. end
  52. end
  53. return M