refactor(drawline): more win_line() improvements

`win_line()` previously used a conspicuous `ptrdiff_t v` variable in many different
places for different reasons. The change encapsulates those uses and
introduces local variables in each with a reduced scope.

Also add `const` to some fields in winlinevars_T.
This commit is contained in:
Lewis Russell 2024-01-18 13:49:47 +00:00
parent ab3a7fc3e3
commit 4706b9c2e9

View File

@ -61,10 +61,10 @@
/// structure with variables passed between win_line() and other functions /// structure with variables passed between win_line() and other functions
typedef struct { typedef struct {
linenr_T lnum; ///< line number to be drawn const linenr_T lnum; ///< line number to be drawn
foldinfo_T foldinfo; ///< fold info for this line const foldinfo_T foldinfo; ///< fold info for this line
int startrow; ///< first row in the window to be drawn const int startrow; ///< first row in the window to be drawn
int row; ///< row in the window, excl w_winrow int row; ///< row in the window, excl w_winrow
colnr_T vcol; ///< virtual column, before wrapping colnr_T vcol; ///< virtual column, before wrapping
@ -918,8 +918,6 @@ static void fix_for_boguscols(winlinevars_T *wlv)
int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, spellvars_T *spv, int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, spellvars_T *spv,
foldinfo_T foldinfo) foldinfo_T foldinfo)
{ {
winlinevars_T wlv; // variables passed between functions
colnr_T vcol_prev = -1; // "wlv.vcol" of previous character colnr_T vcol_prev = -1; // "wlv.vcol" of previous character
char *line; // current line char *line; // current line
char *ptr; // current position in "line" char *ptr; // current position in "line"
@ -937,7 +935,6 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
bool noinvcur = false; // don't invert the cursor bool noinvcur = false; // don't invert the cursor
bool lnum_in_visual_area = false; bool lnum_in_visual_area = false;
pos_T pos; pos_T pos;
ptrdiff_t v;
bool attr_pri = false; // char_attr has priority bool attr_pri = false; // char_attr has priority
bool area_highlighting = false; // Visual or incsearch highlighting in this line bool area_highlighting = false; // Visual or incsearch highlighting in this line
@ -1005,16 +1002,17 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
assert(startrow < endrow); assert(startrow < endrow);
CLEAR_FIELD(wlv); // variables passed between functions
winlinevars_T wlv = {
wlv.lnum = lnum; .lnum = lnum,
wlv.foldinfo = foldinfo; .foldinfo = foldinfo,
wlv.startrow = startrow; .startrow = startrow,
wlv.row = startrow; .row = startrow,
wlv.fromcol = -10; .fromcol = -10,
wlv.tocol = MAXCOL; .tocol = MAXCOL,
wlv.vcol_sbr = -1; .vcol_sbr = -1,
wlv.old_boguscols = 0; .old_boguscols = 0,
};
buf_T *buf = wp->w_buffer; buf_T *buf = wp->w_buffer;
const bool end_fill = (lnum == buf->b_ml.ml_line_count + 1); const bool end_fill = (lnum == buf->b_ml.ml_line_count + 1);
@ -1274,17 +1272,17 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
nextlinecol = MAXCOL; nextlinecol = MAXCOL;
nextline_idx = 0; nextline_idx = 0;
} else { } else {
v = (ptrdiff_t)strlen(line); const size_t line_len = strlen(line);
if (v < SPWORDLEN) { if (line_len < SPWORDLEN) {
// Short line, use it completely and append the start of the // Short line, use it completely and append the start of the
// next line. // next line.
nextlinecol = 0; nextlinecol = 0;
memmove(nextline, line, (size_t)v); memmove(nextline, line, line_len);
STRMOVE(nextline + v, nextline + SPWORDLEN); STRMOVE(nextline + line_len, nextline + SPWORDLEN);
nextline_idx = (int)v + 1; nextline_idx = (int)line_len + 1;
} else { } else {
// Long line, use only the last SPWORDLEN bytes. // Long line, use only the last SPWORDLEN bytes.
nextlinecol = (int)v - SPWORDLEN; nextlinecol = (int)line_len - SPWORDLEN;
memmove(nextline, line + nextlinecol, SPWORDLEN); memmove(nextline, line + nextlinecol, SPWORDLEN);
nextline_idx = SPWORDLEN + 1; nextline_idx = SPWORDLEN + 1;
} }
@ -1316,20 +1314,19 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
// 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the // 'nowrap' or 'wrap' and a single line that doesn't fit: Advance to the
// first character to be displayed. // first character to be displayed.
if (wp->w_p_wrap) { const int start_col = wp->w_p_wrap
v = startrow == 0 ? wp->w_skipcol : 0; ? (startrow == 0 ? wp->w_skipcol : 0)
} else { : wp->w_leftcol;
v = wp->w_leftcol;
} if (start_col > 0 && col_rows == 0) {
if (v > 0 && col_rows == 0) {
char *prev_ptr = ptr; char *prev_ptr = ptr;
chartabsize_T cts; chartabsize_T cts;
int charsize = 0; int charsize = 0;
int head = 0; int head = 0;
init_chartabsize_arg(&cts, wp, lnum, wlv.vcol, line, ptr); init_chartabsize_arg(&cts, wp, lnum, wlv.vcol, line, ptr);
cts.cts_max_head_vcol = (int)v; cts.cts_max_head_vcol = start_col;
while (cts.cts_vcol < v && *cts.cts_ptr != NUL) { while (cts.cts_vcol < start_col && *cts.cts_ptr != NUL) {
head = 0; head = 0;
charsize = win_lbr_chartabsize(&cts, &head); charsize = win_lbr_chartabsize(&cts, &head);
cts.cts_vcol += charsize; cts.cts_vcol += charsize;
@ -1365,22 +1362,22 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
// - 'virtualedit' is set, or // - 'virtualedit' is set, or
// - the visual mode is active, // - the visual mode is active,
// the end of the line may be before the start of the displayed part. // the end of the line may be before the start of the displayed part.
if (wlv.vcol < v && (wp->w_p_cuc if (wlv.vcol < start_col && (wp->w_p_cuc
|| wlv.color_cols || wlv.color_cols
|| virtual_active() || virtual_active()
|| (VIsual_active && wp->w_buffer == curwin->w_buffer))) { || (VIsual_active && wp->w_buffer == curwin->w_buffer))) {
wlv.vcol = (colnr_T)v; wlv.vcol = start_col;
} }
// Handle a character that's not completely on the screen: Put ptr at // Handle a character that's not completely on the screen: Put ptr at
// that character but skip the first few screen characters. // that character but skip the first few screen characters.
if (wlv.vcol > v) { if (wlv.vcol > start_col) {
wlv.vcol -= charsize; wlv.vcol -= charsize;
ptr = prev_ptr; ptr = prev_ptr;
} }
if (v > wlv.vcol) { if (start_col > wlv.vcol) {
wlv.skip_cells = (int)v - wlv.vcol - head; wlv.skip_cells = start_col - wlv.vcol - head;
} }
// Adjust for when the inverted text is before the screen, // Adjust for when the inverted text is before the screen,
@ -1455,8 +1452,8 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
} }
if (col_rows == 0 && !has_fold && !end_fill) { if (col_rows == 0 && !has_fold && !end_fill) {
v = ptr - line; const int v = (int)(ptr - line);
area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v, area_highlighting |= prepare_search_hl_line(wp, lnum, v,
&line, &screen_search_hl, &search_attr, &line, &screen_search_hl, &search_attr,
&search_attr_from_match); &search_attr_from_match);
ptr = line + v; // "line" may have been updated ptr = line + v; // "line" may have been updated
@ -1513,7 +1510,7 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
if (sign_num_attr == 0) { if (sign_num_attr == 0) {
statuscol.num_attr = get_line_number_attr(wp, &wlv); statuscol.num_attr = get_line_number_attr(wp, &wlv);
} }
v = ptr - line; const int v = (int)(ptr - line);
draw_statuscol(wp, &wlv, lnum, wlv.row - startrow - wlv.filler_lines, &statuscol); draw_statuscol(wp, &wlv, lnum, wlv.row - startrow - wlv.filler_lines, &statuscol);
if (wp->w_redr_statuscol) { if (wp->w_redr_statuscol) {
break; break;
@ -1670,8 +1667,8 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
// Check for start/end of 'hlsearch' and other matches. // Check for start/end of 'hlsearch' and other matches.
// After end, check for start/end of next match. // After end, check for start/end of next match.
// When another match, have to check for start again. // When another match, have to check for start again.
v = ptr - line; const int v = (int)(ptr - line);
search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line, &screen_search_hl, search_attr = update_search_hl(wp, lnum, v, &line, &screen_search_hl,
&has_match_conc, &match_conc, lcs_eol_todo, &has_match_conc, &match_conc, lcs_eol_todo,
&on_last_col, &search_attr_from_match); &on_last_col, &search_attr_from_match);
ptr = line + v; // "line" may have been changed ptr = line + v; // "line" may have been changed
@ -1734,7 +1731,7 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
} }
if (draw_folded && wlv.n_extra == 0 && wlv.col == win_col_offset) { if (draw_folded && wlv.n_extra == 0 && wlv.col == win_col_offset) {
v = ptr - line; const int v = (int)(ptr - line);
linenr_T lnume = lnum + foldinfo.fi_lines - 1; linenr_T lnume = lnum + foldinfo.fi_lines - 1;
memset(buf_fold, ' ', FOLD_TEXT_LEN); memset(buf_fold, ' ', FOLD_TEXT_LEN);
wlv.p_extra = get_foldtext(wp, lnum, lnume, foldinfo, buf_fold, &fold_vt); wlv.p_extra = get_foldtext(wp, lnum, lnume, foldinfo, buf_fold, &fold_vt);
@ -1922,7 +1919,7 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
// Get extmark and syntax attributes, unless still at the start of the line // Get extmark and syntax attributes, unless still at the start of the line
// (double-wide char that doesn't fit). // (double-wide char that doesn't fit).
v = ptr - line; const int v = (int)(ptr - line);
const ptrdiff_t prev_v = prev_ptr - line; const ptrdiff_t prev_v = prev_ptr - line;
if (has_syntax && v > 0) { if (has_syntax && v > 0) {
// Get the syntax attribute for the character. If there // Get the syntax attribute for the character. If there
@ -1930,8 +1927,7 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
save_did_emsg = did_emsg; save_did_emsg = did_emsg;
did_emsg = false; did_emsg = false;
decor_attr = get_syntax_attr((colnr_T)v - 1, decor_attr = get_syntax_attr(v - 1, spv->spv_has_spell ? &can_spell : NULL, false);
spv->spv_has_spell ? &can_spell : NULL, false);
if (did_emsg) { if (did_emsg) {
wp->w_s->b_syn_error = true; wp->w_s->b_syn_error = true;
@ -1982,15 +1978,15 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
// Only do this when there is no syntax highlighting, the // Only do this when there is no syntax highlighting, the
// @Spell cluster is not used or the current syntax item // @Spell cluster is not used or the current syntax item
// contains the @Spell cluster. // contains the @Spell cluster.
v = ptr - line; int v1 = (int)(ptr - line);
if (spv->spv_has_spell && v >= word_end && v > cur_checked_col) { if (spv->spv_has_spell && v1 >= word_end && v1 > cur_checked_col) {
spell_attr = 0; spell_attr = 0;
// do not calculate cap_col at the end of the line or when // do not calculate cap_col at the end of the line or when
// only white space is following // only white space is following
if (mb_schar != 0 && (*skipwhite(prev_ptr) != NUL) && can_spell) { if (mb_schar != 0 && (*skipwhite(prev_ptr) != NUL) && can_spell) {
char *p; char *p;
hlf_T spell_hlf = HLF_COUNT; hlf_T spell_hlf = HLF_COUNT;
v -= mb_l - 1; v1 -= mb_l - 1;
// Use nextline[] if possible, it has the start of the // Use nextline[] if possible, it has the start of the
// next line concatenated. // next line concatenated.
@ -2003,7 +1999,7 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
size_t tmplen = spell_check(wp, p, &spell_hlf, &spv->spv_cap_col, spv->spv_unchanged); size_t tmplen = spell_check(wp, p, &spell_hlf, &spv->spv_cap_col, spv->spv_unchanged);
assert(tmplen <= INT_MAX); assert(tmplen <= INT_MAX);
int len = (int)tmplen; int len = (int)tmplen;
word_end = (int)v + len; word_end = v1 + len;
// In Insert mode only highlight a word that // In Insert mode only highlight a word that
// doesn't touch the cursor. // doesn't touch the cursor.
@ -2503,15 +2499,10 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
// At end of the text line. // At end of the text line.
if (mb_schar == NUL) { if (mb_schar == NUL) {
// Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. // Highlight 'cursorcolumn' & 'colorcolumn' past end of the line.
if (wp->w_p_wrap) {
v = wlv.startrow == 0 ? wp->w_skipcol : 0;
} else {
v = wp->w_leftcol;
}
// check if line ends before left margin // check if line ends before left margin
if (wlv.vcol < v + wlv.col - win_col_off(wp)) { if (wlv.vcol < start_col + wlv.col - win_col_off(wp)) {
wlv.vcol = (colnr_T)v + wlv.col - win_col_off(wp); wlv.vcol = start_col + wlv.col - win_col_off(wp);
} }
// Get rid of the boguscols now, we want to draw until the right // Get rid of the boguscols now, we want to draw until the right
// edge for 'cursorcolumn'. // edge for 'cursorcolumn'.
@ -2531,7 +2522,7 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s
if (((wp->w_p_cuc if (((wp->w_p_cuc
&& wp->w_virtcol >= vcol_hlc(wlv) - eol_hl_off && wp->w_virtcol >= vcol_hlc(wlv) - eol_hl_off
&& wp->w_virtcol < grid->cols * (ptrdiff_t)(wlv.row - startrow + 1) + v && wp->w_virtcol < grid->cols * (ptrdiff_t)(wlv.row - startrow + 1) + start_col
&& lnum != wp->w_cursor.lnum) && lnum != wp->w_cursor.lnum)
|| wlv.color_cols || wlv.line_attr_lowprio || wlv.line_attr || wlv.color_cols || wlv.line_attr_lowprio || wlv.line_attr
|| wlv.diff_hlf != 0 || has_virttext)) { || wlv.diff_hlf != 0 || has_virttext)) {