Merge pull request #12935 from vigoux/byte-change-lines

buf_updates: fix wrong updates on linewise change
This commit is contained in:
Björn Linse 2020-09-19 11:02:32 +02:00 committed by GitHub
commit cea2417f30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 28 deletions

View File

@ -68,6 +68,10 @@
# define LOG_CALLSTACK_TO_FILE(fp) log_callstack_to_file(fp, __func__, __LINE__) # define LOG_CALLSTACK_TO_FILE(fp) log_callstack_to_file(fp, __func__, __LINE__)
#endif #endif
#if defined(__has_include) && __has_include("sanitizer/asan_interface.h")
# include "sanitizer/asan_interface.h"
#endif
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
# include "log.h.generated.h" # include "log.h.generated.h"
#endif #endif

View File

@ -1544,10 +1544,10 @@ int op_delete(oparg_T *oap)
oap->line_count = 0; // no lines deleted oap->line_count = 0; // no lines deleted
} else if (oap->motion_type == kMTLineWise) { } else if (oap->motion_type == kMTLineWise) {
if (oap->op_type == OP_CHANGE) { if (oap->op_type == OP_CHANGE) {
/* Delete the lines except the first one. Temporarily move the // Delete the lines except the first one. Temporarily move the
* cursor to the next line. Save the current line number, if the // cursor to the next line. Save the current line number, if the
* last line is deleted it may be changed. // last line is deleted it may be changed.
*/
if (oap->line_count > 1) { if (oap->line_count > 1) {
lnum = curwin->w_cursor.lnum; lnum = curwin->w_cursor.lnum;
++curwin->w_cursor.lnum; ++curwin->w_cursor.lnum;
@ -1560,12 +1560,21 @@ int op_delete(oparg_T *oap)
beginline(BL_WHITE); // cursor on first non-white beginline(BL_WHITE); // cursor on first non-white
did_ai = true; // delete the indent when ESC hit did_ai = true; // delete the indent when ESC hit
ai_col = curwin->w_cursor.col; ai_col = curwin->w_cursor.col;
} else } else {
beginline(0); /* cursor in column 0 */ beginline(0); // cursor in column 0
truncate_line(FALSE); /* delete the rest of the line */ }
/* leave cursor past last char in line */
if (oap->line_count > 1) int old_len = (int)STRLEN(ml_get(curwin->w_cursor.lnum));
u_clearline(); /* "U" command not possible after "2cc" */ truncate_line(false); // delete the rest of the line
extmark_splice_cols(curbuf,
(int)curwin->w_cursor.lnum-1, curwin->w_cursor.col,
old_len - curwin->w_cursor.col, 0, kExtmarkUndo);
// leave cursor past last char in line
if (oap->line_count > 1) {
u_clearline(); // "U" command not possible after "2cc"
}
} else { } else {
del_lines(oap->line_count, TRUE); del_lines(oap->line_count, TRUE);
beginline(BL_WHITE | BL_FIX); beginline(BL_WHITE | BL_FIX);

View File

@ -287,26 +287,36 @@ describe('lua: nvim_buf_attach on_bytes', function()
fail(msg) fail(msg)
end end
if verify then if not verify then
for _, event in ipairs(events) do return
if event[1] == verify_name and event[2] == "bytes" then end
local _, _, _, _, _, _, start_byte, _, _, old_byte, _, _, new_byte = unpack(event)
local before = string.sub(shadowbytes, 1, start_byte) for _, event in ipairs(events) do
-- no text in the tests will contain 0xff bytes (invalid UTF-8) for _, elem in ipairs(event) do
-- so we can use it as marker for unknown bytes if type(elem) == "number" and elem < 0 then
local unknown = string.rep('\255', new_byte) fail(string.format("Received event has negative values"))
local after = string.sub(shadowbytes, start_byte + old_byte + 1)
shadowbytes = before .. unknown .. after
end end
end end
local text = meths.buf_get_lines(0, 0, -1, true)
local bytes = table.concat(text, '\n') .. '\n' if event[1] == verify_name and event[2] == "bytes" then
eq(string.len(bytes), string.len(shadowbytes), '\non_bytes: total bytecount of buffer is wrong') local _, _, _, _, _, _, start_byte, _, _, old_byte, _, _, new_byte = unpack(event)
for i = 1, string.len(shadowbytes) do local before = string.sub(shadowbytes, 1, start_byte)
local shadowbyte = string.sub(shadowbytes, i, i) -- no text in the tests will contain 0xff bytes (invalid UTF-8)
if shadowbyte ~= '\255' then -- so we can use it as marker for unknown bytes
eq(string.sub(bytes, i, i), shadowbyte, i) local unknown = string.rep('\255', new_byte)
end local after = string.sub(shadowbytes, start_byte + old_byte + 1)
shadowbytes = before .. unknown .. after
end
end
local text = meths.buf_get_lines(0, 0, -1, true)
local bytes = table.concat(text, '\n') .. '\n'
eq(string.len(bytes), string.len(shadowbytes), '\non_bytes: total bytecount of buffer is wrong')
for i = 1, string.len(shadowbytes) do
local shadowbyte = string.sub(shadowbytes, i, i)
if shadowbyte ~= '\255' then
eq(string.sub(bytes, i, i), shadowbyte, i)
end end
end end
end end
@ -411,6 +421,23 @@ describe('lua: nvim_buf_attach on_bytes', function()
{ "test1", "bytes", 1, 3, 0, 0, 0, 0, 0, 0, 0, 1, 1 }; { "test1", "bytes", 1, 3, 0, 0, 0, 0, 0, 0, 0, 1, 1 };
} }
end) end)
it("changing lines", function()
local check_events = setup_eventcheck(verify, origlines)
feed "cc"
check_events {
{ "test1", "bytes", 1, 4, 0, 0, 0, 0, 15, 15, 0, 0, 0 };
}
feed "<ESC>"
check_events {}
feed "c3j"
check_events {
{ "test1", "bytes", 1, 4, 1, 0, 1, 3, 0, 48, 0, 0, 0 };
}
end)
end end
describe('(with verify) handles', function() describe('(with verify) handles', function()