From 9202bc31f0f7d2503879c8b65d8b99ab0fae28f6 Mon Sep 17 00:00:00 2001 From: giskard Date: Fri, 3 Mar 2023 16:49:35 +0800 Subject: [PATCH] add user command TrimNow and config.trim_on_write (#16) * add user command TrimNow and config.trim_on_write - add function based on previous update: . TrimNow: user command to call directly . trim_on_write: config option to control the initial state - compatability change: . config.disable rename to config.blacklist * user command rename: TrimNow -> Trim * move fstype check from trim() to BufWritePre cb - manually Trim should work on all files * fix config compatability: map disable to blacklist - use `disable` if present when `blacklist` not set * config rename: blacklist -> ft_blocklist --- README.md | 14 +++++++++++--- lua/trim/init.lua | 21 +++++++++++++++++---- plugin/trim.lua | 6 ++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5d70379..a48ceaa 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ require("lazy").setup({ ### Packer ```lua -use({ +use({ "cappyzawa/trim.nvim", config = function() require("trim").setup({}) @@ -36,8 +36,9 @@ use({ ```lua -- default config local default_config = { - disable = {}, + ft_blocklist = {}, patterns = {}, + trim_on_write = true, trim_trailing = true, trim_last_line = true, trim_first_line = true, @@ -48,12 +49,15 @@ local default_config = { require('trim').setup({ -- if you want to ignore markdown file. -- you can specify filetypes. - disable = {"markdown"}, + ft_blocklist = {"markdown"}, -- if you want to remove multiple blank lines patterns = { [[%s/\(\n\n\)\n\+/\1/]], -- replace multiple blank lines with a single line }, + + -- if you want to disable trim on write by default + trim_on_write = false, }) ``` @@ -62,3 +66,7 @@ require('trim').setup({ ### `:TrimToggle` Toggle trim on save. + +### `:Trim` + +Trim the buffer right away. diff --git a/lua/trim/init.lua b/lua/trim/init.lua index f6f2a66..9d6efaf 100644 --- a/lua/trim/init.lua +++ b/lua/trim/init.lua @@ -2,8 +2,9 @@ local vim = vim local trimmer = require 'trim.trimmer' local default_config = { - disable = {}, + ft_blocklist = {}, patterns = {}, + trim_on_write = true, trim_trailing = true, trim_last_line = true, trim_first_line = true, @@ -24,6 +25,12 @@ end function M.setup(opts) opts = opts or {} + + -- compatability: disable -> ft_blocklist + if (opts.disable and not opts.ft_blocklist) then + opts.ft_blocklist = opts.disable + end + M.config = vim.tbl_deep_extend('force', default_config, opts) if M.config.trim_first_line then @@ -36,7 +43,9 @@ function M.setup(opts) table.insert(M.config.patterns, 1, [[%s/\s\+$//e]]) end - M.enable(true) + if M.config.trim_on_write then + M.enable(true) + end end function M.toggle() @@ -58,8 +67,8 @@ function M.enable(is_configured) group = 'TrimNvim', pattern = opts.pattern, callback = function() - if not has_value(M.config.disable, vim.bo.filetype) then - trimmer.trim(M.config.patterns) + if not has_value(M.config.ft_blocklist, vim.bo.filetype) then + M.trim() end end, }) @@ -73,4 +82,8 @@ function M.disable() vim.notify('TrimNvim disabled', vim.log.levels.INFO, { title = 'trim.nvim' }) end +function M.trim() + trimmer.trim(M.config.patterns) +end + return M diff --git a/plugin/trim.lua b/plugin/trim.lua index cca142a..3adc370 100644 --- a/plugin/trim.lua +++ b/plugin/trim.lua @@ -13,3 +13,9 @@ vim.api.nvim_create_user_command('TrimToggle', function(args) end, { range = false, }) + +vim.api.nvim_create_user_command('Trim', function(args) + require('trim').trim() +end, { + range = false, +})