Skip to content

Commit

Permalink
Serve viewport info (start, end, dimensions) rather than linemap
Browse files Browse the repository at this point in the history
Indexing the viewport lines has a relatively high complexity
versus simply comparing line lengths to viewport width.
  • Loading branch information
dther committed Apr 24, 2024
1 parent c18dd35 commit 32e8269
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions vis-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,24 +531,6 @@ static void pushrange(lua_State *L, Filerange *r) {
lua_settable(L, -3);
}

static void pushlinemap(lua_State *L, Win *win) {
if (!win || !win->view) {
lua_pushnil(L);
return;
}
View *view = win->view;
Line *line = view_lines_first(view);
size_t lines = view_height_get(view);
lua_createtable(L, lines, 0);
for (lua_Integer l = 1; l <= lines; l++) {
if (!line)
break;
lua_pushunsigned(L, line->lineno);
lua_seti(L, -2, l);
line = line->next;
}
}

static Filerange getrange(lua_State *L, int index) {
Filerange range = text_range_empty();
if (lua_istable(L, index)) {
Expand Down Expand Up @@ -1852,8 +1834,16 @@ static const struct luaL_Reg registers_funcs[] = {
* @tfield Range viewport
*/
/***
* A list mapping lines in the viewport to logical lines in the file.
* @tfield list linemap
* The logical line numbers of the first and last visible lines.
* @tfield Range view_lines
*/
/***
* Viewport height, accounting for window decorations.
* @tfield int view_height
*/
/***
* Viewport width, accounting for window decorations.
* @tfield int view_width
*/
/***
* The window width.
Expand Down Expand Up @@ -1894,8 +1884,21 @@ static int window_index(lua_State *L) {
return 1;
}

if (strcmp(key, "linemap") == 0) {
pushlinemap(L, win);
if (strcmp(key, "view_lines") == 0) {
Filerange r;
r.start = view_lines_first(win->view)->lineno;
r.end = view_lines_last(win->view)->lineno;
pushrange(L, &r);
return 1;
}

if (strcmp(key, "view_width") == 0) {
lua_pushunsigned(L, view_width_get(win->view));
return 1;
}

if (strcmp(key, "view_height") == 0) {
lua_pushunsigned(L, view_height_get(win->view));
return 1;
}

Expand Down

0 comments on commit 32e8269

Please sign in to comment.