A simple Neovim plugin to format current buffer using an external command
MIT License
A simple Neovim plugin to format current buffer using an external command
Thank you https://github.com/ckipp01/stylua-nvim
There are tons of Vim/Neovim plugins which formats code using external commands, e.g.) Yapf.vim, Stylua.vim, and Pretteier.vim. Some of these plugins has some problems:
nvim-format-buffer
is written in Lua so it's fast and does not change cursor positions at all. It also respects your OS settings to find path.
Using Packer.nvim:
use("acro5piano/nvim-format-buffer")
require("nvim-format-buffer").setup({
-- If true, print an error message if command fails. default: false
verbose = false,
format_rules = {
{ pattern = { "*.rs" }, command = "rustfmt --edition 2021" },
-- Stdin as `-` is supported in many formatters
{ pattern = { "*.lua" }, command = "stylua -" },
-- You can pipe multiple commands. No need to escape
{ pattern = { "*.py" }, command = "black -q - | isort -" },
-- Do not include stderr
{ pattern = { "*.tsx", "*.ts", }, command = "prettier --parser typescript 2>/dev/null" },
-- command can be a function which returns string
{
pattern = { "*.ts", "*.tsx", "*.css" , "*.md", "*.astro" },
command = function()
-- Wrap filepath with ' becuase of special filename of Next.js
return "prettier --stdin-filepath " .. "'" .. vim.api.nvim_buf_get_name(0) .. "'"
end,
},
},
})
You can use internal create_format_fn
to control more flow.
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
pattern = { "*.lua" },
callback = function()
require("nvim-format-buffer").format_whole_file("stylua -")
print("Formatted!")
end,
})