diff options
Diffstat (limited to 'src/nvim/api')
-rw-r--r-- | src/nvim/api/buffer.c | 21 | ||||
-rw-r--r-- | src/nvim/api/keysets.h | 7 | ||||
-rw-r--r-- | src/nvim/api/private/helpers.c | 21 | ||||
-rw-r--r-- | src/nvim/api/window.c | 103 | ||||
-rw-r--r-- | src/nvim/api/window.h | 1 |
5 files changed, 132 insertions, 21 deletions
diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index a0322556b4..3b73237b06 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -1334,27 +1334,6 @@ static void fix_cursor(linenr_T lo, linenr_T hi, linenr_T extra) invalidate_botline(); } -// Normalizes 0-based indexes to buffer line numbers -static 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; - // Fix if < 0 - index = index < 0 ? max_index + index + 1 : index; - - // Check for oob - if (index > max_index) { - *oob = true; - index = max_index; - } else if (index < 0) { - *oob = true; - index = 0; - } - // Convert the index to a vim line number - index++; - return index; -} - /// Initialise a string array either: /// - on the Lua stack (as a table) (if lstate is not NULL) /// - as an API array object (if lstate is NULL). diff --git a/src/nvim/api/keysets.h b/src/nvim/api/keysets.h index 333b90d7fd..e08186161a 100644 --- a/src/nvim/api/keysets.h +++ b/src/nvim/api/keysets.h @@ -172,6 +172,13 @@ typedef struct { } Dict(get_highlight); typedef struct { + Object start_row; + Object end_row; + Object start_vcol; + Object end_vcol; +} Dict(win_text_height); + +typedef struct { Object buffer; Object event; Object group; 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 diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index 5fb07ecbc8..07aad2cd88 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -8,6 +8,7 @@ #include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" +#include "nvim/api/private/validate.h" #include "nvim/api/window.h" #include "nvim/ascii.h" #include "nvim/buffer_defs.h" @@ -20,6 +21,7 @@ #include "nvim/lua/executor.h" #include "nvim/memline_defs.h" #include "nvim/move.h" +#include "nvim/plines.h" #include "nvim/pos.h" #include "nvim/types.h" #include "nvim/window.h" @@ -462,3 +464,104 @@ void nvim_win_set_hl_ns(Window window, Integer ns_id, Error *err) win->w_hl_needs_update = true; redraw_later(win, UPD_NOT_VALID); } + +/// Computes the number of screen lines occupied by a range of text in a given window. +/// Works for off-screen text and takes folds into account. +/// +/// Diff filler or virtual lines above a line are counted as a part of that line, +/// unless the line is on "start_row" and "start_vcol" is specified. +/// +/// Diff filler or virtual lines below the last buffer line are counted in the result +/// when "end_row" is omitted. +/// +/// Line indexing is similar to |nvim_buf_get_text()|. +/// +/// @param window Window handle, or 0 for current window. +/// @param opts Optional parameters: +/// - start_row: Starting line index, 0-based inclusive. +/// When omitted start at the very top. +/// - end_row: Ending line index, 0-based inclusive. +/// When omitted end at the very bottom. +/// - start_vcol: Starting virtual column index on "start_row", +/// 0-based inclusive, rounded down to full screen lines. +/// When omitted include the whole line. +/// - end_vcol: Ending virtual column index on "end_row", +/// 0-based exclusive, rounded up to full screen lines. +/// When omitted include the whole line. +/// @return The number of screen lines that the range of text occupy. +/// +/// @see |virtcol()| for text width. +Object nvim_win_text_height(Window window, Dict(win_text_height) *opts, Error *err) + FUNC_API_SINCE(12) +{ + win_T *const win = find_window_by_handle(window, err); + if (!win) { + return NIL; + } + buf_T *const buf = win->w_buffer; + const linenr_T line_count = buf->b_ml.ml_line_count; + + linenr_T start_lnum = 1; + linenr_T end_lnum = line_count; + int64_t start_vcol = -1; + int64_t end_vcol = -1; + + bool oob = false; + + if (HAS_KEY(opts->start_row)) { + VALIDATE_T("start_row", kObjectTypeInteger, opts->start_row.type, { + return NIL; + }); + start_lnum = (linenr_T)normalize_index(buf, opts->start_row.data.integer, false, &oob); + } + + if (HAS_KEY(opts->end_row)) { + VALIDATE_T("end_row", kObjectTypeInteger, opts->end_row.type, { + return NIL; + }); + end_lnum = (linenr_T)normalize_index(buf, opts->end_row.data.integer, false, &oob); + } + + VALIDATE(!oob, "%s", "Line index out of bounds", { + return NIL; + }); + VALIDATE((start_lnum <= end_lnum), "%s", "'start_row' is higher than 'end_row'", { + return NIL; + }); + + if (HAS_KEY(opts->start_vcol)) { + VALIDATE(HAS_KEY(opts->start_row), "%s", "'start_vcol' specified without 'start_row'", { + return NIL; + }); + VALIDATE_T("start_vcol", kObjectTypeInteger, opts->start_vcol.type, { + return NIL; + }); + start_vcol = opts->start_vcol.data.integer; + VALIDATE_RANGE((start_vcol >= 0 && start_vcol <= MAXCOL), "start_vcol", { + return NIL; + }); + } + + if (HAS_KEY(opts->end_vcol)) { + VALIDATE(HAS_KEY(opts->end_row), "%s", "'end_vcol' specified without 'end_row'", { + return NIL; + }); + VALIDATE_T("end_vcol", kObjectTypeInteger, opts->end_vcol.type, { + return NIL; + }); + end_vcol = opts->end_vcol.data.integer; + VALIDATE_RANGE((end_vcol >= 0 && end_vcol <= MAXCOL), "end_vcol", { + return NIL; + }); + } + + if (start_lnum == end_lnum && start_vcol >= 0 && end_vcol >= 0) { + VALIDATE((start_vcol <= end_vcol), "%s", "'start_vcol' is higher than 'end_vcol'", { + return NIL; + }); + } + + const int64_t res = win_text_height(win, start_lnum, start_vcol, end_lnum, end_vcol) + + (HAS_KEY(opts->end_row) ? 0 : win_get_fill(win, line_count + 1)); + return INTEGER_OBJ(res); +} diff --git a/src/nvim/api/window.h b/src/nvim/api/window.h index 0f36c12a9f..046d64c1a4 100644 --- a/src/nvim/api/window.h +++ b/src/nvim/api/window.h @@ -1,6 +1,7 @@ #ifndef NVIM_API_WINDOW_H #define NVIM_API_WINDOW_H +#include "nvim/api/keysets.h" #include "nvim/api/private/defs.h" #ifdef INCLUDE_GENERATED_DECLARATIONS |