nvim: add external formatter support

dev-docs
Moritz Böhme 2023-02-18 13:35:11 +01:00
parent 9cf8f7e827
commit 6e6bb3422c
Signed by: moritz
GPG Key ID: 970C6E89EB0547A9
2 changed files with 76 additions and 32 deletions

View File

@ -44,6 +44,7 @@ in
extraPackages = with pkgs; [
sumneko-lua-language-server
nil
stylua
];
plugins = with pkgs.vimPlugins; [
catppuccin-nvim
@ -55,6 +56,7 @@ in
lualine-lsp-progress
lualine-nvim
luasnip
formatter-nvim
neogit
noice-nvim
nui-nvim # for noice-nvim

View File

@ -133,38 +133,6 @@ cmp.setup({
},
})
-- The nvim-cmp almost supports LSP's capabilities so You should advertise it to LSP servers..
local capabilities = require("cmp_nvim_lsp").default_capabilities()
local lspconfig = require("lspconfig")
local on_attach_def = function(_, bufnr)
wk.register({
K = { vim.lsp.buf.hover, "show info" },
["<leader>l"] = {
name = "lsp",
d = { vim.diagnostic.open_float, "open diagnostic window" },
n = { vim.diagnostic.goto_next, "next error" },
p = { vim.diagnostic.goto_prev, "prev error" },
c = { vim.lsp.buf.code_action, "code action" },
r = { vim.lsp.buf.rename, "rename" },
f = {
function()
vim.lsp.buf.format({ async = true })
end,
"format",
},
},
g = {
name = "goto",
r = { vim.lsp.buf.references, "references" },
d = { vim.lsp.buf.definition, "definition" },
D = { vim.lsp.buf.declaration, "declaration" },
i = { vim.lsp.buf.implementation, "implementation" },
t = { vim.lsp.buf.type_definition, "type defininition" },
},
}, { noremap = true, silent = true, buffer = bufnr })
end
---merge tables
---@param ... table[]
---@return table
@ -193,6 +161,80 @@ local function table_merge(...)
return result
end
-- Provides the Format, FormatWrite, FormatLock, and FormatWriteLock commands
require("formatter").setup({
-- Enable or disable logging
logging = true,
-- Set the log level
log_level = vim.log.levels.WARN,
-- All formatter configurations are opt-in
filetype = {
-- Formatter configurations for filetype "lua" go here
-- and will be executed in order
lua = {
-- "formatter.filetypes.lua" defines default configurations for the
-- "lua" filetype
require("formatter.filetypes.lua").stylua,
},
-- Use the special "*" filetype for defining formatter configurations on
-- any filetype
["*"] = {
-- "formatter.filetypes.any" defines default configurations for any
-- filetype
require("formatter.filetypes.any").remove_trailing_whitespace,
},
},
})
wk.register({
["="] = { "<cmd>Format<cr>", "format (formatter)" },
}, { noremap = true, silent = true })
-- The nvim-cmp almost supports LSP's capabilities so You should advertise it to LSP servers..
local capabilities = require("cmp_nvim_lsp").default_capabilities()
local lspconfig = require("lspconfig")
local on_attach_def = function(client, bufnr)
local options = { noremap = true, silent = true } -- default options
local buffer_options = table_merge(options, { buffer = bufnr }) -- buffer specific options
-- set format keybinding
-- prefere lsp formatting over external formatter
if client.server_capabilities.documentFormattingProvider then
local modes = { "n", "v" }
for _, mode in ipairs(modes) do
wk.register({
["="] = {
function()
vim.lsp.buf.format({ async = true })
end,
"format (lsp)",
},
}, table_merge(buffer_options, { mode = mode }))
end
end
wk.register({
K = { vim.lsp.buf.hover, "show info" },
["<leader>l"] = {
name = "lsp",
d = { vim.diagnostic.open_float, "open diagnostic window" },
n = { vim.diagnostic.goto_next, "next error" },
p = { vim.diagnostic.goto_prev, "prev error" },
c = { vim.lsp.buf.code_action, "code action" },
r = { vim.lsp.buf.rename, "rename" },
},
g = {
name = "goto",
r = { vim.lsp.buf.references, "references" },
d = { vim.lsp.buf.definition, "definition" },
D = { vim.lsp.buf.declaration, "declaration" },
i = { vim.lsp.buf.implementation, "implementation" },
t = { vim.lsp.buf.type_definition, "type defininition" },
},
}, buffer_options)
end
local lspconfig_default_options = {
on_attach = on_attach_def,
capabilities = capabilities,