manager.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. local lvim_lsp_utils = require "lvim.lsp.utils"
  4. function M.init_defaults(languages)
  5. for _, entry in ipairs(languages) do
  6. if not lvim.lang[entry] then
  7. lvim.lang[entry] = {
  8. formatters = {},
  9. linters = {},
  10. lsp = {},
  11. }
  12. end
  13. end
  14. end
  15. ---Resolve the configuration for a server based on both common and user configuration
  16. ---@param name string
  17. ---@param user_config table [optional]
  18. ---@return table
  19. local function resolve_config(name, user_config)
  20. local config = {
  21. on_attach = require("lvim.lsp").common_on_attach,
  22. on_init = require("lvim.lsp").common_on_init,
  23. capabilities = require("lvim.lsp").common_capabilities(),
  24. }
  25. local has_custom_provider, custom_config = pcall(require, "lvim/lsp/providers/" .. name)
  26. if has_custom_provider then
  27. Log:debug("Using custom configuration for requested server: " .. name)
  28. config = vim.tbl_deep_extend("force", config, custom_config)
  29. end
  30. if user_config then
  31. config = vim.tbl_deep_extend("force", config, user_config)
  32. end
  33. return config
  34. end
  35. ---Setup a language server by providing a name
  36. ---@param server_name string name of the language server
  37. ---@param user_config table [optional] when available it will take predence over any default configurations
  38. function M.setup(server_name, user_config)
  39. vim.validate { name = { server_name, "string" } }
  40. if lvim_lsp_utils.is_client_active(server_name) then
  41. return
  42. end
  43. local config = resolve_config(server_name, user_config)
  44. local server_available, requested_server = require("nvim-lsp-installer.servers").get_server(server_name)
  45. local function ensure_installed(server)
  46. if server:is_installed() then
  47. return true
  48. end
  49. if not lvim.lsp.automatic_servers_installation then
  50. Log:debug(server.name .. " is not managed by the automatic installer")
  51. return false
  52. end
  53. Log:debug(string.format("Installing [%s]", server.name))
  54. server:install()
  55. vim.schedule(function()
  56. vim.cmd [[LspStart]]
  57. end)
  58. end
  59. if server_available and ensure_installed(requested_server) then
  60. requested_server:setup(config)
  61. else
  62. -- since it may not be installed, don't attempt to configure the LSP unless there is a custom provider
  63. local has_custom_provider, _ = pcall(require, "lvim/lsp/providers/" .. server_name)
  64. if has_custom_provider then
  65. require("lspconfig")[server_name].setup(config)
  66. end
  67. end
  68. end
  69. return M