diff --git a/README.md b/README.md index 9d3d30a..e82a363 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # tidy.nvim 🧹 -A function and autocommand pair that removes all +An [autocommand](https://neovim.io/doc/user/autocmd.html) that removes all - trailing whitespace - empty lines at the end of the buffer @@ -12,11 +12,11 @@ on every `BufWritePre`. ## About -I originally wrote this as a wrapper around a couple of vim regex commands used for formatting files before I began using formatters. These commands are not mine, please see the `Credits` section below for sources. My contribution is a basic conditional which ensures that the user's cursor doesn't migrate during the cleaning operations conducted on the buffer's contents unless the cursor is in the removed white space. Even with real formatters in my setup now, I still like and use this because I like these specific formats to be applied to every buffer and don't want to have a formatting tool installed for them. There really isn't a reason to have this in a plugin other than wanting to disseminate it for new users or people who didn't know you could do this. You could (should) instead just yank and put the code right in your configuration. +I originally wrote this as a wrapper around a couple of vim regex commands used for formatting files before I began using formatters. These commands are not mine, please see the `Credits` section below for sources. Even with real formatters in my setup now, I still like and use this because I like these specific formats to be applied to every buffer and don't want to have a formatting tool installed for them. There really isn't a reason to have this in a plugin other than wanting to disseminate it for new users or people who didn't know you could do this. You could (should) instead just yank and put the code right in your configuration. ## Installation -- Packer +- [packer.nvim](https://github.com/wbthomason/packer.nvim) ```lua use({ @@ -27,6 +27,16 @@ use({ }) ``` +## Configuration +Tidy will work on all buffers using only the basic installation shown above. All configuration options are optional and there are no defaults. The options displayed below are simply examples. + +```lua +require("tidy").setup({ + filetype_exclude = { "markdown", "python" }, +}) +``` + + ## Credits - [Vim Tips Wiki entry for removing unwanted spaces](https://vim.fandom.com/wiki/Remove_unwanted_spaces#Automatically_removing_all_trailing_whitespace) diff --git a/lua/tidy/init.lua b/lua/tidy/init.lua index 0fe1299..23a317d 100644 --- a/lua/tidy/init.lua +++ b/lua/tidy/init.lua @@ -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