From ac8cd5368db83cced9bc049ceb50c21cb8a4f743 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 12 Mar 2024 10:44:53 +0800 Subject: refactor: use ml_get_buf_len() in API code (#27825) --- src/nvim/lua/stdlib.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/lua/stdlib.c') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 8f58fd1a1a..a5262efcfa 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -107,15 +107,15 @@ static int regex_match_line(lua_State *lstate) } char *line = ml_get_buf(buf, rownr + 1); - size_t len = strlen(line); + colnr_T len = ml_get_buf_len(buf, rownr + 1); - if (start < 0 || (size_t)start > len) { + if (start < 0 || start > len) { return luaL_error(lstate, "invalid start"); } char save = NUL; if (end >= 0) { - if ((size_t)end > len || end < start) { + if (end > len || end < start) { return luaL_error(lstate, "invalid end"); } save = line[end]; -- cgit From f150b62423d57b6f9fbe57330589937dfbb34f4a Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 17 Apr 2024 05:44:06 +0800 Subject: fix(lua): only free luarefs when returning from API (#28373) --- src/nvim/lua/stdlib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua/stdlib.c') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index a5262efcfa..788185f2b4 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -449,7 +449,7 @@ int nlua_getvar(lua_State *lstate) if (di == NULL) { return 0; // nil } - nlua_push_typval(lstate, &di->di_tv, false); + nlua_push_typval(lstate, &di->di_tv, 0); return 1; } -- cgit From 2b6c9bbe7f7ac950683e81129b76e35e35839ede Mon Sep 17 00:00:00 2001 From: Jaehwang Jung Date: Sat, 20 Apr 2024 02:33:44 +0900 Subject: perf(treesitter): incremental foldupdate Problem: While the fold level computation is incremental, the evaluation of the foldexpr is done on the full buffer. Despite that the foldexpr reads from the cache, it can take tens of milliseconds for moderately big (10K lines) buffers. Solution: Track the range of lines on which the foldexpr should be evaluated. --- src/nvim/lua/stdlib.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'src/nvim/lua/stdlib.c') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 788185f2b4..032cb246bf 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -543,14 +543,25 @@ static int nlua_iconv(lua_State *lstate) return 1; } -// Update foldlevels (e.g., by evaluating 'foldexpr') for all lines in the current window without -// invoking other side effects. Unlike `zx`, it does not close manually opened folds and does not -// open folds under the cursor. +// Update foldlevels (e.g., by evaluating 'foldexpr') for the given line range in the given window, +// without invoking other side effects. Unlike `zx`, it does not close manually opened folds and +// does not open folds under the cursor. static int nlua_foldupdate(lua_State *lstate) { - curwin->w_foldinvalid = true; // recompute folds - foldUpdate(curwin, 1, (linenr_T)MAXLNUM); - curwin->w_foldinvalid = false; + handle_T window = (handle_T)luaL_checkinteger(lstate, 1); + Error err = ERROR_INIT; + win_T *win = find_window_by_handle(window, &err); + if (ERROR_SET(&err)) { + nlua_push_errstr(lstate, err.msg); + api_clear_error(&err); + lua_error(lstate); + return 0; + } + + linenr_T start = (linenr_T)luaL_checkinteger(lstate, 2); + linenr_T end = (linenr_T)luaL_checkinteger(lstate, 3); + + foldUpdate(win, start + 1, end); return 0; } -- cgit From 4caf71af58f34726bf6f1d8a2d5ddeaf650e6286 Mon Sep 17 00:00:00 2001 From: Jaehwang Jung Date: Mon, 22 Apr 2024 23:46:43 +0900 Subject: refactor(fold): avoid coverity false-positive Also add some more argument checks. --- src/nvim/lua/stdlib.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'src/nvim/lua/stdlib.c') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 032cb246bf..22ee0a1c98 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -549,19 +549,21 @@ static int nlua_iconv(lua_State *lstate) static int nlua_foldupdate(lua_State *lstate) { handle_T window = (handle_T)luaL_checkinteger(lstate, 1); - Error err = ERROR_INIT; - win_T *win = find_window_by_handle(window, &err); - if (ERROR_SET(&err)) { - nlua_push_errstr(lstate, err.msg); - api_clear_error(&err); - lua_error(lstate); - return 0; + win_T *win = handle_get_window(window); + if (!win) { + return luaL_error(lstate, "invalid window"); + } + // input is zero-based end-exclusive range + linenr_T top = (linenr_T)luaL_checkinteger(lstate, 2) + 1; + if (top < 1 || top > win->w_buffer->b_ml.ml_line_count) { + return luaL_error(lstate, "invalid top"); + } + linenr_T bot = (linenr_T)luaL_checkinteger(lstate, 3); + if (top > bot) { + return luaL_error(lstate, "invalid bot"); } - linenr_T start = (linenr_T)luaL_checkinteger(lstate, 2); - linenr_T end = (linenr_T)luaL_checkinteger(lstate, 3); - - foldUpdate(win, start + 1, end); + foldUpdate(win, top, bot); return 0; } -- cgit