fix(nvim): migrate formatting settings to Neovim 0.8 API

This commit is contained in:
Stefan Imhoff
2022-10-08 10:25:23 +02:00
parent 4456853ddc
commit 5f718170c0
4 changed files with 36 additions and 34 deletions

View File

@@ -116,14 +116,14 @@ local mappings = {
}, },
L = { L = {
name = "LSP", name = "LSP",
c = { "<CMD>vim.lsp.buf.code_action()<CR>", "Code Action" }, c = { "<CMD>lua vim.lsp.buf.code_action()<CR>", "Code Action" },
f = { "<CMD>vim.lsp.buf.formatting()<CR>", "Formatting" }, f = { "<CMD>lua vim.lsp.buf.format()<CR>", "Formatting" },
l = { "<CMD>vim.diagnostic.setloclist<CR>", "Set Loclist" }, l = { "<CMD>lua vim.diagnostic.setloclist<CR>", "Set Loclist" },
r = { "<CMD>vim.lsp.buf.rename()<CR>", "Rename" }, r = { "<CMD>lua vim.lsp.buf.rename()<CR>", "Rename" },
t = { "<CMD>vim.lsp.buf.type_definition()<CR>", "Type Definition" }, t = { "<CMD>lua vim.lsp.buf.type_definition()<CR>", "Type Definition" },
w = { "<CMD>function() print(vim.inspect(vim.lsp.buf.list_workspace_folders()))", "List Workspace Folder" }, w = { "<CMD>function() print(vim.inspect(vim.lsp.buf.list_workspace_folders()))", "List Workspace Folder" },
a = { "<CMD>vim.lsp.buf.add_workspace_folder()<CR>", "Add Workspace" }, a = { "<CMD>lua vim.lsp.buf.add_workspace_folder()<CR>", "Add Workspace" },
v = { "<CMD>vim.lsp.buf.remove_workspace_folder()<CR>", "Remove Workspace" }, v = { "<CMD>lua vim.lsp.buf.remove_workspace_folder()<CR>", "Remove Workspace" },
}, },
P = { P = {
name = "Packer", name = "Packer",

View File

@@ -109,6 +109,6 @@ vim.cmd([[
augroup _lsp augroup _lsp
autocmd! autocmd!
autocmd BufWritePre * lua vim.lsp.buf.formatting() autocmd BufWritePre * lua vim.lsp.buf.format()
augroup end augroup end
]]) ]])

View File

@@ -147,4 +147,4 @@ keymap("o", "al", ":<c-u>normal! $v0<CR>", opts)
keymap("x", "al", ":<c-u>normal! $v0<CR>", opts) keymap("x", "al", ":<c-u>normal! $v0<CR>", opts)
-- LSP formatting -- LSP formatting
keymap("n", "<leader>o", "<cmd>lua vim.lsp.buf.formatting_sync()<CR>", opts) keymap("n", "<leader>o", "<cmd>lua vim.lsp.buf.format()<CR>", opts)

View File

@@ -41,19 +41,23 @@ M.setup = function()
}) })
end end
local function lsp_highlight_document(client) local function lsp_highlight_document(client, bufnr)
-- Set autocommands conditional on server_capabilities -- Set autocommands conditional on server_capabilities
if client.resolved_capabilities.document_highlight then if client.server_capabilities.documentHighlightProvider then
vim.api.nvim_exec( vim.api.nvim_create_augroup("lsp_document_highlight", { clear = true })
[[ vim.api.nvim_clear_autocmds({ buffer = bufnr, group = "lsp_document_highlight" })
augroup lsp_document_highlight vim.api.nvim_create_autocmd("CursorHold", {
autocmd! * <buffer> callback = vim.lsp.buf.document_highlight,
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight() buffer = bufnr,
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references() group = "lsp_document_highlight",
augroup END desc = "Document Highlight",
]], })
false vim.api.nvim_create_autocmd("CursorMoved", {
) callback = vim.lsp.buf.clear_references,
buffer = bufnr,
group = "lsp_document_highlight",
desc = "Clear All the References",
})
end end
end end
@@ -76,11 +80,9 @@ local function lsp_keymaps(bufnr)
keymap(bufnr, "n", "ä", "<cmd>lua vim.diagnostic.goto_next()<CR>", opts) keymap(bufnr, "n", "ä", "<cmd>lua vim.diagnostic.goto_next()<CR>", opts)
end end
-- TODO: Neovim 0.8 https://github.com/jose-elias-alvarez/null-ls.nvim/wiki/Avoiding-LSP-formatting-conflicts
-- https://github.com/jose-elias-alvarez/null-ls.nvim/wiki/Formatting-on-save -- https://github.com/jose-elias-alvarez/null-ls.nvim/wiki/Formatting-on-save
local lsp_formatting = function(bufnr) local lsp_formatting = function(bufnr)
-- vim.lsp.buf.format({}) vim.lsp.buf.format({
vim.lsp.buf.formatting_sync({
bufnr = bufnr, bufnr = bufnr,
filter = function(client) filter = function(client)
return client.name == "null-ls" return client.name == "null-ls"
@@ -105,47 +107,47 @@ M.on_attach = function(client, bufnr)
-- TypeScript -- TypeScript
if client.name == "tsserver" then if client.name == "tsserver" then
client.resolved_capabilities.document_formatting = false client.server_capabilities.documentFormattingProvider = false
end end
-- HTML -- HTML
if client.name == "html" then if client.name == "html" then
client.resolved_capabilities.document_formatting = false client.server_capabilities.documentFormattingProvider = false
end end
-- Stylelint -- Stylelint
if client.name == "stylelint_lsp" then if client.name == "stylelint_lsp" then
client.resolved_capabilities.document_formatting = false client.server_capabilities.documentFormattingProvider = false
end end
-- JSON -- JSON
if client.name == "jsonls" then if client.name == "jsonls" then
client.resolved_capabilities.document_formatting = false client.server_capabilities.documentFormattingProvider = false
end end
-- Lua -- Lua
if client.name == "sumneko_lua" then if client.name == "sumneko_lua" then
client.resolved_capabilities.document_formatting = false client.server_capabilities.documentFormattingProvider = false
client.resolved_capabilities.document_range_formatting = false client.server_capabilities.documentRangeFormattingProvider = false
end end
-- Rust -- Rust
if client.name == "rust_analyzer" then if client.name == "rust_analyzer" then
client.resolved_capabilities.document_formatting = false client.server_capabilities.documentFormattingProvider = false
end end
-- Astro -- Astro
if client.name == "astro" then if client.name == "astro" then
client.resolved_capabilities.document_formatting = false client.server_capabilities.documentFormattingProvider = false
end end
-- Diagnostic -- Diagnostic
if client.name == "diagnosticls" then if client.name == "diagnosticls" then
client.resolved_capabilities.document_formatting = false client.server_capabilities.documentFormattingProvider = false
end end
lsp_keymaps(bufnr) lsp_keymaps(bufnr)
lsp_highlight_document(client) lsp_highlight_document(client, bufnr)
end end
local capabilities = vim.lsp.protocol.make_client_capabilities() local capabilities = vim.lsp.protocol.make_client_capabilities()