mirror of
https://github.com/LazyVim/LazyVim.git
synced 2025-06-23 17:28:57 +02:00
refactor(lsp): simplified lsp code
This commit is contained in:
parent
75d8aff979
commit
9ab118aade
2 changed files with 22 additions and 34 deletions
|
@ -4,43 +4,34 @@ M.autoformat = true
|
||||||
|
|
||||||
function M.toggle()
|
function M.toggle()
|
||||||
M.autoformat = not M.autoformat
|
M.autoformat = not M.autoformat
|
||||||
if M.autoformat then
|
vim.notify(M.autoformat and "Enabled format on save" or "Disabled format on save")
|
||||||
vim.notify("enabled format on save")
|
|
||||||
else
|
|
||||||
vim.notify("disabled format on save")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.format()
|
function M.format()
|
||||||
if M.autoformat then
|
local buf = vim.api.nvim_get_current_buf()
|
||||||
vim.lsp.buf.format()
|
local ft = vim.bo[buf].filetype
|
||||||
end
|
local have_nls = #require("null-ls.sources").get_available(ft, "NULL_LS_FORMATTING") > 0
|
||||||
end
|
|
||||||
|
|
||||||
function M.nls_formatter(ft)
|
vim.lsp.buf.format({
|
||||||
local sources = require("null-ls.sources")
|
bufnr = buf,
|
||||||
local available = sources.get_available(ft, "NULL_LS_FORMATTING")
|
filter = function(client)
|
||||||
return #available > 0
|
if have_nls then
|
||||||
|
return client.name == "null-ls"
|
||||||
|
end
|
||||||
|
return client.name ~= "null-ls"
|
||||||
|
end,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.on_attach(client, buf)
|
function M.on_attach(client, buf)
|
||||||
local ft = vim.api.nvim_buf_get_option(buf, "filetype")
|
if client.supports_method("textDocument/formatting") then
|
||||||
|
|
||||||
local enable = false
|
|
||||||
if M.nls_formatter(ft) then
|
|
||||||
enable = client.name == "null-ls"
|
|
||||||
else
|
|
||||||
enable = not (client.name == "null-ls")
|
|
||||||
end
|
|
||||||
|
|
||||||
client.server_capabilities.documentFormattingProvider = enable
|
|
||||||
-- format on save
|
|
||||||
if client.server_capabilities.documentFormattingProvider then
|
|
||||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||||
group = vim.api.nvim_create_augroup("LspFormat", {}),
|
group = vim.api.nvim_create_augroup("LspFormat", {}),
|
||||||
buffer = 0,
|
buffer = buf,
|
||||||
callback = function()
|
callback = function()
|
||||||
M.format()
|
if M.autoformat then
|
||||||
|
M.format()
|
||||||
|
end
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
local servers = require("user.plugins.lsp.servers")
|
local servers = require("user.plugins.lsp.servers")
|
||||||
|
|
||||||
local function on_attach(client, bufnr)
|
local function on_attach(client, bufnr)
|
||||||
require("user.plugins.lsp.format").on_attach(client, bufnr)
|
require("user.plugins.lsp.format").on_attach(client, bufnr)
|
||||||
require("user.plugins.lsp.keymaps").on_attach(client, bufnr)
|
require("user.plugins.lsp.keymaps").on_attach(client, bufnr)
|
||||||
|
@ -17,14 +18,10 @@ return {
|
||||||
"hrsh7th/cmp-nvim-lsp",
|
"hrsh7th/cmp-nvim-lsp",
|
||||||
},
|
},
|
||||||
config = function()
|
config = function()
|
||||||
---@type _.lspconfig.options
|
local capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities())
|
||||||
local defaults = {
|
|
||||||
on_attach = on_attach,
|
|
||||||
capabilities = require("cmp_nvim_lsp").default_capabilities(vim.lsp.protocol.make_client_capabilities()),
|
|
||||||
}
|
|
||||||
|
|
||||||
for server, opts in pairs(servers) do
|
for server, opts in pairs(servers) do
|
||||||
opts = vim.tbl_deep_extend("force", {}, defaults, opts or {})
|
opts.capabilities = capabilities
|
||||||
|
opts.on_attach = on_attach
|
||||||
require("lspconfig")[server].setup(opts)
|
require("lspconfig")[server].setup(opts)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue