mirror of
https://github.com/neovim/neovim
synced 2025-07-16 17:21:49 +00:00
feat(defaults): dot-repeat [<Space> #31186
Problem: `[<Space>` and `]<Space>` do not support repetition. Solution: use `operatorfunc` and `g@l` to make these mappings dot repeatable.
This commit is contained in:
23
runtime/lua/vim/_buf.lua
Normal file
23
runtime/lua/vim/_buf.lua
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
local M = {}
|
||||||
|
|
||||||
|
--- Adds one or more blank lines above or below the cursor.
|
||||||
|
-- TODO: move to _defaults.lua once it is possible to assign a Lua function to options #25672
|
||||||
|
--- @param above? boolean Place blank line(s) above the cursor
|
||||||
|
local function add_blank(above)
|
||||||
|
local offset = above and 1 or 0
|
||||||
|
local repeated = vim.fn['repeat']({ '' }, vim.v.count1)
|
||||||
|
local linenr = vim.api.nvim_win_get_cursor(0)[1]
|
||||||
|
vim.api.nvim_buf_set_lines(0, linenr - offset, linenr - offset, true, repeated)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- TODO: move to _defaults.lua once it is possible to assign a Lua function to options #25672
|
||||||
|
function M.space_above()
|
||||||
|
add_blank(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- TODO: move to _defaults.lua once it is possible to assign a Lua function to options #25672
|
||||||
|
function M.space_below()
|
||||||
|
add_blank()
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
@ -366,16 +366,16 @@ do
|
|||||||
|
|
||||||
-- Add empty lines
|
-- Add empty lines
|
||||||
vim.keymap.set('n', '[<Space>', function()
|
vim.keymap.set('n', '[<Space>', function()
|
||||||
local repeated = vim.fn['repeat']({ '' }, vim.v.count1)
|
-- TODO: update once it is possible to assign a Lua function to options #25672
|
||||||
local linenr = vim.api.nvim_win_get_cursor(0)[1]
|
vim.go.operatorfunc = "v:lua.require'vim._buf'.space_above"
|
||||||
vim.api.nvim_buf_set_lines(0, linenr - 1, linenr - 1, true, repeated)
|
return 'g@l'
|
||||||
end, { desc = 'Add empty line above cursor' })
|
end, { expr = true, desc = 'Add empty line above cursor' })
|
||||||
|
|
||||||
vim.keymap.set('n', ']<Space>', function()
|
vim.keymap.set('n', ']<Space>', function()
|
||||||
local repeated = vim.fn['repeat']({ '' }, vim.v.count1)
|
-- TODO: update once it is possible to assign a Lua function to options #25672
|
||||||
local linenr = vim.api.nvim_win_get_cursor(0)[1]
|
vim.go.operatorfunc = "v:lua.require'vim._buf'.space_below"
|
||||||
vim.api.nvim_buf_set_lines(0, linenr, linenr, true, repeated)
|
return 'g@l'
|
||||||
end, { desc = 'Add empty line below cursor' })
|
end, { expr = true, desc = 'Add empty line below cursor' })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -170,6 +170,30 @@ describe('default', function()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
first line]])
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('supports dot repetition', function()
|
||||||
|
n.clear({ args_rm = { '--cmd' } })
|
||||||
|
n.insert([[first line]])
|
||||||
|
n.feed('[<Space>')
|
||||||
|
n.feed('.')
|
||||||
|
n.expect([[
|
||||||
|
|
||||||
|
|
||||||
|
first line]])
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('supports dot repetition and a count', function()
|
||||||
|
n.clear({ args_rm = { '--cmd' } })
|
||||||
|
n.insert([[first line]])
|
||||||
|
n.feed('[<Space>')
|
||||||
|
n.feed('3.')
|
||||||
|
n.expect([[
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
first line]])
|
first line]])
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
@ -194,6 +218,29 @@ describe('default', function()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
]])
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('supports dot repetition', function()
|
||||||
|
n.clear({ args_rm = { '--cmd' } })
|
||||||
|
n.insert([[first line]])
|
||||||
|
n.feed(']<Space>')
|
||||||
|
n.feed('.')
|
||||||
|
n.expect([[
|
||||||
|
first line
|
||||||
|
|
||||||
|
]])
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('supports dot repetition and a count', function()
|
||||||
|
n.clear({ args_rm = { '--cmd' } })
|
||||||
|
n.insert([[first line]])
|
||||||
|
n.feed(']<Space>')
|
||||||
|
n.feed('2.')
|
||||||
|
n.expect([[
|
||||||
|
first line
|
||||||
|
|
||||||
|
|
||||||
]])
|
]])
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
Reference in New Issue
Block a user