mirror of https://github.com/tiyn/tidy.nvim
• 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 #4main
parent
1f1aa06991
commit
9f906a154d
@ -1,32 +1,37 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.tidy_up()
|
function M.setup()
|
||||||
-- get tuple of cursor position before making changes
|
local tidy_grp = vim.api.nvim_create_augroup("tidy", { clear = true })
|
||||||
local pos = vim.api.nvim_win_get_cursor(0)
|
|
||||||
|
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||||
-- delete all whitespace, see source 1
|
group = tidy_grp,
|
||||||
vim.cmd([[:keepjumps keeppatterns %s/\s\+$//e]])
|
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
|
-- delete all lines at end of buffer, see source 2
|
||||||
local end_row = vim.api.nvim_buf_line_count(0)
|
vim.cmd([[:keepjumps keeppatterns silent! 0;/^\%(\n*.\)\@!/,$d]])
|
||||||
|
|
||||||
--[[
|
-- get row count after line deletion
|
||||||
if the row value in the original cursor
|
local last_row = vim.api.nvim_buf_line_count(0)
|
||||||
position tuple is greater than the
|
|
||||||
line count after empty line deletion
|
--[[
|
||||||
(meaning that the cursor was inside of
|
if the row value in the original cursor
|
||||||
the group of empty lines at the end of
|
position tuple is greater than the
|
||||||
the file when they were deleted), set
|
line count after empty line deletion
|
||||||
the cursor row to the last line
|
(meaning that the cursor was inside of
|
||||||
]]
|
the group of empty lines at the end of
|
||||||
if pos[1] > end_row then
|
the file when they were deleted), set
|
||||||
pos[1] = end_row
|
the cursor row to the last line.
|
||||||
end
|
]]
|
||||||
|
if pos[1] > last_row then
|
||||||
vim.api.nvim_win_set_cursor(0, pos)
|
pos[1] = last_row
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.api.nvim_win_set_cursor(0, pos)
|
||||||
|
end,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
augroup Tidy
|
|
||||||
au!
|
|
||||||
au BufWritePre * lua require( "tidy" ).tidy_up()
|
|
||||||
augroup END
|
|
Loading…
Reference in new issue