summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/lsp/utils.lua
blob: e0046db6f55e7c47742934e8078b0fb0647ac32c (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
local M = {}

function M.is_client_active(name)
  local clients = vim.lsp.get_active_clients()
  for _, client in pairs(clients) do
    if client.name == name then
      return true, client
    end
  end
  return false
end

function M.disable_formatting_capability(client)
  -- FIXME: figure out a reasonable way to do this
  client.resolved_capabilities.document_formatting = false
  require("core.log"):debug(
    string.format(
      "Turning off formatting capability for language server [%s] ",
      client.name
    )
  )
end

function M.get_active_client_by_ft(filetype)
  local matches = {}
  local clients = vim.lsp.get_active_clients()
  for _, client in pairs(clients) do
    local supported_filetypes = client.config.filetypes or {}
    if client.name ~= "null-ls" and vim.tbl_contains(supported_filetypes, filetype) then
      table.insert(matches, client)
    end
  end
  return matches
end

function M.get_ls_capabilities(client_id)
  if not client_id then
    local buf_clients = vim.lsp.buf_get_clients()
    for _, buf_client in ipairs(buf_clients) do
      if buf_client.name ~= "null-ls" then
        client_id = buf_client.id
        break
      end
    end
  end
  if not client_id then
    error "Unable to determine client_id"
    return
  end

  local client = vim.lsp.get_client_by_id(tonumber(client_id))

  local enabled_caps = {}
  for capability, status in pairs(client.resolved_capabilities) do
    if status == true then
      table.insert(enabled_caps, capability)
    end
  end

  return enabled_caps
end

function M.get_supported_filetypes(server_name)
  -- print("got filetypes query request for: " .. server_name)
  local configs = require "lspconfig/configs"
  pcall(require, ("lspconfig/" .. server_name))
  for _, config in pairs(configs) do
    if config.name == server_name then
      return config.document_config.default_config.filetypes or {}
    end
  end
end

return M