manager.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. local M = {}
  2. local Log = require "core.log"
  3. local lsp_utils = require "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. local function is_overridden(server)
  16. local overrides = lvim.lsp.override
  17. if type(overrides) == "table" then
  18. if vim.tbl_contains(overrides, server) then
  19. return true
  20. end
  21. end
  22. end
  23. ---Resolve the configuration for a server based on both common and user configuration
  24. ---@param name string
  25. ---@param user_config table [optional]
  26. ---@return table
  27. local function resolve_config(name, user_config)
  28. local config = {
  29. on_attach = require("lsp").common_on_attach,
  30. on_init = require("lsp").common_on_init,
  31. capabilities = require("lsp").common_capabilities(),
  32. }
  33. local status_ok, custom_config = pcall(require, "lsp/providers/" .. name)
  34. if status_ok then
  35. Log:debug("Using custom configuration for requested server: " .. name)
  36. config = vim.tbl_deep_extend("force", config, custom_config)
  37. end
  38. if user_config then
  39. config = vim.tbl_deep_extend("force", config, user_config)
  40. end
  41. return config
  42. end
  43. ---Setup a language server by providing a name
  44. ---@param server_name string name of the language server
  45. ---@param user_config table [optional] when available it will take predence over any default configurations
  46. function M.setup(server_name, user_config)
  47. vim.validate { name = { server_name, "string" } }
  48. if lsp_utils.is_client_active(server_name) or is_overridden(server_name) then
  49. return
  50. end
  51. local config = resolve_config(server_name, user_config)
  52. local server_available, requested_server = require("nvim-lsp-installer.servers").get_server(server_name)
  53. local function ensure_installed(server)
  54. if server:is_installed() then
  55. return true
  56. end
  57. if not lvim.lsp.automatic_servers_installation then
  58. Log:debug(server.name .. " is not managed by the automatic installer")
  59. return false
  60. end
  61. Log:debug(string.format("Installing [%s]", server.name))
  62. server:install()
  63. vim.schedule(function()
  64. vim.cmd [[LspStart]]
  65. end)
  66. end
  67. if server_available and ensure_installed(requested_server) then
  68. requested_server:setup(config)
  69. else
  70. require("lspconfig")[server_name].setup(config)
  71. end
  72. end
  73. return M