mirror of
https://github.com/neovim/neovim.git
synced 2024-12-23 20:55:18 -07:00
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:
parent
ab3a7fc3e3
commit
4706b9c2e9
@ -61,10 +61,10 @@
|
||||
|
||||
/// structure with variables passed between win_line() and other functions
|
||||
typedef struct {
|
||||
linenr_T lnum; ///< line number to be drawn
|
||||
foldinfo_T foldinfo; ///< fold info for this line
|
||||
const linenr_T lnum; ///< line number to be drawn
|
||||
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
|
||||
|
||||
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,
|
||||
foldinfo_T foldinfo)
|
||||
{
|
||||
winlinevars_T wlv; // variables passed between functions
|
||||
|
||||
colnr_T vcol_prev = -1; // "wlv.vcol" of previous character
|
||||
char *line; // current 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 lnum_in_visual_area = false;
|
||||
pos_T pos;
|
||||
ptrdiff_t v;
|
||||
|
||||
bool attr_pri = false; // char_attr has priority
|
||||
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);
|
||||
|
||||
CLEAR_FIELD(wlv);
|
||||
|
||||
wlv.lnum = lnum;
|
||||
wlv.foldinfo = foldinfo;
|
||||
wlv.startrow = startrow;
|
||||
wlv.row = startrow;
|
||||
wlv.fromcol = -10;
|
||||
wlv.tocol = MAXCOL;
|
||||
wlv.vcol_sbr = -1;
|
||||
wlv.old_boguscols = 0;
|
||||
// variables passed between functions
|
||||
winlinevars_T wlv = {
|
||||
.lnum = lnum,
|
||||
.foldinfo = foldinfo,
|
||||
.startrow = startrow,
|
||||
.row = startrow,
|
||||
.fromcol = -10,
|
||||
.tocol = MAXCOL,
|
||||
.vcol_sbr = -1,
|
||||
.old_boguscols = 0,
|
||||
};
|
||||
|
||||
buf_T *buf = wp->w_buffer;
|
||||
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;
|
||||
nextline_idx = 0;
|
||||
} else {
|
||||
v = (ptrdiff_t)strlen(line);
|
||||
if (v < SPWORDLEN) {
|
||||
const size_t line_len = strlen(line);
|
||||
if (line_len < SPWORDLEN) {
|
||||
// Short line, use it completely and append the start of the
|
||||
// next line.
|
||||
nextlinecol = 0;
|
||||
memmove(nextline, line, (size_t)v);
|
||||
STRMOVE(nextline + v, nextline + SPWORDLEN);
|
||||
nextline_idx = (int)v + 1;
|
||||
memmove(nextline, line, line_len);
|
||||
STRMOVE(nextline + line_len, nextline + SPWORDLEN);
|
||||
nextline_idx = (int)line_len + 1;
|
||||
} else {
|
||||
// Long line, use only the last SPWORDLEN bytes.
|
||||
nextlinecol = (int)v - SPWORDLEN;
|
||||
nextlinecol = (int)line_len - SPWORDLEN;
|
||||
memmove(nextline, line + nextlinecol, SPWORDLEN);
|
||||
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
|
||||
// first character to be displayed.
|
||||
if (wp->w_p_wrap) {
|
||||
v = startrow == 0 ? wp->w_skipcol : 0;
|
||||
} else {
|
||||
v = wp->w_leftcol;
|
||||
}
|
||||
if (v > 0 && col_rows == 0) {
|
||||
const int start_col = wp->w_p_wrap
|
||||
? (startrow == 0 ? wp->w_skipcol : 0)
|
||||
: wp->w_leftcol;
|
||||
|
||||
if (start_col > 0 && col_rows == 0) {
|
||||
char *prev_ptr = ptr;
|
||||
chartabsize_T cts;
|
||||
int charsize = 0;
|
||||
int head = 0;
|
||||
|
||||
init_chartabsize_arg(&cts, wp, lnum, wlv.vcol, line, ptr);
|
||||
cts.cts_max_head_vcol = (int)v;
|
||||
while (cts.cts_vcol < v && *cts.cts_ptr != NUL) {
|
||||
cts.cts_max_head_vcol = start_col;
|
||||
while (cts.cts_vcol < start_col && *cts.cts_ptr != NUL) {
|
||||
head = 0;
|
||||
charsize = win_lbr_chartabsize(&cts, &head);
|
||||
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
|
||||
// - the visual mode is active,
|
||||
// the end of the line may be before the start of the displayed part.
|
||||
if (wlv.vcol < v && (wp->w_p_cuc
|
||||
|| wlv.color_cols
|
||||
|| virtual_active()
|
||||
|| (VIsual_active && wp->w_buffer == curwin->w_buffer))) {
|
||||
wlv.vcol = (colnr_T)v;
|
||||
if (wlv.vcol < start_col && (wp->w_p_cuc
|
||||
|| wlv.color_cols
|
||||
|| virtual_active()
|
||||
|| (VIsual_active && wp->w_buffer == curwin->w_buffer))) {
|
||||
wlv.vcol = start_col;
|
||||
}
|
||||
|
||||
// Handle a character that's not completely on the screen: Put ptr at
|
||||
// that character but skip the first few screen characters.
|
||||
if (wlv.vcol > v) {
|
||||
if (wlv.vcol > start_col) {
|
||||
wlv.vcol -= charsize;
|
||||
ptr = prev_ptr;
|
||||
}
|
||||
|
||||
if (v > wlv.vcol) {
|
||||
wlv.skip_cells = (int)v - wlv.vcol - head;
|
||||
if (start_col > wlv.vcol) {
|
||||
wlv.skip_cells = start_col - wlv.vcol - head;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
v = ptr - line;
|
||||
area_highlighting |= prepare_search_hl_line(wp, lnum, (colnr_T)v,
|
||||
const int v = (int)(ptr - line);
|
||||
area_highlighting |= prepare_search_hl_line(wp, lnum, v,
|
||||
&line, &screen_search_hl, &search_attr,
|
||||
&search_attr_from_match);
|
||||
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) {
|
||||
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);
|
||||
if (wp->w_redr_statuscol) {
|
||||
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.
|
||||
// After end, check for start/end of next match.
|
||||
// When another match, have to check for start again.
|
||||
v = ptr - line;
|
||||
search_attr = update_search_hl(wp, lnum, (colnr_T)v, &line, &screen_search_hl,
|
||||
const int v = (int)(ptr - line);
|
||||
search_attr = update_search_hl(wp, lnum, v, &line, &screen_search_hl,
|
||||
&has_match_conc, &match_conc, lcs_eol_todo,
|
||||
&on_last_col, &search_attr_from_match);
|
||||
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) {
|
||||
v = ptr - line;
|
||||
const int v = (int)(ptr - line);
|
||||
linenr_T lnume = lnum + foldinfo.fi_lines - 1;
|
||||
memset(buf_fold, ' ', FOLD_TEXT_LEN);
|
||||
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
|
||||
// (double-wide char that doesn't fit).
|
||||
v = ptr - line;
|
||||
const int v = (int)(ptr - line);
|
||||
const ptrdiff_t prev_v = prev_ptr - line;
|
||||
if (has_syntax && v > 0) {
|
||||
// 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;
|
||||
did_emsg = false;
|
||||
|
||||
decor_attr = get_syntax_attr((colnr_T)v - 1,
|
||||
spv->spv_has_spell ? &can_spell : NULL, false);
|
||||
decor_attr = get_syntax_attr(v - 1, spv->spv_has_spell ? &can_spell : NULL, false);
|
||||
|
||||
if (did_emsg) {
|
||||
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
|
||||
// @Spell cluster is not used or the current syntax item
|
||||
// contains the @Spell cluster.
|
||||
v = ptr - line;
|
||||
if (spv->spv_has_spell && v >= word_end && v > cur_checked_col) {
|
||||
int v1 = (int)(ptr - line);
|
||||
if (spv->spv_has_spell && v1 >= word_end && v1 > cur_checked_col) {
|
||||
spell_attr = 0;
|
||||
// do not calculate cap_col at the end of the line or when
|
||||
// only white space is following
|
||||
if (mb_schar != 0 && (*skipwhite(prev_ptr) != NUL) && can_spell) {
|
||||
char *p;
|
||||
hlf_T spell_hlf = HLF_COUNT;
|
||||
v -= mb_l - 1;
|
||||
v1 -= mb_l - 1;
|
||||
|
||||
// Use nextline[] if possible, it has the start of the
|
||||
// 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);
|
||||
assert(tmplen <= INT_MAX);
|
||||
int len = (int)tmplen;
|
||||
word_end = (int)v + len;
|
||||
word_end = v1 + len;
|
||||
|
||||
// In Insert mode only highlight a word that
|
||||
// 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.
|
||||
if (mb_schar == NUL) {
|
||||
// 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
|
||||
if (wlv.vcol < v + wlv.col - win_col_off(wp)) {
|
||||
wlv.vcol = (colnr_T)v + wlv.col - win_col_off(wp);
|
||||
if (wlv.vcol < start_col + 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
|
||||
// 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
|
||||
&& 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)
|
||||
|| wlv.color_cols || wlv.line_attr_lowprio || wlv.line_attr
|
||||
|| wlv.diff_hlf != 0 || has_virttext)) {
|
||||
|
Loading…
Reference in New Issue
Block a user