Very small plugin to run Uncrustify from Neovim
I tried to make this work with the existing null-ls builtin but I wasn't able to for two reasons:
- I couldn't figure out how to get it to pass only the selected text to stdin of the command it runs
- null-ls has been archived, so even if I could get it to work, I couldn't merge it into the repo
Maybe someone else could figure it out, but this didn't take very long to make and it was fun.
Packer:
use({
"rickyelopez/uncrustify.nvim",
requires = { "nvim-lua/plenary.nvim" },
config = function()
require("uncrustify").setup({format_timeout = 5000})
end,
})
Call the setup()
function (either with Packer as shown above, or elsewhere in your config)
if you want to modify any of the default options. Available options are:
uncrustify_bin_path
default:"uncrustify"
Path to use to run uncrustifyuncrustify_cfg_path
default:vim.env.HOME .. "/.config/uncrustify.cfg"
Path to the uncrustify config file
note: this does not presently expand env vars. Usevim.env.VAR_NAME
to construct the path.filetype_mapping
default:{ c = "c", cpp = "cpp" }
Table mapping vim filetypes to uncrustify filetypesformat_timeout
default: 3000
Timeout when running uncrustify
Define a keybind that calls the format()
function in normal or visual modes. Eg. for clangd
and lspconfig
:
local on_attach = function(client, bufnr)
if client.name and client.name == "clangd" then
vim.keymap.set({ "n", "v" }, "<leader>f", require("uncrustify").format, { noremap = true })
end
end
The above on_attach
function can then be passed to the lspconfig
setup function for clangd, in this case.
Note: in visual mode, Uncrustify is run with the --frag
argument, which makes it assume that the first
line is indented correctly.