diff options
author | Tony Chen <tchen1998@gmail.com> | 2020-12-21 17:51:52 -0800 |
---|---|---|
committer | Björn Linse <bjorn.linse@gmail.com> | 2021-01-01 19:51:45 +0100 |
commit | 45b14f88db1b332938f4a73be28966ae60563a50 (patch) | |
tree | 5603556abeb06119df0f30af921ba20f71a498f2 | |
parent | 29ad2ebc1688176a1c7acaa81103dac289de0ad1 (diff) | |
download | rneovim-45b14f88db1b332938f4a73be28966ae60563a50.tar.gz rneovim-45b14f88db1b332938f4a73be28966ae60563a50.tar.bz2 rneovim-45b14f88db1b332938f4a73be28966ae60563a50.zip |
api: set_text: rebase, update to new api, and add more tests
-rw-r--r-- | src/nvim/api/buffer.c | 66 | ||||
-rw-r--r-- | src/nvim/extmark.c | 17 | ||||
-rw-r--r-- | test/functional/api/buffer_spec.lua | 63 |
3 files changed, 129 insertions, 17 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index 7fa30e7a7d..21a7e897a9 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -309,6 +309,27 @@ end: return rv; } +static bool check_string_array(Array arr, bool disallow_nl, Error *err) +{ + for (size_t i = 0; i < arr.size; i++) { + if (arr.items[i].type != kObjectTypeString) { + api_set_error(err, + kErrorTypeValidation, + "All items in the replacement array must be strings"); + return false; + } + // Disallow newlines in the middle of the line. + if (disallow_nl) { + const String l = arr.items[i].data.string; + if (memchr(l.data, NL, l.size)) { + api_set_error(err, kErrorTypeValidation, + "String cannot contain newlines"); + return false; + } + } + } + return true; +} /// Sets (replaces) a line-range in the buffer. /// @@ -519,10 +540,10 @@ void nvim_buf_set_text(uint64_t channel_id, } end_row = normalize_index(buf, end_row, &oob); - /* if (oob) { */ - /* api_set_error(err, kErrorTypeValidation, "end_row out of bounds"); */ - /* return; */ - /* } */ + if (oob) { + api_set_error(err, kErrorTypeValidation, "end_row out of bounds"); + return; + } char *str_at_start = (char *)ml_get_buf(buf, start_row, false); if (start_col < 0 || (size_t)start_col > strlen(str_at_start)) { @@ -547,10 +568,36 @@ void nvim_buf_set_text(uint64_t channel_id, return; } + bcount_t new_byte = 0; + bcount_t old_byte = 0; + + // calculate byte size of old region before it gets modified/deleted + if (start_row == end_row) { + old_byte = (bcount_t)end_col - start_col; + } else { + char_u *line; + old_byte += (bcount_t)strlen(str_at_start) - start_col; + for (size_t i = 0; i < (bcount_t)end_row - start_row; i++){ + int64_t lnum = start_row + (int64_t)i; + + if (lnum >= MAXLNUM) { + api_set_error(err, kErrorTypeValidation, "Index value is too high"); + goto end; + } + + line = ml_get_buf(buf, lnum, false); + old_byte += (bcount_t)(strlen((char *)line)); + } + old_byte += end_col; + } + size_t new_len = replacement.size; String first_item = replacement.items[0].data.string; String last_item = replacement.items[replacement.size-1].data.string; + + new_byte += (bcount_t)(first_item.size); + size_t firstlen = (size_t)start_col+first_item.size; size_t last_part_len = strlen(str_at_end) - (size_t)end_col; if (replacement.size == 1) { @@ -578,10 +625,11 @@ void nvim_buf_set_text(uint64_t channel_id, // NL-used-for-NUL. lines[i] = xmemdupz(l.data, l.size); memchrsub(lines[i], NUL, NL, l.size); - /* lines[i] = replacement.items[i].data.string.data; */ + new_byte += (bcount_t)(l.size); } if (replacement.size > 1) { lines[replacement.size-1] = last; + new_byte += (bcount_t)(last_item.size); } try_start(); @@ -593,7 +641,7 @@ void nvim_buf_set_text(uint64_t channel_id, goto end; } - if (u_save((linenr_T)start_row - 1, (linenr_T)end_row) == FAIL) { + if (u_save((linenr_T)start_row - 1, (linenr_T)end_row + 1) == FAIL) { api_set_error(err, kErrorTypeException, "Failed to save undo information"); goto end; } @@ -670,8 +718,10 @@ void nvim_buf_set_text(uint64_t channel_id, colnr_T col_extent = (colnr_T)(end_col - ((end_col > start_col) ? start_col : 0)); extmark_splice(buf, (int)start_row-1, (colnr_T)start_col, - (int)(end_row-start_row), col_extent, - (int)new_len-1, (colnr_T)last_item.size, kExtmarkUndo); + (int)(end_row-start_row), col_extent, (bcount_t)(old_byte), + (int)new_len-1, (colnr_T)last_item.size, (bcount_t)new_byte, + kExtmarkUndo); + changed_lines((linenr_T)start_row, 0, (linenr_T)end_row, (long)extra, true); diff --git a/src/nvim/extmark.c b/src/nvim/extmark.c index ba685b158e..b2d8532cd7 100644 --- a/src/nvim/extmark.c +++ b/src/nvim/extmark.c @@ -560,6 +560,23 @@ void extmark_adjust(buf_T *buf, new_row, 0, new_byte, undo); } +// Adjust extmarks following a text edit. +// +// @param buf +// @param start_row Start row of the region to be changed +// @param start_col Start col of the region to be changed +// @param old_row End row of the region to be changed. +// Encoded as an offset to start_row. +// @param old_col End col of the region to be changed. Encodes +// an offset from start_col if old_row = 0; otherwise, +// encodes the end column of the old region. +// @param old_byte Byte extent of the region to be changed. +// @param new_row Row offset of the new region. +// @param new_col Col offset of the new region. Encodes an offset from +// start_col if new_row = 0; otherwise, encodes +// the end column of the new region. +// @param new_byte Byte extent of the new region. +// @param undo void extmark_splice(buf_T *buf, int start_row, colnr_T start_col, int old_row, colnr_T old_col, bcount_t old_byte, diff --git a/test/functional/api/buffer_spec.lua b/test/functional/api/buffer_spec.lua index 60ff246436..dbe585a064 100644 --- a/test/functional/api/buffer_spec.lua +++ b/test/functional/api/buffer_spec.lua @@ -409,6 +409,14 @@ describe('api/buf', function() set_text(0, 6, 0, 9, {'world'}) eq({'hello world!', 'text'}, get_lines(0, 2, true)) + -- can insert text + set_text(0, 0, 0, 0, {'well '}) + eq({'well hello world!', 'text'}, get_lines(0, 2, true)) + + -- can delete text + set_text(0, 0, 0, 5, {''}) + eq({'hello world!', 'text'}, get_lines(0, 2, true)) + -- can replace with multiple lines local err = set_text(0, 6, 0, 11, {'foo', 'wo', 'more'}) eq({'hello foo', 'wo', 'more!', 'text'}, get_lines(0, 4, true)) @@ -418,6 +426,43 @@ describe('api/buf', function() eq({'hello bar'}, get_lines(0, 1, true)) end) + pending('can handle multibyte characters', function() + insert([[ + hellØ world! + ]]) + + eq({'hellØ world!'}, get_lines(0, 1, true)) + + -- inserting multibyte + set_text(0, 11, 0, 11, {'Ø'}) + eq({'hellØ worldØ!'}, get_lines(0, 1, true)) + + -- deleting multibyte + set_text(0, 0, 0, 6, {''}) + eq({'worldØ!'}, get_lines(0, 1, true)) + end) + + it('works with undo', function() + insert([[ + hello world! + ]]) + + -- setting text + set_text(0, 0, 0, 0, {'well '}) + feed('u') + eq({'hello world!'}, get_lines(0, 1, true)) + + -- deleting text + set_text(0, 0, 0, 6, {''}) + feed('u') + eq({'hello world!'}, get_lines(0, 1, true)) + + -- inserting newlines + set_text(0, 0, 0, 0, {'hello', 'mr '}) + feed('u') + eq({'hello world!'}, get_lines(0, 1, true)) + end) + it('updates the cursor position', function() insert([[ hello world! @@ -443,34 +488,34 @@ describe('api/buf', function() foo bar baz ]]) - local id1 = curbufmeths.set_extmark(ns, 0, 0, 1, {}) - local id2 = curbufmeths.set_extmark(ns, 0, 0, 7, {}) - local id3 = curbufmeths.set_extmark(ns, 0, 1, 1, {}) + local id1 = curbufmeths.set_extmark(ns, 0, 1, {}) + local id2 = curbufmeths.set_extmark(ns, 0, 7, {}) + local id3 = curbufmeths.set_extmark(ns, 1, 1, {}) set_text(0, 4, 0, 7, {"q"}) -- TODO: if we set text at 0,3, what happens to the mark at 0,3 eq({'foo q', 'baz'}, get_lines(0, 2, true)) -- mark before replacement point is unaffected - rv = curbufmeths.get_extmark_by_id(ns, id1) + rv = curbufmeths.get_extmark_by_id(ns, id1, {}) eq({0, 1}, rv) -- mark gets shifted back because the replacement was shorter - rv = curbufmeths.get_extmark_by_id(ns, id2) + rv = curbufmeths.get_extmark_by_id(ns, id2, {}) eq({0, 5}, rv) -- mark on the next line is unaffected - rv = curbufmeths.get_extmark_by_id(ns, id3) + rv = curbufmeths.get_extmark_by_id(ns, id3, {}) eq({1, 1}, rv) -- replacing the text spanning two lines will adjust the mark on the next line set_text(0, 3, 1, 3, {"qux"}) - rv = curbufmeths.get_extmark_by_id(ns, id3) + rv = curbufmeths.get_extmark_by_id(ns, id3, {}) eq({'fooqux', ''}, get_lines(0, 2, true)) eq({0, 6}, rv) -- but mark before replacement point is still unaffected - rv = curbufmeths.get_extmark_by_id(ns, id1) + rv = curbufmeths.get_extmark_by_id(ns, id1, {}) eq({0, 1}, rv) -- and the mark in the middle was shifted to the end of the insertion - rv = curbufmeths.get_extmark_by_id(ns, id2) + rv = curbufmeths.get_extmark_by_id(ns, id2, {}) eq({0, 6}, rv) end) end) |