mirror of
https://github.com/LunarVim/LunarVim.git
synced 2025-08-31 07:09:23 +02:00
refactor: auto-generate language configuration (#1584)
Refactor the monolithic `lvim.lang` design into a more modular approach. IMPORTANT: run `:LvimUpdate` in order to generate the new ftplugin template files.
This commit is contained in:
parent
3e1cd1ec23
commit
d01ba08eae
98 changed files with 1127 additions and 1567 deletions
108
lua/lsp/init.lua
108
lua/lsp/init.lua
|
@ -1,5 +1,6 @@
|
|||
local M = {}
|
||||
local Log = require "core.log"
|
||||
local utils = require "utils"
|
||||
|
||||
local function lsp_highlight_document(client)
|
||||
if lvim.lsp.document_highlight == false then
|
||||
|
@ -61,48 +62,12 @@ function M.common_capabilities()
|
|||
return capabilities
|
||||
end
|
||||
|
||||
function M.get_ls_capabilities(client_id)
|
||||
local client
|
||||
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"
|
||||
end
|
||||
|
||||
client = vim.lsp.get_client_by_id(tonumber(client_id))
|
||||
|
||||
local enabled_caps = {}
|
||||
|
||||
for k, v in pairs(client.resolved_capabilities) do
|
||||
if v == true then
|
||||
table.insert(enabled_caps, k)
|
||||
end
|
||||
end
|
||||
|
||||
return enabled_caps
|
||||
end
|
||||
|
||||
function M.common_on_init(client, bufnr)
|
||||
if lvim.lsp.on_init_callback then
|
||||
lvim.lsp.on_init_callback(client, bufnr)
|
||||
Log:debug "Called lsp.on_init_callback"
|
||||
return
|
||||
end
|
||||
|
||||
local formatters = lvim.lang[vim.bo.filetype].formatters
|
||||
if not vim.tbl_isempty(formatters) and formatters[1]["exe"] ~= nil and formatters[1].exe ~= "" then
|
||||
client.resolved_capabilities.document_formatting = false
|
||||
Log:debug(
|
||||
string.format("Overriding language server [%s] with format provider [%s]", client.name, formatters[1].exe)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
function M.common_on_attach(client, bufnr)
|
||||
|
@ -112,63 +77,46 @@ function M.common_on_attach(client, bufnr)
|
|||
end
|
||||
lsp_highlight_document(client)
|
||||
add_lsp_buffer_keybindings(bufnr)
|
||||
require("lsp.null-ls").setup(vim.bo.filetype)
|
||||
end
|
||||
|
||||
function M.setup(lang)
|
||||
local lsp_utils = require "lsp.utils"
|
||||
local lsp = lvim.lang[lang].lsp
|
||||
if (lsp.active ~= nil and not lsp.active) or lsp_utils.is_client_active(lsp.provider) then
|
||||
local function bootstrap_nlsp(opts)
|
||||
opts = opts or {}
|
||||
local lsp_settings_status_ok, lsp_settings = pcall(require, "nlspsettings")
|
||||
if lsp_settings_status_ok then
|
||||
lsp_settings.setup(opts)
|
||||
end
|
||||
end
|
||||
|
||||
function M.get_common_opts()
|
||||
return {
|
||||
on_attach = M.common_on_attach,
|
||||
on_init = M.common_on_init,
|
||||
capabilities = M.common_capabilities(),
|
||||
}
|
||||
end
|
||||
|
||||
function M.setup()
|
||||
Log:debug "Setting up LSP support"
|
||||
|
||||
local lsp_status_ok, _ = pcall(require, "lspconfig")
|
||||
if not lsp_status_ok then
|
||||
return
|
||||
end
|
||||
|
||||
local overrides = lvim.lsp.override
|
||||
if type(overrides) == "table" then
|
||||
if vim.tbl_contains(overrides, lang) then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
if lsp.provider ~= nil and lsp.provider ~= "" then
|
||||
local lspconfig = require "lspconfig"
|
||||
|
||||
if not lsp.setup.on_attach then
|
||||
lsp.setup.on_attach = M.common_on_attach
|
||||
end
|
||||
if not lsp.setup.on_init then
|
||||
lsp.setup.on_init = M.common_on_init
|
||||
end
|
||||
if not lsp.setup.capabilities then
|
||||
lsp.setup.capabilities = M.common_capabilities()
|
||||
end
|
||||
|
||||
lspconfig[lsp.provider].setup(lsp.setup)
|
||||
end
|
||||
end
|
||||
|
||||
function M.global_setup()
|
||||
vim.lsp.protocol.CompletionItemKind = lvim.lsp.completion.item_kind
|
||||
|
||||
for _, sign in ipairs(lvim.lsp.diagnostics.signs.values) do
|
||||
vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = sign.name })
|
||||
end
|
||||
|
||||
require("lsp.handlers").setup()
|
||||
|
||||
local null_status_ok, null_ls = pcall(require, "null-ls")
|
||||
if null_status_ok then
|
||||
null_ls.config()
|
||||
require("lspconfig")["null-ls"].setup(lvim.lsp.null_ls.setup)
|
||||
if not utils.is_directory(lvim.lsp.templates_dir) then
|
||||
require("lsp.templates").generate_templates()
|
||||
end
|
||||
|
||||
local utils = require "utils"
|
||||
bootstrap_nlsp { config_home = utils.join_paths(get_config_dir(), "lsp-settings") }
|
||||
|
||||
local lsp_settings_status_ok, lsp_settings = pcall(require, "nlspsettings")
|
||||
if lsp_settings_status_ok then
|
||||
lsp_settings.setup {
|
||||
config_home = utils.join_paths(get_config_dir(), "lsp-settings"),
|
||||
}
|
||||
end
|
||||
require("lsp.null-ls").setup()
|
||||
|
||||
require("utils").toggle_autoformat()
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue