aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTony Chen <tchen1998@gmail.com>2020-12-21 17:51:52 -0800
committerBjörn Linse <bjorn.linse@gmail.com>2021-01-01 19:51:45 +0100
commit45b14f88db1b332938f4a73be28966ae60563a50 (patch)
tree5603556abeb06119df0f30af921ba20f71a498f2 /src
parent29ad2ebc1688176a1c7acaa81103dac289de0ad1 (diff)
downloadrneovim-45b14f88db1b332938f4a73be28966ae60563a50.tar.gz
rneovim-45b14f88db1b332938f4a73be28966ae60563a50.tar.bz2
rneovim-45b14f88db1b332938f4a73be28966ae60563a50.zip
api: set_text: rebase, update to new api, and add more tests
Diffstat (limited to 'src')
-rw-r--r--src/nvim/api/buffer.c66
-rw-r--r--src/nvim/extmark.c17
2 files changed, 75 insertions, 8 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,