utils.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.disable_formatting_capability(client)
  12. -- FIXME: figure out a reasonable way to do this
  13. client.resolved_capabilities.document_formatting = false
  14. require("core.log"):debug(string.format("Turning off formatting capability for language server [%s] ", client.name))
  15. end
  16. function M.get_active_client_by_ft(filetype)
  17. local matches = {}
  18. local clients = vim.lsp.get_active_clients()
  19. for _, client in pairs(clients) do
  20. local supported_filetypes = client.config.filetypes or {}
  21. if client.name ~= "null-ls" and vim.tbl_contains(supported_filetypes, filetype) then
  22. table.insert(matches, client)
  23. end
  24. end
  25. return matches
  26. end
  27. function M.get_ls_capabilities(client_id)
  28. if not client_id then
  29. local buf_clients = vim.lsp.buf_get_clients()
  30. for _, buf_client in ipairs(buf_clients) do
  31. if buf_client.name ~= "null-ls" then
  32. client_id = buf_client.id
  33. break
  34. end
  35. end
  36. end
  37. if not client_id then
  38. error "Unable to determine client_id"
  39. return
  40. end
  41. local client = vim.lsp.get_client_by_id(tonumber(client_id))
  42. local enabled_caps = {}
  43. for capability, status in pairs(client.resolved_capabilities) do
  44. if status == true then
  45. table.insert(enabled_caps, capability)
  46. end
  47. end
  48. return enabled_caps
  49. end
  50. function M.get_supported_filetypes(server_name)
  51. -- print("got filetypes query request for: " .. server_name)
  52. local configs = require "lspconfig/configs"
  53. pcall(require, ("lspconfig/" .. server_name))
  54. for _, config in pairs(configs) do
  55. if config.name == server_name then
  56. return config.document_config.default_config.filetypes or {}
  57. end
  58. end
  59. end
  60. return M