Skip to content

Commit

Permalink
using new bultin function
Browse files Browse the repository at this point in the history
  • Loading branch information
cappyzawa committed May 22, 2022
1 parent beafcd6 commit 0134550
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ This plugin trims trailing whitespace and lines.

Neovim v0.5.0+

***It will soon only work with Neovim v0.7.0+.***

## How to install

```vim
Expand Down
67 changes: 49 additions & 18 deletions lua/trim/init.lua
Original file line number Diff line number Diff line change
@@ -1,27 +1,58 @@
local vim = vim
local config = require 'trim.config'

local init = {}
local M = {}

init.setup = function(cfg)
if cfg.disable then config.disable = cfg.disable end
local has_value = function(tbl, val)
for _, v in ipairs(tbl) do
if v == val then
return true
end
end
return false
end

if cfg.patterns then config.patterns = cfg.patterns end
local init_with_070_plus = function(cfg)
vim.api.nvim_create_augroup('TrimNvim', { clear = true })
vim.api.nvim_create_autocmd('BufWritePre', {
pattern = '*',
callback = function()
if not has_value(cfg.disable, vim.bo.filetype) then
require 'trim.trimmer'.trim()
end
end,
group = 'TrimNvim'
})
end

vim.cmd [[augroup TrimNvim]]
vim.cmd [[ autocmd!]]
if #config.disable == 0 then
vim.cmd [[ autocmd BufWritePre * lua require'trim.trimmer'.trim() ]]
else
local disables = {}
for _, v in pairs(config.disable) do
table.insert(disables, string.format("&filetype != '%s'", v))
end
vim.cmd(string.format(
" autocmd BufWritePre * if %s | lua require'trim.trimmer'.trim()",
table.concat(disables, " && ")))
local init_without_070_plus = function(cfg)
vim.notify_once('trim.nvim will soon only work with nvim 0.7.0+', vim.log.levels.WARN)
vim.cmd [[augroup TrimNvim]]
vim.cmd [[ autocmd!]]
if #cfg.disable == 0 then
vim.cmd [[ autocmd BufWritePre * lua require'trim.trimmer'.trim() ]]
else
local disables = {}
for _, v in pairs(cfg.disable) do
table.insert(disables, string.format("&filetype != '%s'", v))
end
vim.cmd [[augroup END]]
vim.cmd(string.format(
" autocmd BufWritePre * if %s | lua require'trim.trimmer'.trim()",
table.concat(disables, " && ")))
end
vim.cmd [[augroup END]]
end

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 not vim.api.nvim_create_autocmd then
init_without_070_plus(cfg)
else
init_with_070_plus(cfg)
end
end

return init
return M

0 comments on commit 0134550

Please sign in to comment.