diff options
Diffstat (limited to '.config/nvim')
| -rw-r--r-- | .config/nvim/after/ftplugin/bash.lua | 17 | ||||
| -rw-r--r-- | .config/nvim/after/ftplugin/lua.lua | 29 | ||||
| -rw-r--r-- | .config/nvim/after/ftplugin/markdown.lua | 56 | ||||
| -rw-r--r-- | .config/nvim/after/ftplugin/python.lua | 12 | ||||
| -rw-r--r-- | .config/nvim/after/ftplugin/sh.lua | 7 | ||||
| -rw-r--r-- | .config/nvim/plugin/50-autoformat.lua | 24 | ||||
| -rw-r--r-- | .config/nvim/plugin/50-format.lua | 67 |
7 files changed, 77 insertions, 135 deletions
diff --git a/.config/nvim/after/ftplugin/bash.lua b/.config/nvim/after/ftplugin/bash.lua deleted file mode 100644 index 6e10433..0000000 --- a/.config/nvim/after/ftplugin/bash.lua +++ /dev/null @@ -1,17 +0,0 @@ -vim.opt_local.textwidth = vim.g.dotfiles.textwidth.sh -- gw wraps at this value -vim.opt_local.colorcolumn = "+1" -- Highlight one column after 'textwidth' -vim.opt_local.complete = { - "o", -- 'omnifunc' -} - -vim.b.autoformat = true -vim.b.format_func = function() - local ret = vim.system({ "shfmt" }, { stdin = vim.api.nvim_buf_get_lines(0, 0, -1, false) }):wait() - if ret.code ~= 0 then - vim.notify("shfmt failed (" .. ret.code .. "):\n" .. ret.stderr, vim.log.levels.WARN) - return - end - local view = vim.fn.winsaveview() - vim.cmd("%!shfmt") - vim.fn.winrestview(view) -end diff --git a/.config/nvim/after/ftplugin/lua.lua b/.config/nvim/after/ftplugin/lua.lua index b7d29c1..2b2f2c5 100644 --- a/.config/nvim/after/ftplugin/lua.lua +++ b/.config/nvim/after/ftplugin/lua.lua @@ -1,29 +1,3 @@ -local function format() - local view = vim.fn.winsaveview() - local buf_str = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n") .. "\n" - local tempname = vim.fn.tempname() - local tempfile = assert(io.open(tempname, "w"), "Could not open temporary file") - tempfile:write(buf_str) - tempfile:close() - local r_check = vim.system({ "stylua", "--check", tempname }):wait() - if r_check.code == 0 then - return - elseif r_check.code ~= 1 then - vim.notify("stylua failed (" .. r_check.code .. "):\n" .. r_check.stderr) - end - local r_format = vim.system({ "stylua", tempname }):wait() - if r_format.code ~= 0 then - vim.notify("stylua failed (" .. r_format.code .. "):\n" .. r_format.stderr) - end - local formatted_lines = {} - for line in io.lines(tempname) do - formatted_lines[#formatted_lines + 1] = line - end - os.remove(tempname) - vim.api.nvim_buf_set_lines(0, 0, -1, false, formatted_lines) - vim.fn.winrestview(view) -end - vim.opt_local.textwidth = vim.g.dotfiles.textwidth.lua -- gw wraps at this value vim.opt_local.colorcolumn = "+1" -- Highlight one column after 'textwidth' vim.opt_local.complete = { @@ -31,8 +5,7 @@ vim.opt_local.complete = { "F", -- 'completefunc' (snippet source, see plugin/50-completion.lua) } -vim.b.autoformat = true -vim.b.format_func = format +vim.b.format = true -- See plugin/50-format.lua ------------------------------------------------------------------------------------------------------------------------ -- Snippets diff --git a/.config/nvim/after/ftplugin/markdown.lua b/.config/nvim/after/ftplugin/markdown.lua index d492855..f0f9691 100644 --- a/.config/nvim/after/ftplugin/markdown.lua +++ b/.config/nvim/after/ftplugin/markdown.lua @@ -1,56 +1,3 @@ -local function format() - local view = vim.fn.winsaveview() - local buf_str = table.concat(vim.api.nvim_buf_get_lines(0, 0, -1, false), "\n") .. "\n" - local tempname = vim.fn.tempname() - local tempfile = assert(io.open(tempname, "w"), "Could not open temporary file") - tempfile:write(buf_str) - tempfile:close() - - local r_check = vim.system({ - "mdformat", - "--number", - "--extensions", - "tables", - "--extensions", - "frontmatter", - "--extensions", - "wikilink", - "--wrap", - tostring(vim.bo.textwidth), - "--check", - tempname, - }):wait() - if r_check.code == 0 then - return - elseif r_check.code ~= 1 then - vim.notify("mdformat failed (" .. r_check.code .. "):\n" .. r_check.stderr) - end - local r_format = vim.system({ - "mdformat", - "--number", - "--extensions", - "tables", - "--extensions", - "frontmatter", - "--extensions", - "wikilink", - "--wrap", - tostring(vim.bo.textwidth), - tempname, - }):wait() - if r_format.code ~= 0 then - vim.notify("mdformat failed (" .. r_format.code .. "):\n" .. r_format.stderr) - end - local formatted_lines = {} - for line in io.lines(tempname) do - formatted_lines[#formatted_lines + 1] = line - end - os.remove(tempname) - vim.api.nvim_buf_set_lines(0, 0, -1, false, formatted_lines) - - vim.fn.winrestview(view) -end - vim.opt_local.tabstop = 2 -- CommonMark expects two spaces for indentation vim.opt_local.shiftwidth = 0 vim.opt_local.softtabstop = -1 @@ -74,8 +21,7 @@ vim.opt_local.complete = { -- Visible link labels vim.api.nvim_set_hl(0, "@markup.link.label.markdown_inline", { underline = true, update = true }) -vim.b.autoformat = true -vim.b.format_func = format +vim.b.format = true -- See plugin/50-format.lua local function item_range() local cursor_row = vim.api.nvim_win_get_cursor(0)[1] diff --git a/.config/nvim/after/ftplugin/python.lua b/.config/nvim/after/ftplugin/python.lua index fa39ee8..63045ed 100644 --- a/.config/nvim/after/ftplugin/python.lua +++ b/.config/nvim/after/ftplugin/python.lua @@ -4,14 +4,4 @@ vim.opt_local.complete = { "o", -- 'omnifunc' } -vim.b.autoformat = true -vim.b.format_func = function() - local ret = vim.system({ "black", "--check", "-" }, { stdin = vim.api.nvim_buf_get_lines(0, 0, -1, false) }):wait() - if ret.code > 1 then - vim.notify("black failed (" .. ret.code .. "):\n" .. ret.stderr, vim.log.levels.WARN) - return - end - local view = vim.fn.winsaveview() - vim.cmd("%!black --quiet -") - vim.fn.winrestview(view) -end +vim.b.format = true -- See plugin/50-format.lua diff --git a/.config/nvim/after/ftplugin/sh.lua b/.config/nvim/after/ftplugin/sh.lua new file mode 100644 index 0000000..3574ac0 --- /dev/null +++ b/.config/nvim/after/ftplugin/sh.lua @@ -0,0 +1,7 @@ +vim.opt_local.textwidth = vim.g.dotfiles.textwidth.sh -- gw wraps at this value +vim.opt_local.colorcolumn = "+1" -- Highlight one column after 'textwidth' +vim.opt_local.complete = { + "o", -- 'omnifunc' +} + +vim.b.format = true -- See plugin/50-format.lua diff --git a/.config/nvim/plugin/50-autoformat.lua b/.config/nvim/plugin/50-autoformat.lua deleted file mode 100644 index 4761097..0000000 --- a/.config/nvim/plugin/50-autoformat.lua +++ /dev/null @@ -1,24 +0,0 @@ --- --- Autoformat plugin --- - -local function toggle_autoformat() - vim.b.autoformat = not vim.b.autoformat -end - --- Autoformat -vim.api.nvim_create_autocmd("BufWritePre", { - desc = "Autoformat buffer", - group = vim.g.dotfiles.augroup, - callback = function(ev) - if vim.b.autoformat then - if vim.b.format_func == nil then - vim.notify("No formatter set for " .. ev.file .. " (" .. vim.o.filetype .. ")", vim.log.levels.WARN) - return - end - vim.b.format_func() - end - end, -}) - -vim.api.nvim_create_user_command("AutoformatToggle", toggle_autoformat, { desc = "Toggle autoformatting" }) diff --git a/.config/nvim/plugin/50-format.lua b/.config/nvim/plugin/50-format.lua new file mode 100644 index 0000000..46a245c --- /dev/null +++ b/.config/nvim/plugin/50-format.lua @@ -0,0 +1,67 @@ +-- +-- 50-format.lua +-- +-- Specifies the formatters to use for different file types and sets up a global autocommand to trigger them. +-- +-- User commands: +-- FormatToggle: toggle formatting on save on/off for the current buffer + +local function toggle_format() + vim.b.format = not vim.b.format +end + +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 + +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 on_format(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) +end + +vim.api.nvim_create_autocmd("BufWritePre", { + desc = "Format buffer", + group = vim.g.dotfiles.augroup, + callback = on_format, +}) + +vim.api.nvim_create_user_command("FormatToggle", toggle_format, { desc = "Toggle format on save" }) |
