diff options
author | zeertzjq <zeertzjq@outlook.com> | 2023-07-11 07:15:46 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-11 07:15:46 +0800 |
commit | db8fe63a9398efd57c3ff28aa3d93e45fb70ee1a (patch) | |
tree | 31107910712b0454160b28441b7945a8167628c7 /src/nvim/api/private/helpers.c | |
parent | 3750e5ed9c740a4334628c4a560d77c1dc2d0bfd (diff) | |
download | rneovim-db8fe63a9398efd57c3ff28aa3d93e45fb70ee1a.tar.gz rneovim-db8fe63a9398efd57c3ff28aa3d93e45fb70ee1a.tar.bz2 rneovim-db8fe63a9398efd57c3ff28aa3d93e45fb70ee1a.zip |
feat(api): add nvim_win_text_height (#24236)
It uses the same code as "scroll_delta" of "win_viewport" UI event to
calculate text height, but is more flexible.
Diffstat (limited to 'src/nvim/api/private/helpers.c')
-rw-r--r-- | src/nvim/api/private/helpers.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/nvim/api/private/helpers.c b/src/nvim/api/private/helpers.c index f9861c82bf..d0c8ab4dd4 100644 --- a/src/nvim/api/private/helpers.c +++ b/src/nvim/api/private/helpers.c @@ -478,6 +478,27 @@ Array string_to_array(const String input, bool crlf) return ret; } +/// Normalizes 0-based indexes to buffer line numbers. +int64_t normalize_index(buf_T *buf, int64_t index, bool end_exclusive, bool *oob) +{ + assert(buf->b_ml.ml_line_count > 0); + int64_t max_index = buf->b_ml.ml_line_count + (int)end_exclusive - 1; + // A negative index counts from the bottom. + index = index < 0 ? max_index + index + 1 : index; + + // Check for oob and clamp. + if (index > max_index) { + *oob = true; + index = max_index; + } else if (index < 0) { + *oob = true; + index = 0; + } + // Convert the index to a 1-based line number. + index++; + return index; +} + /// Returns a substring of a buffer line /// /// @param buf Buffer handle |