diff options
Diffstat (limited to 'src/nvim/api/buffer.c')
-rw-r--r-- | src/nvim/api/buffer.c | 284 |
1 files changed, 268 insertions, 16 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index f1151d196a..8d82d22040 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. /// @@ -362,22 +383,9 @@ void nvim_buf_set_lines(uint64_t channel_id, return; } - for (size_t i = 0; i < replacement.size; i++) { - if (replacement.items[i].type != kObjectTypeString) { - api_set_error(err, - kErrorTypeValidation, - "All items in the replacement array must be strings"); - return; - } - // Disallow newlines in the middle of the line. - if (channel_id != VIML_INTERNAL_CALL) { - const String l = replacement.items[i].data.string; - if (memchr(l.data, NL, l.size)) { - api_set_error(err, kErrorTypeValidation, - "String cannot contain newlines"); - return; - } - } + bool disallow_nl = (channel_id != VIML_INTERNAL_CALL); + if (!check_string_array(replacement, disallow_nl, err)) { + return; } size_t new_len = replacement.size; @@ -487,6 +495,250 @@ end: try_end(err); } +/// Sets (replaces) a range in the buffer +/// +/// This is recommended over nvim_buf_set_lines when only modifying parts of a +/// line, as extmarks will be preserved on non-modified parts of the touched +/// lines. +/// +/// Indexing is zero-based and end-exclusive. +/// +/// To insert text at a given index, set `start` and `end` ranges to the same +/// index. To delete a range, set `replacement` to an array containing +/// an empty string, or simply an empty array. +/// +/// Prefer nvim_buf_set_lines when adding or deleting entire lines only. +/// +/// @param channel_id +/// @param buffer Buffer handle, or 0 for current buffer +/// @param start_row First line index +/// @param start_column Last column +/// @param end_row Last line index +/// @param end_column Last column +/// @param replacement Array of lines to use as replacement +/// @param[out] err Error details, if any +void nvim_buf_set_text(uint64_t channel_id, Buffer buffer, + Integer start_row, Integer start_col, + Integer end_row, Integer end_col, + ArrayOf(String) replacement, Error *err) + FUNC_API_SINCE(7) +{ + FIXED_TEMP_ARRAY(scratch, 1); + if (replacement.size == 0) { + scratch.items[0] = STRING_OBJ(STATIC_CSTR_AS_STRING("")); + replacement = scratch; + } + + buf_T *buf = find_buffer_by_handle(buffer, err); + if (!buf) { + return; + } + + bool oob = false; + + // check range is ordered and everything! + // start_row, end_row within buffer len (except add text past the end?) + start_row = normalize_index(buf, start_row, &oob); + if (oob || start_row == buf->b_ml.ml_line_count + 1) { + api_set_error(err, kErrorTypeValidation, "start_row out of bounds"); + return; + } + + end_row = normalize_index(buf, end_row, &oob); + if (oob || end_row == buf->b_ml.ml_line_count + 1) { + 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)) { + api_set_error(err, kErrorTypeValidation, "start_col out of bounds"); + return; + } + + char *str_at_end = (char *)ml_get_buf(buf, end_row, false); + size_t len_at_end = strlen(str_at_end); + if (end_col < 0 || (size_t)end_col > len_at_end) { + api_set_error(err, kErrorTypeValidation, "end_col out of bounds"); + return; + } + + if (start_row > end_row || (end_row == start_row && start_col > end_col)) { + api_set_error(err, kErrorTypeValidation, "start is higher than end"); + return; + } + + bool disallow_nl = (channel_id != VIML_INTERNAL_CALL); + if (!check_string_array(replacement, disallow_nl, err)) { + return; + } + + size_t new_len = replacement.size; + + 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 { + const char *bufline; + old_byte += (bcount_t)strlen(str_at_start) - start_col; + for (int64_t i = 1; i < end_row - start_row; i++) { + int64_t lnum = start_row + i; + + bufline = (char *)ml_get_buf(buf, lnum, false); + old_byte += (bcount_t)(strlen(bufline))+1; + } + old_byte += (bcount_t)end_col+1; + } + + String first_item = replacement.items[0].data.string; + String last_item = replacement.items[replacement.size-1].data.string; + + 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) { + firstlen += last_part_len; + } + char *first = xmallocz(firstlen), *last = NULL; + memcpy(first, str_at_start, (size_t)start_col); + memcpy(first+start_col, first_item.data, first_item.size); + memchrsub(first+start_col, NUL, NL, first_item.size); + if (replacement.size == 1) { + memcpy(first+start_col+first_item.size, str_at_end+end_col, last_part_len); + } else { + last = xmallocz(last_item.size+last_part_len); + memcpy(last, last_item.data, last_item.size); + memchrsub(last, NUL, NL, last_item.size); + memcpy(last+last_item.size, str_at_end+end_col, last_part_len); + } + + char **lines = (new_len != 0) ? xcalloc(new_len, sizeof(char *)) : NULL; + lines[0] = first; + new_byte += (bcount_t)(first_item.size); + for (size_t i = 1; i < new_len-1; i++) { + const String l = replacement.items[i].data.string; + + // Fill lines[i] with l's contents. Convert NULs to newlines as required by + // NL-used-for-NUL. + lines[i] = xmemdupz(l.data, l.size); + memchrsub(lines[i], NUL, NL, l.size); + new_byte += (bcount_t)(l.size)+1; + } + if (replacement.size > 1) { + lines[replacement.size-1] = last; + new_byte += (bcount_t)(last_item.size)+1; + } + + try_start(); + aco_save_T aco; + aucmd_prepbuf(&aco, (buf_T *)buf); + + if (!MODIFIABLE(buf)) { + api_set_error(err, kErrorTypeException, "Buffer is not 'modifiable'"); + goto end; + } + + // Small note about undo states: unlike set_lines, we want to save the + // undo state of one past the end_row, since end_row is inclusive. + 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; + } + + ptrdiff_t extra = 0; // lines added to text, can be negative + size_t old_len = (size_t)(end_row-start_row+1); + + // 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 + // repeatedly deleting line "start". + size_t to_delete = (new_len < old_len) ? (size_t)(old_len - new_len) : 0; + for (size_t i = 0; i < to_delete; i++) { + if (ml_delete((linenr_T)start_row, false) == FAIL) { + api_set_error(err, kErrorTypeException, "Failed to delete line"); + goto end; + } + } + + if (to_delete > 0) { + extra -= (ptrdiff_t)to_delete; + } + + // For as long as possible, replace the existing old_len with the + // new old_len. This is a more efficient operation, as it requires + // less memory allocation and freeing. + size_t to_replace = old_len < new_len ? old_len : new_len; + for (size_t i = 0; i < to_replace; i++) { + int64_t lnum = start_row + (int64_t)i; + + if (lnum >= MAXLNUM) { + api_set_error(err, kErrorTypeValidation, "Index value is too high"); + goto end; + } + + if (ml_replace((linenr_T)lnum, (char_u *)lines[i], false) == FAIL) { + api_set_error(err, kErrorTypeException, "Failed to replace line"); + goto end; + } + // Mark lines that haven't been passed to the buffer as they need + // to be freed later + lines[i] = NULL; + } + + // Now we may need to insert the remaining new old_len + for (size_t i = to_replace; i < new_len; i++) { + int64_t lnum = start_row + (int64_t)i - 1; + + if (lnum >= MAXLNUM) { + api_set_error(err, kErrorTypeValidation, "Index value is too high"); + goto end; + } + + if (ml_append((linenr_T)lnum, (char_u *)lines[i], 0, false) == FAIL) { + api_set_error(err, kErrorTypeException, "Failed to insert line"); + goto end; + } + + // Same as with replacing, but we also need to free lines + xfree(lines[i]); + lines[i] = NULL; + extra++; + } + + // Adjust marks. Invalidate any which lie in the + // changed range, and move any in the remainder of the buffer. + mark_adjust((linenr_T)start_row, + (linenr_T)end_row, + MAXLNUM, + (long)extra, + kExtmarkNOOP); + + colnr_T col_extent = (colnr_T)(end_col + - ((end_row == start_row) ? start_col : 0)); + extmark_splice(buf, (int)start_row-1, (colnr_T)start_col, + (int)(end_row-start_row), col_extent, old_byte, + (int)new_len-1, (colnr_T)last_item.size, new_byte, + kExtmarkUndo); + + + changed_lines((linenr_T)start_row, 0, (linenr_T)end_row, (long)extra, true); + + // adjust cursor like an extmark ( i e it was inside last_part_len) + if (curwin->w_cursor.lnum == end_row && curwin->w_cursor.col > end_col) { + curwin->w_cursor.col -= col_extent - (colnr_T)last_item.size; + } + fix_cursor((linenr_T)start_row, (linenr_T)end_row, (linenr_T)extra); + +end: + for (size_t i = 0; i < new_len; i++) { + xfree(lines[i]); + } + xfree(lines); + aucmd_restbuf(&aco); + try_end(err); +} + /// Returns the byte offset of a line (0-indexed). |api-indexing| /// /// Line 1 (index=0) has offset 0. UTF-8 bytes are counted. EOL is one byte. |