mirror of
https://github.com/neovim/neovim.git
synced 2025-09-09 04:48:18 +00:00
api: implement nvim_buf_get_offset_for_line
Like line2byte, but works for any buffer, and uses zero-based indexing (API conventions).
This commit is contained in:
@@ -491,6 +491,42 @@ end:
|
||||
try_end(err);
|
||||
}
|
||||
|
||||
/// Return the byte offset for a line.
|
||||
//
|
||||
/// This includes the end-of-line character, depending on the 'fileformat'
|
||||
/// option for the current buffer. The first line returns 0. UTF-8 encoding is
|
||||
/// used, 'fileencoding' is ignored. Sending in the index just below the last
|
||||
/// line gives the total byte count for the entire file. A final newline is
|
||||
/// included if it would be written, see 'eol'.
|
||||
///
|
||||
/// Unlike |line2byte()|, throws error for out-of-bounds indexing.
|
||||
/// Returns -1 for unloaded buffer.
|
||||
///
|
||||
/// @param buffer Buffer handle
|
||||
/// @param index Line index
|
||||
/// @param[out] err Error details, if any
|
||||
/// @return Integer Byte offset
|
||||
Integer nvim_buf_get_offset_for_line(Buffer buffer, Integer index, Error *err)
|
||||
FUNC_API_SINCE(5)
|
||||
{
|
||||
buf_T *buf = find_buffer_by_handle(buffer, err);
|
||||
if (!buf) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// return sentinel value if the buffer isn't loaded
|
||||
if (buf->b_ml.ml_mfp == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (index < 0 || index > buf->b_ml.ml_line_count) {
|
||||
api_set_error(err, kErrorTypeValidation, "Index out of bounds");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ml_find_line_or_offset(buf, (int)index+1, NULL);
|
||||
}
|
||||
|
||||
/// Gets a buffer-scoped (b:) variable.
|
||||
///
|
||||
/// @param buffer Buffer handle
|
||||
|
Reference in New Issue
Block a user