manager.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. languages = languages or lvim_lsp_utils.get_all_supported_filetypes()
  6. for _, entry in ipairs(languages) do
  7. if not lvim.lang[entry] then
  8. lvim.lang[entry] = {
  9. formatters = {},
  10. linters = {},
  11. lsp = {},
  12. }
  13. end
  14. end
  15. end
  16. ---Resolve the configuration for a server by merging with the default config
  17. ---@param server_name string
  18. ---@vararg any config table [optional]
  19. ---@return table
  20. local function resolve_config(server_name, ...)
  21. local defaults = {
  22. on_attach = require("lvim.lsp").common_on_attach,
  23. on_init = require("lvim.lsp").common_on_init,
  24. on_exit = require("lvim.lsp").common_on_exit,
  25. capabilities = require("lvim.lsp").common_capabilities(),
  26. }
  27. local has_custom_provider, custom_config = pcall(require, "lvim/lsp/providers/" .. server_name)
  28. if has_custom_provider then
  29. Log:debug("Using custom configuration for requested server: " .. server_name)
  30. defaults = vim.tbl_deep_extend("force", defaults, custom_config)
  31. end
  32. defaults = vim.tbl_deep_extend("force", defaults, ...)
  33. return defaults
  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. Log:debug(string.format("[%q] is already configured", server_name))
  49. return true
  50. end
  51. end
  52. return false
  53. end
  54. local function launch_server(server_name, config)
  55. pcall(function()
  56. require("lspconfig")[server_name].setup(config)
  57. buf_try_add(server_name)
  58. end)
  59. end
  60. ---Setup a language server by providing a name
  61. ---@param server_name string name of the language server
  62. ---@param user_config table? when available it will take predence over any default configurations
  63. function M.setup(server_name, user_config)
  64. vim.validate { name = { server_name, "string" } }
  65. user_config = user_config or {}
  66. if lvim_lsp_utils.is_client_active(server_name) or client_is_configured(server_name) then
  67. return
  68. end
  69. local servers = require "nvim-lsp-installer.servers"
  70. local server_available, server = servers.get_server(server_name)
  71. if not server_available then
  72. local config = resolve_config(server_name, user_config)
  73. launch_server(server_name, config)
  74. return
  75. end
  76. local install_in_progress = false
  77. if not server:is_installed() then
  78. if lvim.lsp.automatic_servers_installation then
  79. Log:debug "Automatic server installation detected"
  80. server:install()
  81. install_in_progress = true
  82. else
  83. Log:debug(server.name .. " is not managed by the automatic installer")
  84. end
  85. end
  86. server:on_ready(function()
  87. if install_in_progress then
  88. vim.notify(string.format("Installation complete for [%s] server", server.name), vim.log.levels.INFO)
  89. end
  90. install_in_progress = false
  91. local config = resolve_config(server_name, server:get_default_options(), user_config)
  92. launch_server(server_name, config)
  93. end)
  94. end
  95. return M