2021-08-13 22:32:56 +02:00
|
|
|
local M = {}
|
|
|
|
|
2021-10-10 21:07:41 +02:00
|
|
|
local tbl = require "lvim.utils.table"
|
2021-10-09 13:45:34 +02:00
|
|
|
|
2021-08-13 22:32:56 +02:00
|
|
|
function M.is_client_active(name)
|
|
|
|
local clients = vim.lsp.get_active_clients()
|
2021-10-09 13:45:34 +02:00
|
|
|
return tbl.find_first(clients, function(client)
|
|
|
|
return client.name == name
|
|
|
|
end)
|
2021-08-13 22:32:56 +02:00
|
|
|
end
|
|
|
|
|
2021-10-09 13:45:34 +02:00
|
|
|
function M.get_active_clients_by_ft(filetype)
|
2021-10-03 16:13:46 +02:00
|
|
|
local matches = {}
|
2021-08-13 22:32:56 +02:00
|
|
|
local clients = vim.lsp.get_active_clients()
|
|
|
|
for _, client in pairs(clients) do
|
2021-10-03 16:13:46 +02:00
|
|
|
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
|
|
|
|
|
2021-10-09 13:45:34 +02:00
|
|
|
function M.get_client_capabilities(client_id)
|
2021-12-09 17:08:53 +01:00
|
|
|
local client
|
2021-10-03 16:13:46 +02:00
|
|
|
if not client_id then
|
|
|
|
local buf_clients = vim.lsp.buf_get_clients()
|
2021-12-09 17:08:53 +01:00
|
|
|
for _, buf_client in pairs(buf_clients) do
|
2021-10-03 16:13:46 +02:00
|
|
|
if buf_client.name ~= "null-ls" then
|
2021-12-09 17:08:53 +01:00
|
|
|
client = buf_client
|
2021-10-03 16:13:46 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2021-12-12 19:43:35 +01:00
|
|
|
else
|
|
|
|
client = vim.lsp.get_client_by_id(tonumber(client_id))
|
2021-10-03 16:13:46 +02:00
|
|
|
end
|
2021-12-12 19:43:35 +01:00
|
|
|
if not client then
|
2021-10-03 16:13:46 +02:00
|
|
|
error "Unable to determine client_id"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local enabled_caps = {}
|
2022-05-03 01:10:51 +02:00
|
|
|
for capability, status in pairs(client.server_capabilities or client.resolved_capabilities) do
|
2021-10-03 16:13:46 +02:00
|
|
|
if status == true then
|
|
|
|
table.insert(enabled_caps, capability)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return enabled_caps
|
|
|
|
end
|
|
|
|
|
2021-12-20 08:59:47 +01:00
|
|
|
---Get supported filetypes per server
|
|
|
|
---@param server_name string can be any server supported by nvim-lsp-installer
|
2022-08-09 10:41:17 +02:00
|
|
|
---@return string[] supported filestypes as a list of strings
|
2021-10-03 16:13:46 +02:00
|
|
|
function M.get_supported_filetypes(server_name)
|
2022-08-09 10:41:17 +02:00
|
|
|
local status_ok, config = pcall(require, ("lspconfig.server_configurations.%s"):format(server_name))
|
2021-12-20 08:59:47 +01:00
|
|
|
if not status_ok then
|
|
|
|
return {}
|
2021-10-31 16:23:44 +01:00
|
|
|
end
|
2021-12-20 08:59:47 +01:00
|
|
|
|
2022-08-09 10:41:17 +02:00
|
|
|
return config.default_config.filetypes or {}
|
2021-08-13 22:32:56 +02:00
|
|
|
end
|
|
|
|
|
2022-01-08 14:36:56 +01:00
|
|
|
---Get supported servers per filetype
|
2022-08-09 10:41:17 +02:00
|
|
|
---@param filter { filetype: string | string[] }?: (optional) Used to filter the list of server names.
|
|
|
|
---@return string[] list of names of supported servers
|
|
|
|
function M.get_supported_servers(filter)
|
|
|
|
local _, supported_servers = pcall(function()
|
|
|
|
return require("mason-lspconfig").get_available_servers(filter)
|
|
|
|
end)
|
|
|
|
return supported_servers or {}
|
2022-01-08 14:36:56 +01:00
|
|
|
end
|
|
|
|
|
2021-12-20 08:59:47 +01:00
|
|
|
---Get all supported filetypes by nvim-lsp-installer
|
2022-08-09 10:41:17 +02:00
|
|
|
---@return string[] supported filestypes as a list of strings
|
2021-12-20 08:59:47 +01:00
|
|
|
function M.get_all_supported_filetypes()
|
2022-08-09 10:41:17 +02:00
|
|
|
local status_ok, filetype_server_map = pcall(require, "mason-lspconfig.mappings.filetype")
|
2021-12-20 08:59:47 +01:00
|
|
|
if not status_ok then
|
|
|
|
return {}
|
|
|
|
end
|
2022-08-09 10:41:17 +02:00
|
|
|
return vim.tbl_keys(filetype_server_map or {})
|
2021-12-20 08:59:47 +01:00
|
|
|
end
|
|
|
|
|
2022-05-03 01:10:51 +02:00
|
|
|
function M.setup_document_highlight(client, bufnr)
|
|
|
|
local status_ok, highlight_supported = pcall(function()
|
|
|
|
return client.supports_method "textDocument/documentHighlight"
|
2022-01-03 11:06:45 +01:00
|
|
|
end)
|
2022-05-03 01:10:51 +02:00
|
|
|
if not status_ok or not highlight_supported then
|
2022-01-03 11:06:45 +01:00
|
|
|
return
|
|
|
|
end
|
2022-05-03 01:10:51 +02:00
|
|
|
local augroup_exist, _ = pcall(vim.api.nvim_get_autocmds, {
|
|
|
|
group = "lsp_document_highlight",
|
|
|
|
})
|
|
|
|
if not augroup_exist then
|
|
|
|
vim.api.nvim_create_augroup("lsp_document_highlight", {})
|
|
|
|
end
|
|
|
|
vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
|
|
|
|
group = "lsp_document_highlight",
|
|
|
|
buffer = bufnr,
|
|
|
|
callback = vim.lsp.buf.document_highlight,
|
|
|
|
})
|
|
|
|
vim.api.nvim_create_autocmd("CursorMoved", {
|
|
|
|
group = "lsp_document_highlight",
|
|
|
|
buffer = bufnr,
|
|
|
|
callback = vim.lsp.buf.clear_references,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.setup_codelens_refresh(client, bufnr)
|
|
|
|
local status_ok, codelens_supported = pcall(function()
|
|
|
|
return client.supports_method "textDocument/codeLens"
|
|
|
|
end)
|
|
|
|
if not status_ok or not codelens_supported then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local augroup_exist, _ = pcall(vim.api.nvim_get_autocmds, {
|
|
|
|
group = "lsp_code_lens_refresh",
|
|
|
|
})
|
|
|
|
if not augroup_exist then
|
|
|
|
vim.api.nvim_create_augroup("lsp_code_lens_refresh", {})
|
|
|
|
end
|
2022-05-11 16:31:04 +02:00
|
|
|
vim.api.nvim_create_autocmd({ "BufEnter", "InsertLeave" }, {
|
2022-05-03 01:10:51 +02:00
|
|
|
group = "lsp_code_lens_refresh",
|
|
|
|
buffer = bufnr,
|
|
|
|
callback = vim.lsp.codelens.refresh,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
---filter passed to vim.lsp.buf.format
|
2022-07-02 09:36:11 +02:00
|
|
|
---always selects null-ls if it's available and caches the value per buffer
|
|
|
|
---@param client table client attached to a buffer
|
2022-07-31 14:27:59 +02:00
|
|
|
---@return boolean if client matches
|
2022-07-02 09:36:11 +02:00
|
|
|
function M.format_filter(client)
|
|
|
|
local filetype = vim.bo.filetype
|
|
|
|
local n = require "null-ls"
|
|
|
|
local s = require "null-ls.sources"
|
|
|
|
local method = n.methods.FORMATTING
|
2022-07-31 14:27:59 +02:00
|
|
|
local avalable_formatters = s.get_available(filetype, method)
|
2022-07-02 09:36:11 +02:00
|
|
|
|
2022-07-31 14:27:59 +02:00
|
|
|
if #avalable_formatters > 0 then
|
2022-07-02 09:36:11 +02:00
|
|
|
return client.name == "null-ls"
|
2022-07-31 14:27:59 +02:00
|
|
|
elseif client.supports_method "textDocument/formatting" then
|
2022-07-02 09:36:11 +02:00
|
|
|
return true
|
2022-07-31 14:27:59 +02:00
|
|
|
else
|
|
|
|
return false
|
2022-07-02 09:36:11 +02:00
|
|
|
end
|
2022-05-03 01:10:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
---Provide vim.lsp.buf.format for nvim <0.8
|
|
|
|
---@param opts table
|
|
|
|
function M.format(opts)
|
2022-07-02 09:36:11 +02:00
|
|
|
opts = opts or {}
|
|
|
|
opts.filter = opts.filter or M.format_filter
|
2022-05-03 01:10:51 +02:00
|
|
|
|
|
|
|
if vim.lsp.buf.format then
|
2022-05-11 17:15:00 +02:00
|
|
|
return vim.lsp.buf.format(opts)
|
2022-05-03 01:10:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local bufnr = opts.bufnr or vim.api.nvim_get_current_buf()
|
2022-07-02 09:36:11 +02:00
|
|
|
|
2022-07-31 14:27:59 +02:00
|
|
|
---@type table|nil
|
2022-07-02 09:36:11 +02:00
|
|
|
local clients = vim.lsp.get_active_clients {
|
|
|
|
id = opts.id,
|
|
|
|
bufnr = bufnr,
|
|
|
|
name = opts.name,
|
|
|
|
}
|
2022-05-03 01:10:51 +02:00
|
|
|
|
|
|
|
if opts.filter then
|
2022-07-02 09:36:11 +02:00
|
|
|
clients = vim.tbl_filter(opts.filter, clients)
|
2022-05-03 01:10:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
clients = vim.tbl_filter(function(client)
|
|
|
|
return client.supports_method "textDocument/formatting"
|
|
|
|
end, clients)
|
|
|
|
|
|
|
|
if #clients == 0 then
|
2022-06-23 07:59:30 +02:00
|
|
|
vim.notify_once "[LSP] Format request failed, no matching language servers."
|
2022-05-03 01:10:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local timeout_ms = opts.timeout_ms or 1000
|
|
|
|
for _, client in pairs(clients) do
|
|
|
|
local params = vim.lsp.util.make_formatting_params(opts.formatting_options)
|
|
|
|
local result, err = client.request_sync("textDocument/formatting", params, timeout_ms, bufnr)
|
|
|
|
if result and result.result then
|
|
|
|
vim.lsp.util.apply_text_edits(result.result, bufnr, client.offset_encoding)
|
|
|
|
elseif err then
|
|
|
|
vim.notify(string.format("[LSP][%s] %s", client.name, err), vim.log.levels.WARN)
|
|
|
|
end
|
|
|
|
end
|
2022-01-03 11:06:45 +01:00
|
|
|
end
|
|
|
|
|
2021-08-13 22:32:56 +02:00
|
|
|
return M
|