mirror of
https://github.com/tiyn/tidy.nvim.git
synced 2025-10-10 10:11:16 +02:00
docs, feat, refactor:
- add user configuration ability - add two new patterns - remove empty lines at top of buffer - condense all multiple empty lines into one - update README - GIFs displaying formatting options - default config and how to modify - new packer entry - credits - remove vim file and integrate aucmd into lua
This commit is contained in:
@@ -1,23 +1,54 @@
|
||||
-- MP
|
||||
local M = {
|
||||
config = {
|
||||
eof_quant = -1,
|
||||
fmts = {
|
||||
"sof",
|
||||
"eof",
|
||||
"multi",
|
||||
"ws"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function M.setup(user_config)
|
||||
|
||||
local M = {}
|
||||
-- integrate user configuration
|
||||
M.config = vim.tbl_deep_extend("force", M.config, user_config or {})
|
||||
|
||||
vim.cmd[[
|
||||
augroup Tidy
|
||||
au!
|
||||
au BufWritePre * lua require( "tidy" ).tidy_up()
|
||||
augroup END
|
||||
]]
|
||||
end
|
||||
|
||||
function M.tidy_up()
|
||||
local cmd_mods = ":keepjumps keeppatterns silent! "
|
||||
|
||||
local patterns = {
|
||||
-- delete all new lines at beginning of file
|
||||
|
||||
sof = [[%s/\%^\n*/]],
|
||||
-- delete all lines at end of buffer, see source 2
|
||||
|
||||
eof = [[0;/^\%(\n*.\)\@!/ + ]] .. M.config.eof_quant .. ",$d",
|
||||
-- compress all instances of multiple newlines into one
|
||||
|
||||
multi = [[:%s/\n\{2,}/\r\r/e]],
|
||||
-- delete all whitespace, see source 1
|
||||
ws = [[%s/\s\+$//e]]
|
||||
}
|
||||
|
||||
-- get tuple of cursor position before making changes
|
||||
local pos = vim.api.nvim_win_get_cursor( 0 )
|
||||
local pos = vim.api.nvim_win_get_cursor(0)
|
||||
|
||||
-- delete all whitespace, see source 1
|
||||
vim.cmd[[:keepjumps keeppatterns %s/\s\+$//e]]
|
||||
|
||||
-- delete all lines at end of buffer, see source 2
|
||||
vim.cmd[[:keepjumps keeppatterns silent! 0;/^\%(\n*.\)\@!/,$d]]
|
||||
for _, fmt_type in ipairs(M.config.fmts) do
|
||||
vim.cmd(cmd_mods .. patterns[fmt_type])
|
||||
end
|
||||
|
||||
-- get row count after line deletion
|
||||
local end_row = vim.api.nvim_buf_line_count( 0 )
|
||||
|
||||
local end_row = vim.api.nvim_buf_line_count(0)
|
||||
|
||||
--[[
|
||||
if the row value in the original cursor
|
||||
@@ -32,8 +63,7 @@ function M.tidy_up()
|
||||
pos[1] = end_row
|
||||
end
|
||||
|
||||
vim.api.nvim_win_set_cursor( 0, pos )
|
||||
vim.api.nvim_win_set_cursor(0, pos)
|
||||
end
|
||||
|
||||
|
||||
return M
|
||||
|
Reference in New Issue
Block a user