Regenerate nvim config
This commit is contained in:
@ -0,0 +1,67 @@
|
||||
local M = {
|
||||
config = {},
|
||||
}
|
||||
|
||||
local default_config = {
|
||||
ft_blocklist = {},
|
||||
patterns = {},
|
||||
trim_on_write = true,
|
||||
trim_trailing = true,
|
||||
trim_last_line = true,
|
||||
trim_first_line = true,
|
||||
highlight = false,
|
||||
highlight_bg = '#ff0000',
|
||||
highlight_ctermbg = 'red',
|
||||
}
|
||||
|
||||
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_trailing then
|
||||
table.insert(M.config.patterns, [[%s/\s\+$//e]])
|
||||
end
|
||||
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.highlight then
|
||||
local augroup = vim.api.nvim_create_augroup('TrimHighlight', { clear = true })
|
||||
|
||||
-- Define the autocommand for FileType events
|
||||
vim.api.nvim_create_autocmd('FileType', {
|
||||
group = augroup,
|
||||
pattern = '*', -- This will trigger the autocmd for all file types
|
||||
callback = function()
|
||||
-- Check if the current file type is not in the blocklist to avoid highlighting
|
||||
if not vim.tbl_contains(M.config.ft_blocklist, vim.bo.filetype) then
|
||||
-- Apply highlighting for trailing whitespace
|
||||
vim.api.nvim_set_hl(0, 'ExtraWhitespace', {
|
||||
bg = M.config.highlight_bg,
|
||||
ctermbg = M.config.highlight_ctermbg
|
||||
})
|
||||
vim.api.nvim_exec('match ExtraWhitespace /\\s\\+$/', false)
|
||||
else
|
||||
-- Clear the highlighting for this buffer if the file type is in the blocklist
|
||||
vim.api.nvim_exec('match none', false)
|
||||
end
|
||||
end,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
function M.get()
|
||||
return M.config
|
||||
end
|
||||
|
||||
return M
|
||||
14
config/neovim/store/lazy-plugins/trim.nvim/lua/trim/init.lua
Normal file
14
config/neovim/store/lazy-plugins/trim.nvim/lua/trim/init.lua
Normal file
@ -0,0 +1,14 @@
|
||||
local M = {}
|
||||
|
||||
local config = require 'trim.config'
|
||||
local trimmer = require 'trim.trimmer'
|
||||
|
||||
function M.setup(opt)
|
||||
config.setup(opt)
|
||||
local cfg = config.get()
|
||||
if cfg.trim_on_write then
|
||||
trimmer.enable(true)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
@ -0,0 +1,59 @@
|
||||
local vim = vim
|
||||
local api = vim.api
|
||||
|
||||
local trimmer = {}
|
||||
|
||||
function trimmer.trim()
|
||||
local config = require('trim.config').get()
|
||||
local save = vim.fn.winsaveview()
|
||||
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
|
||||
Reference in New Issue
Block a user