manager.lua 3.5 KB

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