refactor(nvim): use trim usercmd function from nvim docs
This commit is contained in:
parent
cee6db9c98
commit
5d82bac8aa
@ -10,28 +10,28 @@ M.setup = function()
|
||||
end,
|
||||
})
|
||||
|
||||
local strip_trail_space = true
|
||||
vim.api.nvim_create_user_command("ToggleStripTrailSpace", function()
|
||||
strip_trail_space = not strip_trail_space
|
||||
local trim_trailing_whitespace_on_save = true
|
||||
vim.api.nvim_create_user_command("ToggleTrimTrailingWhitespace", function()
|
||||
trim_trailing_whitespace_on_save = not trim_trailing_whitespace_on_save
|
||||
local intercept_state = "`Enabled`"
|
||||
if not strip_trail_space then
|
||||
if not trim_trailing_whitespace_on_save then
|
||||
intercept_state = "`Disabled`"
|
||||
end
|
||||
vim.notify("Strip Trail Space set to " .. intercept_state, vim.log.levels.INFO, {
|
||||
title = "Strip Trail Space",
|
||||
vim.notify("Trim Trailing Whitespace Space set to " .. intercept_state, vim.log.levels.INFO, {
|
||||
title = "Trim Trailing Whitespace",
|
||||
---@param win integer The window handle
|
||||
on_open = function(win)
|
||||
vim.api.nvim_set_option_value("filetype", "markdown", { buf = vim.api.nvim_win_get_buf(win) })
|
||||
end,
|
||||
})
|
||||
end, { desc = "Toggles intercepting BufWritePre to strip trail space" })
|
||||
end, { desc = "Toggles intercepting BufWritePre to Trim Trailing Whitespace" })
|
||||
|
||||
-- NOTE: Remove trailing whitespace on save
|
||||
vim.api.nvim_create_autocmd("BufWritePre", {
|
||||
group = augroup,
|
||||
callback = function()
|
||||
if strip_trail_space then
|
||||
vim.cmd.StripTrailSpace()
|
||||
if trim_trailing_whitespace_on_save then
|
||||
vim.cmd.TrimTrailingWhitespace()
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
@ -1,6 +1,76 @@
|
||||
local M = {}
|
||||
M.setup = function()
|
||||
vim.api.nvim_create_user_command("StripTrailSpace", "%s/\\s\\+$//e", {})
|
||||
-- NOTE: TrimTrailingWhitespace user command
|
||||
|
||||
-- If invoked as a preview callback, performs 'inccommand' preview by
|
||||
-- highlighting trailing whitespace in the current buffer.
|
||||
local function trim_space_preview(opts, preview_ns, preview_buf)
|
||||
vim.cmd("highlight clear Whitespace")
|
||||
local line1 = opts.line1
|
||||
local line2 = opts.line2
|
||||
local buf = vim.api.nvim_get_current_buf()
|
||||
local lines = vim.api.nvim_buf_get_lines(buf, line1 - 1, line2, false)
|
||||
local preview_buf_line = 0
|
||||
|
||||
for i, line in ipairs(lines) do
|
||||
local start_idx, end_idx = string.find(line, "%s+$")
|
||||
|
||||
if start_idx and end_idx then
|
||||
-- Highlight the match
|
||||
vim.api.nvim_buf_add_highlight(buf, preview_ns, "Substitute", line1 + i - 2, start_idx - 1, end_idx)
|
||||
|
||||
-- Add lines and set highlights in the preview buffer
|
||||
-- if inccommand=split
|
||||
if preview_buf then
|
||||
local prefix = string.format("|%d| ", line1 + i - 1)
|
||||
|
||||
vim.api.nvim_buf_set_lines(
|
||||
preview_buf,
|
||||
preview_buf_line,
|
||||
preview_buf_line,
|
||||
false,
|
||||
{ prefix .. line }
|
||||
)
|
||||
vim.api.nvim_buf_add_highlight(
|
||||
preview_buf,
|
||||
preview_ns,
|
||||
"Substitute",
|
||||
preview_buf_line,
|
||||
#prefix + start_idx - 1,
|
||||
#prefix + end_idx
|
||||
)
|
||||
preview_buf_line = preview_buf_line + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Return the value of the preview type
|
||||
return 2
|
||||
end
|
||||
|
||||
-- Trims all trailing whitespace in the current buffer.
|
||||
local function trim_space(opts)
|
||||
local line1 = opts.line1
|
||||
local line2 = opts.line2
|
||||
local buf = vim.api.nvim_get_current_buf()
|
||||
local lines = vim.api.nvim_buf_get_lines(buf, line1 - 1, line2, false)
|
||||
|
||||
local new_lines = {}
|
||||
for i, line in ipairs(lines) do
|
||||
new_lines[i] = string.gsub(line, "%s+$", "")
|
||||
end
|
||||
vim.api.nvim_buf_set_lines(buf, line1 - 1, line2, false, new_lines)
|
||||
end
|
||||
|
||||
-- Create the user command
|
||||
vim.api.nvim_create_user_command(
|
||||
"TrimTrailingWhitespace",
|
||||
trim_space,
|
||||
{ nargs = "?", range = "%", addr = "lines", preview = trim_space_preview }
|
||||
)
|
||||
|
||||
-- NOTE: DiffSaved user command
|
||||
|
||||
vim.api.nvim_create_user_command("DiffSaved", function()
|
||||
-- Thanks to sindrets: https://github.com/sindrets/dotfiles/blob/1990282dba25aaf49897f0fc70ebb50f424fc9c4/.config/nvim/lua/user/lib.lua#L175
|
||||
-- Minor alterations by me
|
||||
|
Loading…
Reference in New Issue
Block a user