mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
API: Refactor buffer_{get,set}_line
They are now implemented on top of the buffer_{get,set}_slice functions
This commit is contained in:
parent
4dc34bc0e0
commit
8eb67404f0
@ -51,91 +51,20 @@ int64_t buffer_get_length(Buffer buffer, Error *err)
|
|||||||
|
|
||||||
String buffer_get_line(Buffer buffer, int64_t index, Error *err)
|
String buffer_get_line(Buffer buffer, int64_t index, Error *err)
|
||||||
{
|
{
|
||||||
String rv;
|
String rv = {.size = 0};
|
||||||
buf_T *buf = find_buffer(buffer, err);
|
StringArray slice = buffer_get_slice(buffer, index, index, true, true, err);
|
||||||
|
|
||||||
if (!buf) {
|
if (slice.size) {
|
||||||
return rv;
|
rv = slice.items[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
index = normalize_index(buf, index);
|
|
||||||
char *line = (char *)ml_get_buf(buf, index, false);
|
|
||||||
rv.size = strlen(line);
|
|
||||||
rv.data = xmalloc(rv.size);
|
|
||||||
memcpy(rv.data, line, rv.size);
|
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
void buffer_set_line(Buffer buffer, int64_t index, Object line, Error *err)
|
void buffer_set_line(Buffer buffer, int64_t index, String line, Error *err)
|
||||||
{
|
{
|
||||||
buf_T *buf = find_buffer(buffer, err);
|
StringArray array = {.items = &line, .size = 1};
|
||||||
|
buffer_set_slice(buffer, index, index, true, true, array, err);
|
||||||
if (!buf) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (line.type != kObjectTypeNil && line.type != kObjectTypeString) {
|
|
||||||
set_api_error("Invalid line", err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
index = normalize_index(buf, index);
|
|
||||||
buf_T *save_curbuf = NULL;
|
|
||||||
win_T *save_curwin = NULL;
|
|
||||||
tabpage_T *save_curtab = NULL;
|
|
||||||
try_start();
|
|
||||||
switch_to_win_for_buf(buf, &save_curwin, &save_curtab, &save_curbuf);
|
|
||||||
|
|
||||||
if (line.type == kObjectTypeNil) {
|
|
||||||
// Delete the line
|
|
||||||
|
|
||||||
if (u_savedel(index, 1L) == FAIL) {
|
|
||||||
// Failed to save undo
|
|
||||||
set_api_error("Cannot save undo information", err);
|
|
||||||
} else if (ml_delete(index, FALSE) == FAIL) {
|
|
||||||
// Failed to delete
|
|
||||||
set_api_error("Cannot delete the line", err);
|
|
||||||
} else {
|
|
||||||
restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
|
|
||||||
// Success
|
|
||||||
if (buf == curbuf) {
|
|
||||||
// fix the cursor if it's the current buffer
|
|
||||||
fix_cursor(index, index + 1, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (save_curbuf == NULL) {
|
|
||||||
// Only adjust marks if we managed to switch to a window that
|
|
||||||
// holds the buffer, otherwise line numbers will be invalid.
|
|
||||||
deleted_lines_mark(index, 1L);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (line.type == kObjectTypeString) {
|
|
||||||
// Replace line
|
|
||||||
char *string = xmalloc(line.data.string.size + 1);
|
|
||||||
memcpy(string, line.data.string.data, line.data.string.size);
|
|
||||||
string[line.data.string.size] = NUL;
|
|
||||||
|
|
||||||
if (u_savesub(index) == FAIL) {
|
|
||||||
// Failed to save undo
|
|
||||||
set_api_error("Cannot save undo information", err);
|
|
||||||
} else if (ml_replace(index, (char_u *)string, FALSE) == FAIL) {
|
|
||||||
// Failed to replace
|
|
||||||
set_api_error("Cannot replace line", err);
|
|
||||||
free(string);
|
|
||||||
} else {
|
|
||||||
// Success
|
|
||||||
changed_bytes(index, 0);
|
|
||||||
restore_win_for_buf(save_curwin, save_curtab, save_curbuf);
|
|
||||||
|
|
||||||
// Check that the cursor is not beyond the end of the line now.
|
|
||||||
if (buf == curbuf) {
|
|
||||||
check_cursor_col();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try_end(err);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StringArray buffer_get_slice(Buffer buffer,
|
StringArray buffer_get_slice(Buffer buffer,
|
||||||
|
@ -25,10 +25,9 @@ String buffer_get_line(Buffer buffer, int64_t index, Error *err);
|
|||||||
///
|
///
|
||||||
/// @param buffer The buffer handle
|
/// @param buffer The buffer handle
|
||||||
/// @param index The line index
|
/// @param index The line index
|
||||||
/// @param line The new line. This can can be a String(replacement) or
|
/// @param line The new line.
|
||||||
/// Nil(delete). Anything else is an error.
|
|
||||||
/// @param[out] err Details of an error that may have occurred
|
/// @param[out] err Details of an error that may have occurred
|
||||||
void buffer_set_line(Buffer buffer, int64_t index, Object line, Error *err);
|
void buffer_set_line(Buffer buffer, int64_t index, String line, Error *err);
|
||||||
|
|
||||||
/// Retrieves a line range from the buffer
|
/// Retrieves a line range from the buffer
|
||||||
///
|
///
|
||||||
|
@ -136,7 +136,7 @@ String vim_get_current_line(Error *err)
|
|||||||
return buffer_get_line(curbuf->b_fnum, curwin->w_cursor.lnum - 1, err);
|
return buffer_get_line(curbuf->b_fnum, curwin->w_cursor.lnum - 1, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
void vim_set_current_line(Object line, Error *err)
|
void vim_set_current_line(String line, Error *err)
|
||||||
{
|
{
|
||||||
buffer_set_line(curbuf->b_fnum, curwin->w_cursor.lnum - 1, line, err);
|
buffer_set_line(curbuf->b_fnum, curwin->w_cursor.lnum - 1, line, err);
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ String vim_get_current_line(Error *err);
|
|||||||
///
|
///
|
||||||
/// @param line The line contents
|
/// @param line The line contents
|
||||||
/// @param[out] err Details of an error that may have occurred
|
/// @param[out] err Details of an error that may have occurred
|
||||||
void vim_set_current_line(Object line, Error *err);
|
void vim_set_current_line(String line, Error *err);
|
||||||
|
|
||||||
/// Gets a global or special variable
|
/// Gets a global or special variable
|
||||||
///
|
///
|
||||||
|
Loading…
Reference in New Issue
Block a user