aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchentau <tchen1998@gmail.com>2021-05-06 20:37:09 -0700
committerchentau <tchen1998@gmail.com>2021-05-09 16:23:01 -0700
commit13a9bd006f08819184963b61909f0595321adc51 (patch)
tree9618b3f0ffad35b4c97540707461a9137d0ebd35
parenta847abc21b339a8402d39cc2736f3f73e4f2f124 (diff)
downloadrneovim-13a9bd006f08819184963b61909f0595321adc51.tar.gz
rneovim-13a9bd006f08819184963b61909f0595321adc51.tar.bz2
rneovim-13a9bd006f08819184963b61909f0595321adc51.zip
make get_region_bytecount end-exclusive
-rw-r--r--src/nvim/api/buffer.c2
-rw-r--r--src/nvim/ops.c33
-rw-r--r--test/functional/lua/buffer_updates_spec.lua6
3 files changed, 27 insertions, 14 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c
index a396693a61..665c622c98 100644
--- a/src/nvim/api/buffer.c
+++ b/src/nvim/api/buffer.c
@@ -442,7 +442,7 @@ void nvim_buf_set_lines(uint64_t channel_id,
goto end;
}
- bcount_t deleted_bytes = MAX(get_region_bytecount(start, end, 0, 0)-1, 0);
+ bcount_t deleted_bytes = get_region_bytecount(curbuf, start, end, 0, 0);
// If the size of the range is reducing (ie, new_len < old_len) we
// need to delete some old_len. We do this at the start, by
diff --git a/src/nvim/ops.c b/src/nvim/ops.c
index 569ed167d2..ca68ef04b0 100644
--- a/src/nvim/ops.c
+++ b/src/nvim/ops.c
@@ -1677,8 +1677,8 @@ int op_delete(oparg_T *oap)
curbuf_splice_pending++;
pos_T startpos = curwin->w_cursor; // start position for delete
bcount_t deleted_bytes = get_region_bytecount(
- startpos.lnum, oap->end.lnum, startpos.col,
- oap->end.col) - !oap->inclusive;
+ curbuf, startpos.lnum, oap->end.lnum, startpos.col,
+ oap->end.col) + oap->inclusive;
truncate_line(true); // delete from cursor to end of line
curpos = curwin->w_cursor; // remember curwin->w_cursor
@@ -6300,19 +6300,32 @@ bool op_reg_set_previous(const char name)
return true;
}
-bcount_t get_region_bytecount(linenr_T start_lnum, linenr_T end_lnum,
- colnr_T start_col, colnr_T end_col)
+/// Get the byte count of buffer region. End-exclusive.
+///
+/// @return number of bytes
+bcount_t get_region_bytecount(buf_T *buf, linenr_T start_lnum,
+ linenr_T end_lnum, colnr_T start_col,
+ colnr_T end_col)
{
- const char *first = (const char *)ml_get(start_lnum);
- bcount_t deleted_bytes = (bcount_t)STRLEN(first) - start_col + 1;
-
+ linenr_T max_lnum = buf->b_ml.ml_line_lnum;
+ if (start_lnum > max_lnum) {
+ return 0;
+ }
if (start_lnum == end_lnum) {
- return deleted_bytes - ((bcount_t)STRLEN(first) - end_col + 1);
+ return end_col - start_col;
}
+ const char *first = (const char *)ml_get_buf(buf, start_lnum, false);
+ bcount_t deleted_bytes = (bcount_t)STRLEN(first) - start_col + 1;
for (linenr_T i = 1; i <= end_lnum-start_lnum-1; i++) {
+ if (start_lnum + i > max_lnum) {
+ return deleted_bytes;
+ }
deleted_bytes += (bcount_t)STRLEN(
- ml_get(start_lnum + i)) + 1;
+ ml_get_buf(buf, start_lnum + i, false)) + 1;
+ }
+ if (end_lnum > max_lnum) {
+ return deleted_bytes;
}
- return deleted_bytes + end_col + 1;
+ return deleted_bytes + end_col;
}
diff --git a/test/functional/lua/buffer_updates_spec.lua b/test/functional/lua/buffer_updates_spec.lua
index bd7f0b9cdd..5bfab1d52d 100644
--- a/test/functional/lua/buffer_updates_spec.lua
+++ b/test/functional/lua/buffer_updates_spec.lua
@@ -962,20 +962,20 @@ describe('lua: nvim_buf_attach on_bytes', function()
local check_events = setup_eventcheck(verify, {"AAA", "BBB"})
-- delete
- command("lua vim.api.nvim_buf_set_lines(0, 0, 1, true, {})")
+ meths.buf_set_lines(0, 0, 1, true, {})
check_events {
{ "test1", "bytes", 1, 3, 0, 0, 0, 1, 0, 4, 0, 0, 0 };
}
-- add
- command("lua vim.api.nvim_buf_set_lines(0, 0, 0, true, {'asdf'})")
+ meths.buf_set_lines(0, 0, 0, true, {'asdf'})
check_events {
{ "test1", "bytes", 1, 4, 0, 0, 0, 0, 0, 0, 1, 0, 5 };
}
-- replace
- command("lua vim.api.nvim_buf_set_lines(0, 0, 1, true, {'asdf', 'fdsa'})")
+ meths.buf_set_lines(0, 0, 1, true, {'asdf', 'fdsa'})
check_events {
{ "test1", "bytes", 1, 5, 0, 0, 0, 1, 0, 5, 2, 0, 10 };
}