Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
cappyzawa committed Mar 3, 2023
1 parent b80a184 commit 410e188
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 88 deletions.
40 changes: 40 additions & 0 deletions lua/trim/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
local M = {
config = {},
}

local default_config = {
ft_blocklist = {},
patterns = {},
trim_on_write = true,
trim_trailing = true,
trim_last_line = true,
trim_first_line = true,
}

function M.setup(opts)
opts = opts or {}

-- compatability: disable -> ft_blocklist
if opts.disable and not opts.ft_blocklist then
vim.notify('`disable` is deprecated, use `ft_blocklist` instead', vim.log.levels.WARN, { title = 'trim.nvim' })
opts.ft_blocklist = opts.disable
end

M.config = vim.tbl_deep_extend('force', default_config, opts)

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

function M.get()
return M.config
end

return M
92 changes: 8 additions & 84 deletions lua/trim/init.lua
Original file line number Diff line number Diff line change
@@ -1,90 +1,14 @@
local vim = vim
local trimmer = require 'trim.trimmer'

local default_config = {
ft_blocklist = {},
patterns = {},
trim_on_write = true,
trim_trailing = true,
trim_last_line = true,
trim_first_line = true,
}

local M = {
config = {},
}

local has_value = function(tbl, val)
for _, v in ipairs(tbl) do
if v == val then
return true
end
end
return false
end

function M.setup(opts)
opts = opts or {}

-- compatability: disable -> ft_blocklist
if opts.disable and not opts.ft_blocklist then
vim.notify('`disable` is deprecated, use `ft_blocklist` instead', vim.log.levels.WARN, { title = 'trim.nvim' })
opts.ft_blocklist = opts.disable
end

M.config = vim.tbl_deep_extend('force', default_config, opts)

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

if M.config.trim_on_write then
M.enable(true)
end
end
local M = {}

function M.toggle()
local status = pcall(vim.api.nvim_get_autocmds, {
group = 'TrimNvim',
event = 'BufWritePre',
})
if not status then
M.enable(false)
else
M.disable()
end
end
local config = require 'trim.config'
local trimmer = require 'trim.trimmer'

function M.enable(is_configured)
local opts = { pattern = '*' }
vim.api.nvim_create_augroup('TrimNvim', { clear = true })
vim.api.nvim_create_autocmd('BufWritePre', {
group = 'TrimNvim',
pattern = opts.pattern,
callback = function()
if not has_value(M.config.ft_blocklist, vim.bo.filetype) then
M.trim()
end
end,
})
if not is_configured then
vim.notify('TrimNvim enabled', vim.log.levels.INFO, { title = 'trim.nvim' })
function M.setup(opt)
config.setup(opt)
local cfg = config.get()
if cfg.trim_on_write then
trimmer.enable(true)
end
end

function M.disable()
pcall(vim.api.nvim_del_augroup_by_name, 'TrimNvim')
vim.notify('TrimNvim disabled', vim.log.levels.INFO, { title = 'trim.nvim' })
end

function M.trim()
trimmer.trim(M.config.patterns)
end

return M
49 changes: 47 additions & 2 deletions lua/trim/trimmer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,57 @@ local api = vim.api

local trimmer = {}

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

local has_value = function(tbl, val)
for _, v in ipairs(tbl) do
if v == val then
return true
end
end
return false
end

function trimmer.enable(is_configured)
local opts = { pattern = '*' }
local config = require('trim.config').get()
vim.api.nvim_create_augroup('TrimNvim', { clear = true })
vim.api.nvim_create_autocmd('BufWritePre', {
group = 'TrimNvim',
pattern = opts.pattern,
callback = function()
if not has_value(config.ft_blocklist, vim.bo.filetype) then
trimmer.trim()
end
end,
})
if not is_configured then
vim.notify('TrimNvim enabled', vim.log.levels.INFO, { title = 'trim.nvim' })
end
end

function trimmer.disable()
pcall(vim.api.nvim_del_augroup_by_name, 'TrimNvim')
vim.notify('TrimNvim disabled', vim.log.levels.INFO, { title = 'trim.nvim' })
end

function trimmer.toggle()
local status = pcall(vim.api.nvim_get_autocmds, {
group = 'TrimNvim',
event = 'BufWritePre',
})
if not status then
trimmer.enable(false)
else
trimmer.disable()
end
end

return trimmer
6 changes: 4 additions & 2 deletions plugin/trim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ if vim.g.load_trim_nvim then
end
vim.g.load_trim_nvim = true

local trimmer = require 'trim.trimmer'

if not vim.api.nvim_create_autocmd then
vim.notify_once('trim.nvim requires nvim 0.7.0+.', vim.log.levels.ERROR, { title = 'trim.nvim' })
return
end

vim.api.nvim_create_user_command('TrimToggle', function(args)
require('trim').toggle()
trimmer.toggle()
end, {
range = false,
})

vim.api.nvim_create_user_command('Trim', function(args)
require('trim').trim()
trimmer.trim()
end, {
range = false,
})

0 comments on commit 410e188

Please sign in to comment.