manager.lua 3.6 KB

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