Skip to content

Commit

Permalink
add user command TrimNow and config.trim_on_write (#16)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rtgiskard authored Mar 3, 2023
1 parent e89c10c commit 9202bc3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require("lazy").setup({
### Packer

```lua
use({
use({
"cappyzawa/trim.nvim",
config = function()
require("trim").setup({})
Expand All @@ -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,
Expand All @@ -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,
})
```

Expand All @@ -62,3 +66,7 @@ require('trim').setup({
### `:TrimToggle`

Toggle trim on save.

### `:Trim`

Trim the buffer right away.
21 changes: 17 additions & 4 deletions lua/trim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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,
})
Expand All @@ -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
6 changes: 6 additions & 0 deletions plugin/trim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

0 comments on commit 9202bc3

Please sign in to comment.