manager.lua 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. -- manually start the server and don't wait for the usual filetype trigger from lspconfig
  36. local function buf_try_add(server_name, bufnr)
  37. bufnr = bufnr or vim.api.nvim_get_current_buf()
  38. require("lspconfig")[server_name].manager.try_add_wrapper(bufnr)
  39. end
  40. ---Setup a language server by providing a name
  41. ---@param server_name string name of the language server
  42. ---@param user_config table [optional] when available it will take predence over any default configurations
  43. function M.setup(server_name, user_config)
  44. vim.validate { name = { server_name, "string" } }
  45. if lvim_lsp_utils.is_client_active(server_name) then
  46. return
  47. end
  48. local config = resolve_config(server_name, user_config)
  49. local servers = require "nvim-lsp-installer.servers"
  50. local server_available, requested_server = servers.get_server(server_name)
  51. local is_overridden = vim.tbl_contains(lvim.lsp.override, server_name)
  52. if not server_available or is_overridden then
  53. pcall(function()
  54. require("lspconfig")[server_name].setup(config)
  55. buf_try_add(server_name)
  56. end)
  57. return
  58. end
  59. local install_notification = false
  60. if not requested_server:is_installed() then
  61. if lvim.lsp.automatic_servers_installation then
  62. Log:debug "Automatic server installation detected"
  63. requested_server:install()
  64. install_notification = true
  65. else
  66. Log:debug(requested_server.name .. " is not managed by the automatic installer")
  67. end
  68. end
  69. requested_server:on_ready(function()
  70. if install_notification then
  71. vim.notify(string.format("Installation complete for [%s] server", requested_server.name), vim.log.levels.INFO)
  72. end
  73. install_notification = false
  74. requested_server:setup(config)
  75. end)
  76. end
  77. return M