manager.lua 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 servers = require "nvim-lsp-installer.servers"
  49. local config = resolve_config(server_name, user_config)
  50. local server_available, requested_server = servers.get_server(server_name)
  51. if server_available then
  52. local install_notification = false
  53. if not requested_server:is_installed() then
  54. if lvim.lsp.automatic_servers_installation then
  55. Log:debug "Automatic server installation detected"
  56. requested_server:install()
  57. install_notification = true
  58. else
  59. Log:debug(requested_server.name .. " is not managed by the automatic installer")
  60. end
  61. end
  62. requested_server:on_ready(function()
  63. if install_notification then
  64. vim.notify(string.format("Installation complete for [%s] server", requested_server.name), vim.log.levels.INFO)
  65. end
  66. install_notification = false
  67. requested_server:setup(config)
  68. end)
  69. else
  70. -- since it may not be installed, don't attempt to configure the LSP unless there is a custom provider
  71. local has_custom_provider, _ = pcall(require, "lvim/lsp/providers/" .. server_name)
  72. if has_custom_provider then
  73. require("lspconfig")[server_name].setup(config)
  74. buf_try_add(server_name)
  75. end
  76. end
  77. end
  78. return M