-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |