docs, refactor: update to new aucmd API

• remove vim source code for implementing tidy's autocommands
      and instead use the Lua API from 0.7

    • update README with improved explanation and new installation
      instructions

    Resolves #4
This commit is contained in:
McAuley Penney
2022-06-01 14:59:17 -07:00
parent 1f1aa06991
commit 9f906a154d
3 changed files with 34 additions and 42 deletions

View File

@@ -1,32 +1,37 @@
local M = {}
function M.tidy_up()
-- get tuple of cursor position before making changes
local pos = vim.api.nvim_win_get_cursor(0)
function M.setup()
local tidy_grp = vim.api.nvim_create_augroup("tidy", { clear = true })
-- delete all whitespace, see source 1
vim.cmd([[:keepjumps keeppatterns %s/\s\+$//e]])
vim.api.nvim_create_autocmd("BufWritePre", {
group = tidy_grp,
callback = function()
local pos = vim.api.nvim_win_get_cursor(0)
-- delete all lines at end of buffer, see source 2
vim.cmd([[:keepjumps keeppatterns silent! 0;/^\%(\n*.\)\@!/,$d]])
vim.cmd([[:keepjumps keeppatterns %s/\s\+$//e]])
-- get row count after line deletion
local end_row = vim.api.nvim_buf_line_count(0)
-- delete all lines at end of buffer, see source 2
vim.cmd([[:keepjumps keeppatterns silent! 0;/^\%(\n*.\)\@!/,$d]])
--[[
if the row value in the original cursor
position tuple is greater than the
line count after empty line deletion
(meaning that the cursor was inside of
the group of empty lines at the end of
the file when they were deleted), set
the cursor row to the last line
]]
if pos[1] > end_row then
pos[1] = end_row
end
-- get row count after line deletion
local last_row = vim.api.nvim_buf_line_count(0)
vim.api.nvim_win_set_cursor(0, pos)
--[[
if the row value in the original cursor
position tuple is greater than the
line count after empty line deletion
(meaning that the cursor was inside of
the group of empty lines at the end of
the file when they were deleted), set
the cursor row to the last line.
]]
if pos[1] > last_row then
pos[1] = last_row
end
vim.api.nvim_win_set_cursor(0, pos)
end,
})
end
return M