From 889dc71f200f76e3b115fd3af92671f0d669acbe Mon Sep 17 00:00:00 2001 From: McAuley Penney Date: Fri, 15 Jul 2022 13:22:41 -0700 Subject: [PATCH] refactor: use default settings in place of conditionals - instead of checking for the existence of an option whenever that option may be accessed, initialize default settings and merge them with user-provided settings. - instead of looping through all excluded filetypes and comparing them to the current filetype, create a set from excluded filetypes and check if the current filetype is present. --- lua/tidy/init.lua | 78 ++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/lua/tidy/init.lua b/lua/tidy/init.lua index b31c17b..26dfcd9 100644 --- a/lua/tidy/init.lua +++ b/lua/tidy/init.lua @@ -1,25 +1,30 @@ local M = {} +local function list_to_set(list) + local set = {} + + for _, item in ipairs(list) do + set[item] = true + end + + return set +end + local function is_excluded_ft(opts) - if not opts or 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 - + if not opts.filetype_exclude then return false + end + + local ft = vim.api.nvim_buf_get_option(0, "filetype") + local ft_set = list_to_set(opts.filetype_exclude) + + return ft_set[ft] end local function reset_cursor_pos(pos) - local num_rows = vim.api.nvim_buf_line_count(0) + 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 @@ -28,33 +33,42 @@ local function reset_cursor_pos(pos) 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 + if pos[1] > num_rows then + pos[1] = num_rows + end - vim.api.nvim_win_set_cursor(0, pos) + 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() - if is_excluded_ft(opts) then - return false - end + local defaults = { + filetype_exclude = {}, + newline_at_eof = false + } - local cursor_pos = vim.api.nvim_win_get_cursor(0) + opts = vim.tbl_extend("force", defaults, opts or {}) - vim.cmd([[:keepjumps keeppatterns %s/\s\+$//e]]) + local tidy_grp = vim.api.nvim_create_augroup("tidy", { clear = true }) - -- delete all lines at end of buffer, see source 2 - vim.cmd([[:keepjumps keeppatterns silent! 0;/^\%(\n*.\)\@!/,$d]]) + vim.api.nvim_create_autocmd("BufWritePre", { + group = tidy_grp, + callback = function() + if is_excluded_ft(opts) then + return false + end - reset_cursor_pos(cursor_pos) - end, - }) + local cursor_pos = vim.api.nvim_win_get_cursor(0) + + -- delete trailing whitespace + vim.cmd([[:keepjumps keeppatterns %s/\s\+$//e]]) + + -- delete lines @ eof + vim.cmd([[:keepjumps keeppatterns silent! 0;/^\%(\n*.\)\@!/,$d]]) + + reset_cursor_pos(cursor_pos) + end, + }) end return M