paste: support replace mode (#11945)

* paste: support replace mode
* Clean up

Co-authored-by: Jesse Bakker <git@jessebakker.com>
This commit is contained in:
Jesse 2020-05-05 13:18:41 +02:00 committed by GitHub
parent a467f3f665
commit 48c2198297
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -212,6 +212,18 @@ do
vim.api.nvim_put(lines, 'c', true, true)
-- XXX: Normal-mode: workaround bad cursor-placement after first chunk.
vim.api.nvim_command('normal! a')
elseif phase < 2 and mode == 'R' then
local nchars = 0
for _, line in ipairs(lines) do
nchars = nchars + line:len()
end
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
local bufline = vim.api.nvim_buf_get_lines(0, row-1, row, true)[1]
local firstline = lines[1]
firstline = bufline:sub(1, col)..firstline
lines[1] = firstline
lines[#lines] = lines[#lines]..bufline:sub(col + nchars + 1, bufline:len())
vim.api.nvim_buf_set_lines(0, row-1, row, false, lines)
else
vim.api.nvim_put(lines, 'c', false, true)
end

View File

@ -571,6 +571,28 @@ describe('API', function()
eq({0,7,1,0}, funcs.getpos('.'))
eq(false, nvim('get_option', 'paste'))
end)
it('Replace-mode', function()
-- Within single line
nvim('put', {'aabbccdd', 'eeffgghh', 'iijjkkll'}, "c", true, false)
command('normal l')
command('startreplace')
nvim('paste', '123456', true, -1)
expect([[
a123456d
eeffgghh
iijjkkll]])
command('%delete _')
-- Across lines
nvim('put', {'aabbccdd', 'eeffgghh', 'iijjkkll'}, "c", true, false)
command('normal l')
command('startreplace')
nvim('paste', '123\n456', true, -1)
expect([[
a123
456d
eeffgghh
iijjkkll]])
end)
it('crlf=false does not break lines at CR, CRLF', function()
nvim('paste', 'line 1\r\n\r\rline 2\nline 3\rline 4\r', false, -1)
expect('line 1\r\n\r\rline 2\nline 3\rline 4\r')