summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/lsp/manager.lua
blob: 49771b2cd1d1f92f2a46238105a79000b7bc5a33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
local M = {}

local Log = require "core.log"
local lsp_utils = require "lsp.utils"

function M.init_defaults(languages)
  for _, entry in ipairs(languages) do
    if not options.lang[entry] then
      options.lang[entry] = {
        formatters = {},
        linters = {},
        lsp = {},
      }
    end
  end
end

local function is_overridden(server)
  local overrides = options.lsp.override
  if type(overrides) == "table" then
    if vim.tbl_contains(overrides, server) then
      return true
    end
  end
end

function M.setup_server(server_name)
  vim.validate {
    name = { server_name, "string" },
  }

  if lsp_utils.is_client_active(server_name) or is_overridden(server_name) then
    return
  end

  local lsp_installer_servers = require "nvim-lsp-installer.servers"
  local server_available, requested_server = lsp_installer_servers.get_server(
    server_name
  )
  if server_available then
    if not requested_server:is_installed() then
      Log:debug(string.format("[%s] is not installed", server_name))
      if options.lsp.automatic_servers_installation then
        Log:debug(string.format("Installing [%s]", server_name))
        requested_server:install()
      else
        return
      end
    end
  end

  local default_config = {
    on_attach = require("lsp").common_on_attach,
    on_init = require("lsp").common_on_init,
    capabilities = require("lsp").common_capabilities(),
  }

  local status_ok, custom_config = pcall(
    require,
    "lsp/providers/" .. requested_server.name
  )
  if status_ok then
    local new_config = vim.tbl_deep_extend("force", default_config, custom_config)
    Log:debug(
      "Using custom configuration for requested server: " .. requested_server.name
    )
    requested_server:setup(new_config)
  else
    Log:debug(
      "Using the default configuration for requested server: " .. requested_server.name
    )
    requested_server:setup(default_config)
  end
end

function M.setup(servers)
  local status_ok, _ = pcall(require, "nvim-lsp-installer")
  if not status_ok then
    return
  end

  --- allow using a single value
  if type(servers) == "string" then
    servers = { servers }
  end

  for _, server in ipairs(servers) do
    M.setup_server(server)
  end
end

return M