Skip to content

Commit

Permalink
add boolean config options for common patterns (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobdub authored Oct 18, 2022
1 parent ab366eb commit 9091506
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ Plug 'cappyzawa/trim.nvim'
-- default config
local config = {
disable = {},
patterns = {
[[%s/\s\+$//e]], -- remove unwanted spaces
[[%s/\($\n\s*\)\+\%$//]], -- trim last line
[[%s/\%^\n\+//]], -- trim first line
[[%s/\(\n\n\)\n\+/\1/]], -- replace multiple blank lines with a single line
},
patterns = {},
trim_trailing = true,
trim_last_line = true,
trim_first_line = true,
}
```

Expand All @@ -37,11 +35,9 @@ lua <<EOF
-- you can specify filetypes.
disable = {"markdown"},
-- if you want to ignore space of top
-- if you want to remove multiple blank lines
patterns = {
[[%s/\s\+$//e]],
[[%s/\($\n\s*\)\+\%$//]],
[[%s/\(\n\n\)\n\+/\1/]],
[[%s/\(\n\n\)\n\+/\1/]], -- replace multiple blank lines with a single line
},
})
EOF
Expand Down
8 changes: 4 additions & 4 deletions lua/trim/config.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local config = {
disable = {},
patterns = {
[[%s/\s\+$//e]], [[%s/\($\n\s*\)\+\%$//]], [[%s/\%^\n\+//]],
[[%s/\(\n\n\)\n\+/\1/]]
}
patterns = {},
trim_trailing = true,
trim_last_line = true,
trim_first_line = true,
}

return config
13 changes: 13 additions & 0 deletions lua/trim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ M.setup = function(cfg)
cfg = cfg or {}
if not cfg.disable then cfg.disable = config.disable end
if not cfg.patterns then cfg.patterns = config.patterns end
if cfg.trim_trailing == nil then cfg.trim_trailing = config.trim_trailing end
if cfg.trim_first_line == nil then cfg.trim_first_line = config.trim_first_line end
if cfg.trim_last_line == nil then cfg.trim_last_line = config.trim_last_line end

if cfg.trim_first_line then
table.insert(cfg.patterns, [[%s/\%^\n\+//]])
end
if cfg.trim_last_line then
table.insert(cfg.patterns, [[%s/\($\n\s*\)\+\%$//]])
end
if cfg.trim_trailing then
table.insert(cfg.patterns, 1, [[%s/\s\+$//e]])
end

if not vim.api.nvim_create_autocmd then
vim.notify_once('trim.nvim requires nvim 0.7.0+.', vim.log.levels.ERROR)
Expand Down
2 changes: 1 addition & 1 deletion lua/trim/trimmer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local trimmer = {}

trimmer.trim = function(patterns)
local save = vim.fn.winsaveview()
for _, v in pairs(patterns) do
for _, v in ipairs(patterns) do
api.nvim_exec(string.format("keepjumps keeppatterns silent! %s", v), false)
end
vim.fn.winrestview(save)
Expand Down

0 comments on commit 9091506

Please sign in to comment.