mirror of
				https://github.com/tiyn/tidy.nvim.git
				synced 2025-10-31 03:01:22 +01:00 
			
		
		
		
	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.
			
			
This commit is contained in:
		| @@ -1,19 +1,24 @@ | |||||||
| local M = {} | 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) | local function is_excluded_ft(opts) | ||||||
|     if not opts or not opts.filetype_exclude then |   if not opts.filetype_exclude then | ||||||
|     return false |     return false | ||||||
|   end |   end | ||||||
|  |  | ||||||
|   local ft = vim.api.nvim_buf_get_option(0, "filetype") |   local ft = vim.api.nvim_buf_get_option(0, "filetype") | ||||||
|  |   local ft_set = list_to_set(opts.filetype_exclude) | ||||||
|  |  | ||||||
|     for _, entry in ipairs(opts.filetype_exclude) do |   return ft_set[ft] | ||||||
|         if entry == ft then |  | ||||||
|             return true |  | ||||||
|         end |  | ||||||
|     end |  | ||||||
|  |  | ||||||
|     return false |  | ||||||
| end | end | ||||||
|  |  | ||||||
| local function reset_cursor_pos(pos) | local function reset_cursor_pos(pos) | ||||||
| @@ -36,6 +41,14 @@ local function reset_cursor_pos(pos) | |||||||
| end | end | ||||||
|  |  | ||||||
| function M.setup(opts) | function M.setup(opts) | ||||||
|  |  | ||||||
|  |   local defaults = { | ||||||
|  |     filetype_exclude = {}, | ||||||
|  |     newline_at_eof = false | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   opts = vim.tbl_extend("force", defaults, opts or {}) | ||||||
|  |  | ||||||
|   local tidy_grp = vim.api.nvim_create_augroup("tidy", { clear = true }) |   local tidy_grp = vim.api.nvim_create_augroup("tidy", { clear = true }) | ||||||
|  |  | ||||||
|   vim.api.nvim_create_autocmd("BufWritePre", { |   vim.api.nvim_create_autocmd("BufWritePre", { | ||||||
| @@ -47,9 +60,10 @@ function M.setup(opts) | |||||||
|  |  | ||||||
|       local cursor_pos = vim.api.nvim_win_get_cursor(0) |       local cursor_pos = vim.api.nvim_win_get_cursor(0) | ||||||
|  |  | ||||||
|  |       -- delete trailing whitespace | ||||||
|       vim.cmd([[:keepjumps keeppatterns %s/\s\+$//e]]) |       vim.cmd([[:keepjumps keeppatterns %s/\s\+$//e]]) | ||||||
|  |  | ||||||
|             -- delete all lines at end of buffer, see source 2 |       -- delete lines @ eof | ||||||
|       vim.cmd([[:keepjumps keeppatterns silent! 0;/^\%(\n*.\)\@!/,$d]]) |       vim.cmd([[:keepjumps keeppatterns silent! 0;/^\%(\n*.\)\@!/,$d]]) | ||||||
|  |  | ||||||
|       reset_cursor_pos(cursor_pos) |       reset_cursor_pos(cursor_pos) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user