manager.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. -- check if the manager autocomd has already been configured since some servers can take a while to initialize
  41. -- this helps guarding against a data-race condition where a server can get configured twice
  42. -- which seems to occur only when attaching to single-files
  43. local function client_is_configured(server_name, ft)
  44. ft = ft or vim.bo.filetype
  45. local active_autocmds = vim.split(vim.fn.execute("autocmd FileType " .. ft), "\n")
  46. for _, result in ipairs(active_autocmds) do
  47. if result:match(server_name) then
  48. return true
  49. end
  50. end
  51. return false
  52. end
  53. ---Setup a language server by providing a name
  54. ---@param server_name string name of the language server
  55. ---@param user_config table [optional] when available it will take predence over any default configurations
  56. function M.setup(server_name, user_config)
  57. vim.validate { name = { server_name, "string" } }
  58. if lvim_lsp_utils.is_client_active(server_name) or client_is_configured(server_name) then
  59. Log:debug(string.format("[%q] is already configured. Ignoring repeated setup call.", server_name))
  60. return
  61. end
  62. local config = resolve_config(server_name, user_config)
  63. local servers = require "nvim-lsp-installer.servers"
  64. local server_available, requested_server = servers.get_server(server_name)
  65. local is_overridden = vim.tbl_contains(lvim.lsp.override, server_name)
  66. if not server_available or is_overridden then
  67. pcall(function()
  68. require("lspconfig")[server_name].setup(config)
  69. buf_try_add(server_name)
  70. end)
  71. return
  72. end
  73. local install_notification = false
  74. if not requested_server:is_installed() then
  75. if lvim.lsp.automatic_servers_installation then
  76. Log:debug "Automatic server installation detected"
  77. requested_server:install()
  78. install_notification = true
  79. else
  80. Log:debug(requested_server.name .. " is not managed by the automatic installer")
  81. end
  82. end
  83. requested_server:on_ready(function()
  84. if install_notification then
  85. vim.notify(string.format("Installation complete for [%s] server", requested_server.name), vim.log.levels.INFO)
  86. end
  87. install_notification = false
  88. requested_server:setup(config)
  89. end)
  90. end
  91. return M