diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-24 21:24:54 +0200 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-06-24 21:24:54 +0200 |
| commit | d5887f31a416034300a8675d8883d615b31ced83 (patch) | |
| tree | 7c2b105a8a56ed4c2a9ca6b5d6e9b1b338934f77 /plugin | |
| parent | b0130288e93e00d5f37a5cf70bc8920216600aa7 (diff) | |
| download | nvim-config-d5887f31a416034300a8675d8883d615b31ced83.tar.gz nvim-config-d5887f31a416034300a8675d8883d615b31ced83.zip | |
refactor(nvim): rewrite formatter plugin
Diffstat (limited to 'plugin')
| -rw-r--r-- | plugin/50-format.lua | 69 |
1 files changed, 13 insertions, 56 deletions
diff --git a/plugin/50-format.lua b/plugin/50-format.lua index d77f1fd..efcaea6 100644 --- a/plugin/50-format.lua +++ b/plugin/50-format.lua @@ -1,70 +1,27 @@ -- -- 50-format.lua -- --- * Specifies the formatters to use for different file types. --- * Creates an autocommand to format on 'BufWritePre'. +-- * Registers a stdin formatter command for each supported file type. +-- * Formatting is applied on write by the dotfiles.format engine; toggle it per buffer with +-- `FormatToggle`. -- -- User commands: -- FormatToggle: toggle formatting on write for the current buffer +-- -local function toggle_format() - vim.b.format = not vim.b.format -end +local format = require("dotfiles.format") -local function format_impl(cmd, lines) - local r = vim.system(cmd, { stdin = lines, text = true }):wait() - if r.code ~= 0 then - vim.notify(cmd[1] .. " error:\n" .. r.stderr, vim.log.levels.WARN) - return nil - end - return vim.split(r.stdout, "\n", { trimempty = true }) -end +format.register("sh", { "shfmt", "-" }) +format.register("bash", { "shfmt", "-" }) +format.register("lua", { "stylua", "--search-parent-directories", "-" }) +format.register("markdown", { "mdformat", "-" }) +format.register("python", { "black", "-" }) -local formatters = { - ["sh"] = function(lines) - return format_impl({ "shfmt", "-" }, lines) - end, - ["lua"] = function(lines) - return format_impl({ "stylua", "--search-parent-directories", "-" }, lines) - end, - ["markdown"] = function(lines) - return format_impl({ "mdformat", "-" }, lines) - end, - ["python"] = function(lines) - return format_impl({ "black", "-" }, lines) - end, -} -formatters.bash = formatters.sh - ---- Format the buffer `ev.buf` with the formatter for its filetype. ---- This function is meant to be an event handler for `vim.api.nvim_create_autocmd()` ---- @param ev table `ev` argument passed from `vim.api.nvim_create_autocmd()` -local function format_buffer(ev) - if vim.b[ev.buf].format ~= true then - return - end - local ft = vim.bo[ev.buf].filetype - local format = formatters[ft] - if not format then - vim.notify("No formatter registered for " .. ft, vim.log.levels.ERROR) - return - end - local lines_in = vim.api.nvim_buf_get_lines(ev.buf, 0, -1, false) - local lines_out = format(lines_in) - if not lines_out or vim.deep_equal(lines_in, lines_out) then - return - end - local view = vim.fn.winsaveview() - vim.api.nvim_buf_set_lines(ev.buf, 0, -1, false, lines_out) - vim.fn.winrestview(view) +-- Toggle formatting on write for the current buffer. +local function toggle_format() + vim.b.format = not vim.b.format end ---------------------------------------------------------------------------------------------------- -vim.api.nvim_create_autocmd("BufWritePre", { - desc = "Format buffer", - group = vim.g.dotfiles.augroup, - callback = format_buffer, -}) - vim.api.nvim_create_user_command("FormatToggle", toggle_format, { desc = "Toggle format on save" }) |
