mirror of
https://github.com/tiyn/tidy.nvim.git
synced 2025-10-10 10:11:16 +02:00
feat: add excluded filetypes config opt
• Users may now choose to pass a list of filetypes for Tidy to ignore to the setup function. It is not required and will be ignored if it does not exist. Resolves #5
This commit is contained in:
@@ -1,35 +1,58 @@
|
||||
local M = {}
|
||||
|
||||
function M.setup()
|
||||
local function is_excluded_ft(opts)
|
||||
if not opts.filetype_exclude then
|
||||
return false
|
||||
end
|
||||
|
||||
local ft = vim.api.nvim_buf_get_option(0, "filetype")
|
||||
|
||||
for _, entry in ipairs(opts.filetype_exclude) do
|
||||
if entry == ft then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
local function reset_cursor_pos(pos)
|
||||
local num_rows = vim.api.nvim_buf_line_count(0)
|
||||
|
||||
--[[
|
||||
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] > num_rows then
|
||||
pos[1] = num_rows
|
||||
end
|
||||
|
||||
vim.api.nvim_win_set_cursor(0, pos)
|
||||
end
|
||||
|
||||
function M.setup(opts)
|
||||
local tidy_grp = vim.api.nvim_create_augroup("tidy", { clear = true })
|
||||
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
group = tidy_grp,
|
||||
callback = function()
|
||||
local pos = vim.api.nvim_win_get_cursor(0)
|
||||
if is_excluded_ft(opts) then
|
||||
return false
|
||||
end
|
||||
|
||||
local cursor_pos = vim.api.nvim_win_get_cursor(0)
|
||||
|
||||
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]])
|
||||
|
||||
-- get row count after line deletion
|
||||
local last_row = vim.api.nvim_buf_line_count(0)
|
||||
|
||||
--[[
|
||||
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)
|
||||
reset_cursor_pos(cursor_pos)
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
Reference in New Issue
Block a user