mirror of
https://github.com/tiyn/tidy.nvim.git
synced 2025-06-15 03:27:47 +02:00
fix:
- reinit v1
This commit is contained in:
parent
1720fb50d0
commit
25715ac21f
87
README.md
87
README.md
@ -1,96 +1,35 @@
|
|||||||
|
|
||||||
# tidy.nvim 🧹
|
# tidy.nvim 🧹
|
||||||
|
|
||||||
A function and autocommand pair that can
|
A function and autocommand pair that removes all
|
||||||
|
|
||||||
- remove all empty lines at the top of the buffer
|
- trailing whitespace
|
||||||
- remove all trailing whitespace
|
- empty lines at the end of the buffer
|
||||||
- remove a variable amount of empty lines at the end of the buffer
|
|
||||||
- condense multiple empty lines into one
|
|
||||||
|
|
||||||
on every `BufWritePre`.
|
on every `BufWritePre`.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
|
||||||
## install
|
## install
|
||||||
- Packer with default configuration and lazy-loading
|
- Packer
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
use{
|
use "McAuleyPenney/tidy.nvim"
|
||||||
"mcauley-penney/tidy.nvim",
|
|
||||||
config = function()
|
|
||||||
require "tidy".setup{}
|
|
||||||
end,
|
|
||||||
event = "BufWritePre"
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Configuration
|
or with lazy-loading
|
||||||
|
|
||||||
### default
|
|
||||||
|
|
||||||
Tidy comes with the below default configuration:
|
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
local M = {
|
use{ "McAuleyPenney/tidy.nvim", event = "BufWritePre" }
|
||||||
config = {
|
|
||||||
eof_quant = -1, -- the amount of empty lines to leave at the end of
|
|
||||||
-- the file; -1 = no lines, 0 = 1 line, no limit;
|
|
||||||
-- only applies if "eof" given to "fmts"
|
|
||||||
|
|
||||||
fmts = { -- the types of formattings to apply
|
|
||||||
"eof", -- removes lines at end of file
|
|
||||||
"multi", -- condenses multiple newlines into one
|
|
||||||
"sof", -- removes lines at start of file
|
|
||||||
"ws" -- removes trailing whitespace
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### how to customize
|
|
||||||
|
|
||||||
To customize which formattings will apply, give a list to the `setup` function:
|
|
||||||
|
|
||||||
```lua
|
|
||||||
use{
|
|
||||||
"mcauley-penney/tidy.nvim",
|
|
||||||
config = function()
|
|
||||||
require "tidy".setup{
|
|
||||||
fmts = {
|
|
||||||
"eof",
|
|
||||||
"ws"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end,
|
|
||||||
event = "BufWritePre"
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### formatting styles
|
|
||||||
|
|
||||||
- `eof`: remove a variable amount of newlines at end of file
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
- `multi`: condenses multiple newlines into one
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
- `sof`: removes lines at start of file
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
- `ws`: remove whitespace
|
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Credits:
|
## Credits:
|
||||||
- [Vim Tips Wiki entry](https://vim.fandom.com/wiki/Remove_unwanted_spaces#Automatically_removing_all_trailing_whitespace)
|
- [Vim Tips Wiki entry for removing unwanted spaces](https://vim.fandom.com/wiki/Remove_unwanted_spaces#Automatically_removing_all_trailing_whitespace)
|
||||||
for removing unwanted spaces
|
|
||||||
- ib., the author of [this stack overflow answer](https://stackoverflow.com/a/7501902), for how to remove empty lines at the
|
- ib., the author of [this stack overflow answer](https://stackoverflow.com/a/7501902)
|
||||||
end of the buffer
|
|
||||||
- @blackboardd for how to choose how many lines will be kept
|
|
||||||
- [This line](https://github.com/gpanders/editorconfig.nvim/blob/ae3586771996b2fb1662eb0c17f5d1f4f5759bb7/lua/editorconfig.lua#L180)
|
- [This line](https://github.com/gpanders/editorconfig.nvim/blob/ae3586771996b2fb1662eb0c17f5d1f4f5759bb7/lua/editorconfig.lua#L180)
|
||||||
in [gpanders/editorconfig.nvim](https://github.com/gpanders/editorconfig.nvim) for exposing me to the `keepjumps`
|
in [gpanders/editorconfig.nvim](https://github.com/gpanders/editorconfig.nvim) for exposing me to the `keepjumps`
|
||||||
and `keeppatterns` modifiers
|
and `keeppatterns` modifiers
|
||||||
- [Vim Tips Wiki entry](https://vim.fandom.com/wiki/Remove_unwanted_empty_lines) for condensing multiple empty lines
|
|
||||||
|
@ -1,54 +1,20 @@
|
|||||||
local M = {
|
-- MP
|
||||||
config = {
|
|
||||||
eof_quant = -1,
|
|
||||||
fmts = {
|
|
||||||
"sof",
|
|
||||||
"eof",
|
|
||||||
"multi",
|
|
||||||
"ws"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function M.setup(user_config)
|
local M = {}
|
||||||
-- integrate user configuration
|
|
||||||
M.config = vim.tbl_deep_extend("force", M.config, user_config or {})
|
|
||||||
|
|
||||||
vim.cmd[[
|
|
||||||
augroup Tidy
|
|
||||||
au!
|
|
||||||
au BufWritePre * lua require( "tidy" ).tidy_up()
|
|
||||||
augroup END
|
|
||||||
]]
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.tidy_up()
|
function M.tidy_up()
|
||||||
local cmd_mods = ":keepjumps keeppatterns silent! "
|
|
||||||
|
|
||||||
local patterns = {
|
|
||||||
-- delete all lines at end of buffer, see sources
|
|
||||||
eof = [[0;/^\%(\n*.\)\@!/ + ]] .. M.config.eof_quant .. ",$d",
|
|
||||||
|
|
||||||
-- compress all instances of multiple newlines into one, see sources
|
|
||||||
multi = [[:%s/\n\{2,}/\r\r/e]],
|
|
||||||
|
|
||||||
-- delete all new lines at beginning of file
|
|
||||||
sof = [[%s/\%^\n*/]],
|
|
||||||
|
|
||||||
-- delete all whitespace, see sources
|
|
||||||
ws = [[%s/\s\+$//e]]
|
|
||||||
}
|
|
||||||
|
|
||||||
-- get tuple of cursor position before making changes
|
-- get tuple of cursor position before making changes
|
||||||
local pos = vim.api.nvim_win_get_cursor(0)
|
local pos = vim.api.nvim_win_get_cursor( 0 )
|
||||||
|
|
||||||
-- execute chosen patterns
|
-- delete all whitespace, see source 1
|
||||||
for _, fmt_type in ipairs(M.config.fmts) do
|
vim.cmd[[:keepjumps keeppatterns %s/\s\+$//e]]
|
||||||
vim.cmd(cmd_mods .. patterns[fmt_type])
|
|
||||||
end
|
-- delete all lines at end of buffer, see source 2
|
||||||
|
vim.cmd[[:keepjumps keeppatterns silent! 0;/^\%(\n*.\)\@!/,$d]]
|
||||||
|
|
||||||
-- get row count after line deletion
|
-- get row count after line deletion
|
||||||
local end_row = vim.api.nvim_buf_line_count(0)
|
local end_row = vim.api.nvim_buf_line_count( 0 )
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
if the row value in the original cursor
|
if the row value in the original cursor
|
||||||
@ -63,7 +29,7 @@ function M.tidy_up()
|
|||||||
pos[1] = end_row
|
pos[1] = end_row
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_win_set_cursor(0, pos)
|
vim.api.nvim_win_set_cursor( 0, pos )
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
4
plugin/tidy.vim
Normal file
4
plugin/tidy.vim
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
augroup Tidy
|
||||||
|
au!
|
||||||
|
au BufWritePre * lua require( "tidy" ).tidy_up()
|
||||||
|
augroup END
|
Loading…
x
Reference in New Issue
Block a user