refactor(signs): handle non-sign attrs separately (#19784)

This commit is contained in:
Lewis Russell
2022-08-16 11:03:44 +01:00
committed by GitHub
parent 4f0a0a2933
commit 9a4b8dc603
5 changed files with 149 additions and 136 deletions

View File

@@ -358,7 +358,8 @@ next_mark:
return attr; return attr;
} }
void decor_redraw_signs(buf_T *buf, int row, int *num_signs, sign_attrs_T sattrs[]) void decor_redraw_signs(buf_T *buf, int row, int *num_signs, SignTextAttrs sattrs[],
HlPriAttr *num_attrs, HlPriAttr *line_attrs, HlPriAttr *cul_attrs)
{ {
if (!buf->b_signs) { if (!buf->b_signs) {
return; return;
@@ -383,31 +384,38 @@ void decor_redraw_signs(buf_T *buf, int row, int *num_signs, sign_attrs_T sattrs
goto next_mark; goto next_mark;
} }
if (decor->sign_text) {
int j; int j;
for (j = (*num_signs); j > 0; j--) { for (j = (*num_signs); j > 0; j--) {
if (sattrs[j - 1].sat_prio >= decor->priority) { if (sattrs[j - 1].priority >= decor->priority) {
break; break;
} }
sattrs[j] = sattrs[j - 1]; sattrs[j] = sattrs[j - 1];
} }
if (j < SIGN_SHOW_MAX) { if (j < SIGN_SHOW_MAX) {
CLEAR_FIELD(sattrs[j]); sattrs[j] = (SignTextAttrs) {
sattrs[j].sat_text = decor->sign_text; .text = decor->sign_text,
if (decor->sign_hl_id != 0) { .hl_attr_id = decor->sign_hl_id == 0 ? 0 : syn_id2attr(decor->sign_hl_id),
sattrs[j].sat_texthl = syn_id2attr(decor->sign_hl_id); .priority = decor->priority
} };
if (decor->number_hl_id != 0) {
sattrs[j].sat_numhl = syn_id2attr(decor->number_hl_id);
}
if (decor->line_hl_id != 0) {
sattrs[j].sat_linehl = syn_id2attr(decor->line_hl_id);
}
if (decor->cursorline_hl_id != 0) {
sattrs[j].sat_culhl = syn_id2attr(decor->cursorline_hl_id);
}
sattrs[j].sat_prio = decor->priority;
(*num_signs)++; (*num_signs)++;
} }
}
struct { HlPriAttr *dest; int hl; } cattrs[] = {
{ line_attrs, decor->line_hl_id },
{ num_attrs, decor->number_hl_id },
{ cul_attrs, decor->cursorline_hl_id },
{ NULL, -1 },
};
for (int i = 0; cattrs[i].dest; i++) {
if (cattrs[i].hl != 0 && decor->priority >= cattrs[i].dest->priority) {
*cattrs[i].dest = (HlPriAttr) {
.attr_id = syn_id2attr(cattrs[i].hl),
.priority = decor->priority
};
}
}
next_mark: next_mark:
marktree_itr_next(buf->b_marktree, itr); marktree_itr_next(buf->b_marktree, itr);

View File

@@ -223,4 +223,10 @@ typedef struct {
#define COLOR_ITEM_INITIALIZER { .attr_id = -1, .link_id = -1, \ #define COLOR_ITEM_INITIALIZER { .attr_id = -1, .link_id = -1, \
.version = -1, .is_default = false } .version = -1, .is_default = false }
/// highlight attributes with associated priorities
typedef struct {
int attr_id;
int priority;
} HlPriAttr;
#endif // NVIM_HIGHLIGHT_DEFS_H #endif // NVIM_HIGHLIGHT_DEFS_H

View File

@@ -1943,6 +1943,44 @@ static inline void get_line_number_str(win_T *wp, linenr_T lnum, char_u *buf, si
snprintf((char *)buf, buf_len, fmt, number_width(wp), num); snprintf((char *)buf, buf_len, fmt, number_width(wp), num);
} }
static void apply_cursorline_highlight(win_T *wp, linenr_T lnum, int *line_attr, int *cul_attr,
int *line_attr_lowprio)
{
*cul_attr = win_hl_attr(wp, HLF_CUL);
HlAttrs ae = syn_attr2entry(*cul_attr);
// We make a compromise here (#7383):
// * low-priority CursorLine if fg is not set
// * high-priority ("same as Vim" priority) CursorLine if fg is set
if (ae.rgb_fg_color == -1 && ae.cterm_fg_color == 0) {
*line_attr_lowprio = *cul_attr;
} else {
if (!(State & MODE_INSERT) && bt_quickfix(wp->w_buffer)
&& qf_current_entry(wp) == lnum) {
*line_attr = hl_combine_attr(*cul_attr, *line_attr);
} else {
*line_attr = *cul_attr;
}
}
}
static int get_sign_attrs(buf_T *buf, linenr_T lnum, SignTextAttrs *sattrs, int *line_attr,
int *num_attr, int *cul_attr)
{
HlPriAttr line_attrs = { *line_attr, 0 };
HlPriAttr num_attrs = { *num_attr, 0 };
HlPriAttr cul_attrs = { *cul_attr, 0 };
// TODO(bfredl, vigoux): line_attr should not take priority over decoration!
int num_signs = buf_get_signattrs(buf, lnum, sattrs, &num_attrs, &line_attrs, &cul_attrs);
decor_redraw_signs(buf, lnum - 1, &num_signs, sattrs, &num_attrs, &line_attrs, &cul_attrs);
*line_attr = line_attrs.attr_id;
*num_attr = num_attrs.attr_id;
*cul_attr = cul_attrs.attr_id;
return num_signs;
}
/// Display line "lnum" of window 'wp' on the screen. /// Display line "lnum" of window 'wp' on the screen.
/// wp->w_virtcol needs to be valid. /// wp->w_virtcol needs to be valid.
/// ///
@@ -2049,8 +2087,6 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool noc
bool in_multispace = false; // in multiple consecutive spaces bool in_multispace = false; // in multiple consecutive spaces
int multispace_pos = 0; // position in lcs-multispace string int multispace_pos = 0; // position in lcs-multispace string
bool need_showbreak = false; // overlong line, skip first x chars bool need_showbreak = false; // overlong line, skip first x chars
sign_attrs_T sattrs[SIGN_SHOW_MAX]; // attributes for signs
int num_signs; // number of signs for line
int line_attr = 0; // attribute for the whole line int line_attr = 0; // attribute for the whole line
int line_attr_save; int line_attr_save;
int line_attr_lowprio = 0; // low-priority attribute for the line int line_attr_lowprio = 0; // low-priority attribute for the line
@@ -2320,21 +2356,7 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool noc
cul_screenline = (wp->w_p_wrap cul_screenline = (wp->w_p_wrap
&& (wp->w_p_culopt_flags & CULOPT_SCRLINE)); && (wp->w_p_culopt_flags & CULOPT_SCRLINE));
if (!cul_screenline) { if (!cul_screenline) {
cul_attr = win_hl_attr(wp, HLF_CUL); apply_cursorline_highlight(wp, lnum, &line_attr, &cul_attr, &line_attr_lowprio);
HlAttrs ae = syn_attr2entry(cul_attr);
// We make a compromise here (#7383):
// * low-priority CursorLine if fg is not set
// * high-priority ("same as Vim" priority) CursorLine if fg is set
if (ae.rgb_fg_color == -1 && ae.cterm_fg_color == 0) {
line_attr_lowprio = cul_attr;
} else {
if (!(State & MODE_INSERT) && bt_quickfix(wp->w_buffer)
&& qf_current_entry(wp) == lnum) {
line_attr = hl_combine_attr(cul_attr, line_attr);
} else {
line_attr = cul_attr;
}
}
} else { } else {
margin_columns_win(wp, &left_curline_col, &right_curline_col); margin_columns_win(wp, &left_curline_col, &right_curline_col);
} }
@@ -2342,16 +2364,11 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool noc
} }
} }
SignTextAttrs sattrs[SIGN_SHOW_MAX]; // sign attributes for the sign column
int sign_num_attr = 0; // sign attribute for the number column
int sign_cul_attr = 0; // sign attribute for cursorline
CLEAR_FIELD(sattrs); CLEAR_FIELD(sattrs);
num_signs = buf_get_signattrs(wp->w_buffer, lnum, sattrs); int num_signs = get_sign_attrs(buf, lnum, sattrs, &line_attr, &sign_num_attr, &sign_cul_attr);
decor_redraw_signs(buf, lnum - 1, &num_signs, sattrs);
// If this line has a sign with line highlighting set line_attr.
// TODO(bfredl, vigoux): this should not take priority over decoration!
sign_attrs_T *sattr = sign_get_attr(SIGN_LINEHL, sattrs, 0, 1);
if (sattr != NULL) {
line_attr = sattr->sat_linehl;
}
// Highlight the current line in the quickfix window. // Highlight the current line in the quickfix window.
if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum) { if (bt_quickfix(wp->w_buffer) && qf_current_entry(wp) == lnum) {
@@ -2623,13 +2640,13 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool noc
// sign column, this is hit until sign_idx reaches count // sign column, this is hit until sign_idx reaches count
if (draw_state == WL_SIGN - 1 && n_extra == 0) { if (draw_state == WL_SIGN - 1 && n_extra == 0) {
draw_state = WL_SIGN; draw_state = WL_SIGN;
// Show the sign column when there are any signs in this // Show the sign column when there are any signs in this buffer
// buffer or when using Netbeans.
if (wp->w_scwidth > 0) { if (wp->w_scwidth > 0) {
get_sign_display_info(false, wp, lnum, sattrs, row, get_sign_display_info(false, wp, lnum, sattrs, row,
startrow, filler_lines, filler_todo, startrow, filler_lines, filler_todo,
&c_extra, &c_final, extra, sizeof(extra), &c_extra, &c_final, extra, sizeof(extra),
&p_extra, &n_extra, &char_attr, sign_idx); &p_extra, &n_extra, &char_attr, sign_idx,
sign_cul_attr);
sign_idx++; sign_idx++;
if (sign_idx < wp->w_scwidth) { if (sign_idx < wp->w_scwidth) {
draw_state = WL_SIGN - 1; draw_state = WL_SIGN - 1;
@@ -2649,12 +2666,12 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool noc
// If 'signcolumn' is set to 'number' and a sign is present // If 'signcolumn' is set to 'number' and a sign is present
// in 'lnum', then display the sign instead of the line // in 'lnum', then display the sign instead of the line
// number. // number.
if (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u' if (*wp->w_p_scl == 'n' && *(wp->w_p_scl + 1) == 'u' && num_signs > 0) {
&& num_signs > 0 && sign_get_attr(SIGN_TEXT, sattrs, 0, 1)) {
get_sign_display_info(true, wp, lnum, sattrs, row, get_sign_display_info(true, wp, lnum, sattrs, row,
startrow, filler_lines, filler_todo, startrow, filler_lines, filler_todo,
&c_extra, &c_final, extra, sizeof(extra), &c_extra, &c_final, extra, sizeof(extra),
&p_extra, &n_extra, &char_attr, sign_idx); &p_extra, &n_extra, &char_attr, sign_idx,
sign_cul_attr);
} else { } else {
// Draw the line number (empty space after wrapping). // Draw the line number (empty space after wrapping).
if (row == startrow + filler_lines) { if (row == startrow + filler_lines) {
@@ -2681,7 +2698,11 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool noc
} }
c_final = NUL; c_final = NUL;
n_extra = number_width(wp) + 1; n_extra = number_width(wp) + 1;
char_attr = get_line_number_attr(wp, lnum, row, startrow, filler_lines, sattrs); if (sign_num_attr > 0) {
char_attr = sign_num_attr;
} else {
char_attr = get_line_number_attr(wp, lnum, row, startrow, filler_lines);
}
} }
} }
} }
@@ -2810,18 +2831,7 @@ static int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool noc
if (cul_screenline && draw_state == WL_LINE if (cul_screenline && draw_state == WL_LINE
&& vcol >= left_curline_col && vcol >= left_curline_col
&& vcol < right_curline_col) { && vcol < right_curline_col) {
cul_attr = win_hl_attr(wp, HLF_CUL); apply_cursorline_highlight(wp, lnum, &line_attr, &cul_attr, &line_attr_lowprio);
HlAttrs ae = syn_attr2entry(cul_attr);
if (ae.rgb_fg_color == -1 && ae.cterm_fg_color == 0) {
line_attr_lowprio = cul_attr;
} else {
if (!(State & MODE_INSERT) && bt_quickfix(wp->w_buffer)
&& qf_current_entry(wp) == lnum) {
line_attr = hl_combine_attr(cul_attr, line_attr);
} else {
line_attr = cul_attr;
}
}
} }
// When still displaying '$' of change command, stop at cursor // When still displaying '$' of change command, stop at cursor
@@ -4349,15 +4359,8 @@ static bool use_cursor_line_nr(win_T *wp, linenr_T lnum, int row, int startrow,
&& (wp->w_p_culopt_flags & CULOPT_LINE))); && (wp->w_p_culopt_flags & CULOPT_LINE)));
} }
static int get_line_number_attr(win_T *wp, linenr_T lnum, int row, int startrow, int filler_lines, static int get_line_number_attr(win_T *wp, linenr_T lnum, int row, int startrow, int filler_lines)
sign_attrs_T *sattrs)
{ {
sign_attrs_T *num_sattr = sign_get_attr(SIGN_NUMHL, sattrs, 0, 1);
if (num_sattr != NULL) {
// :sign defined with "numhl" highlight.
return num_sattr->sat_numhl;
}
if (wp->w_p_rnu) { if (wp->w_p_rnu) {
if (lnum < wp->w_cursor.lnum) { if (lnum < wp->w_cursor.lnum) {
// Use LineNrAbove // Use LineNrAbove
@@ -4385,10 +4388,11 @@ static int get_line_number_attr(win_T *wp, linenr_T lnum, int row, int startrow,
// @param count max number of signs // @param count max number of signs
// @param[out] n_extrap number of characters from pp_extra to display // @param[out] n_extrap number of characters from pp_extra to display
// @param sign_idxp Index of the displayed sign // @param sign_idxp Index of the displayed sign
static void get_sign_display_info(bool nrcol, win_T *wp, linenr_T lnum, sign_attrs_T sattrs[], static void get_sign_display_info(bool nrcol, win_T *wp, linenr_T lnum, SignTextAttrs sattrs[],
int row, int startrow, int filler_lines, int filler_todo, int row, int startrow, int filler_lines, int filler_todo,
int *c_extrap, int *c_finalp, char_u *extra, size_t extra_size, int *c_extrap, int *c_finalp, char_u *extra, size_t extra_size,
char_u **pp_extra, int *n_extrap, int *char_attrp, int sign_idx) char_u **pp_extra, int *n_extrap, int *char_attrp, int sign_idx,
int cul_attr)
{ {
// Draw cells with the sign value or blank. // Draw cells with the sign value or blank.
*c_extrap = ' '; *c_extrap = ' ';
@@ -4405,9 +4409,9 @@ static void get_sign_display_info(bool nrcol, win_T *wp, linenr_T lnum, sign_att
} }
if (row == startrow + filler_lines && filler_todo <= 0) { if (row == startrow + filler_lines && filler_todo <= 0) {
sign_attrs_T *sattr = sign_get_attr(SIGN_TEXT, sattrs, sign_idx, wp->w_scwidth); SignTextAttrs *sattr = sign_get_attr(sign_idx, sattrs, wp->w_scwidth);
if (sattr != NULL) { if (sattr != NULL) {
*pp_extra = sattr->sat_text; *pp_extra = sattr->text;
if (*pp_extra != NULL) { if (*pp_extra != NULL) {
*c_extrap = NUL; *c_extrap = NUL;
*c_finalp = NUL; *c_finalp = NUL;
@@ -4423,28 +4427,28 @@ static void get_sign_display_info(bool nrcol, win_T *wp, linenr_T lnum, sign_att
*pp_extra = extra; *pp_extra = extra;
*n_extrap = (int)STRLEN(*pp_extra); *n_extrap = (int)STRLEN(*pp_extra);
} else { } else {
int symbol_blen = (int)STRLEN(*pp_extra); size_t symbol_blen = STRLEN(*pp_extra);
// TODO(oni-link): Is sign text already extended to // TODO(oni-link): Is sign text already extended to
// full cell width? // full cell width?
assert((size_t)win_signcol_width(wp) >= mb_string2cells((char *)(*pp_extra))); assert((size_t)win_signcol_width(wp) >= mb_string2cells((char *)(*pp_extra)));
// symbol(s) bytes + (filling spaces) (one byte each) // symbol(s) bytes + (filling spaces) (one byte each)
*n_extrap = symbol_blen + win_signcol_width(wp) - *n_extrap = (int)symbol_blen + win_signcol_width(wp) -
(int)mb_string2cells((char *)(*pp_extra)); (int)mb_string2cells((char *)(*pp_extra));
assert(extra_size > (size_t)symbol_blen); assert(extra_size > symbol_blen);
memset(extra, ' ', extra_size); memset(extra, ' ', extra_size);
memcpy(extra, *pp_extra, (size_t)symbol_blen); memcpy(extra, *pp_extra, symbol_blen);
*pp_extra = extra; *pp_extra = extra;
(*pp_extra)[*n_extrap] = NUL; (*pp_extra)[*n_extrap] = NUL;
} }
} }
if (use_cursor_line_sign(wp, lnum) && sattr->sat_culhl > 0) { if (use_cursor_line_sign(wp, lnum) && cul_attr > 0) {
*char_attrp = sattr->sat_culhl; *char_attrp = cul_attr;
} else { } else {
*char_attrp = sattr->sat_texthl; *char_attrp = sattr->hl_attr_id;
} }
} }
} }

View File

@@ -442,27 +442,24 @@ static linenr_T buf_change_sign_type(buf_T *buf, int markId, const char_u *group
/// @param max_signs the number of signs, with priority for the ones /// @param max_signs the number of signs, with priority for the ones
/// with the highest Ids. /// with the highest Ids.
/// @return Attrs of the matching sign, or NULL /// @return Attrs of the matching sign, or NULL
sign_attrs_T *sign_get_attr(SignType type, sign_attrs_T sattrs[], int idx, int max_signs) SignTextAttrs *sign_get_attr(int idx, SignTextAttrs sattrs[], int max_signs)
{ {
sign_attrs_T *matches[SIGN_SHOW_MAX]; SignTextAttrs *matches[SIGN_SHOW_MAX];
int nr_matches = 0; int sattr_matches = 0;
for (int i = 0; i < SIGN_SHOW_MAX; i++) { for (int i = 0; i < SIGN_SHOW_MAX; i++) {
if ((type == SIGN_TEXT && sattrs[i].sat_text != NULL) if (sattrs[i].text != NULL) {
|| (type == SIGN_LINEHL && sattrs[i].sat_linehl != 0) matches[sattr_matches++] = &sattrs[i];
|| (type == SIGN_NUMHL && sattrs[i].sat_numhl != 0)) {
matches[nr_matches] = &sattrs[i];
nr_matches++;
// attr list is sorted with most important (priority, id), thus we // attr list is sorted with most important (priority, id), thus we
// may stop as soon as we have max_signs matches // may stop as soon as we have max_signs matches
if (nr_matches >= max_signs) { if (sattr_matches >= max_signs) {
break; break;
} }
} }
} }
if (nr_matches > idx) { if (sattr_matches > idx) {
return matches[nr_matches - idx - 1]; return matches[sattr_matches - idx - 1];
} }
return NULL; return NULL;
@@ -474,12 +471,12 @@ sign_attrs_T *sign_get_attr(SignType type, sign_attrs_T sattrs[], int idx, int m
/// @param lnum Line in which to search /// @param lnum Line in which to search
/// @param sattrs Output array for attrs /// @param sattrs Output array for attrs
/// @return Number of signs of which attrs were found /// @return Number of signs of which attrs were found
int buf_get_signattrs(buf_T *buf, linenr_T lnum, sign_attrs_T sattrs[]) int buf_get_signattrs(buf_T *buf, linenr_T lnum, SignTextAttrs sattrs[], HlPriAttr *num_attrs,
HlPriAttr *line_attrs, HlPriAttr *cul_attrs)
{ {
sign_entry_T *sign; sign_entry_T *sign;
sign_T *sp;
int nr_matches = 0; int sattr_matches = 0;
FOR_ALL_SIGNS_IN_BUF(buf, sign) { FOR_ALL_SIGNS_IN_BUF(buf, sign) {
if (sign->se_lnum > lnum) { if (sign->se_lnum > lnum) {
@@ -488,37 +485,39 @@ int buf_get_signattrs(buf_T *buf, linenr_T lnum, sign_attrs_T sattrs[])
break; break;
} }
if (sign->se_lnum == lnum) { if (sign->se_lnum < lnum) {
sign_attrs_T sattr; continue;
CLEAR_FIELD(sattr);
sattr.sat_typenr = sign->se_typenr;
sp = find_sign_by_typenr(sign->se_typenr);
if (sp != NULL) {
sattr.sat_text = sp->sn_text;
if (sattr.sat_text != NULL && sp->sn_text_hl != 0) {
sattr.sat_texthl = syn_id2attr(sp->sn_text_hl);
}
if (sp->sn_line_hl != 0) {
sattr.sat_linehl = syn_id2attr(sp->sn_line_hl);
}
if (sp->sn_cul_hl != 0) {
sattr.sat_culhl = syn_id2attr(sp->sn_cul_hl);
}
if (sp->sn_num_hl != 0) {
sattr.sat_numhl = syn_id2attr(sp->sn_num_hl);
}
// Store the priority so we can mesh in extmark signs later
sattr.sat_prio = sign->se_priority;
} }
sattrs[nr_matches] = sattr; sign_T *sp = find_sign_by_typenr(sign->se_typenr);
nr_matches++; if (sp == NULL) {
if (nr_matches == SIGN_SHOW_MAX) { continue;
break; }
if (sp->sn_text != NULL && sattr_matches < SIGN_SHOW_MAX) {
sattrs[sattr_matches++] = (SignTextAttrs) {
.text = sp->sn_text,
.hl_attr_id = sp->sn_text_hl == 0 ? 0 : syn_id2attr(sp->sn_text_hl),
.priority = sign->se_priority
};
}
struct { HlPriAttr *dest; int hl; } cattrs[] = {
{ line_attrs, sp->sn_line_hl },
{ num_attrs, sp->sn_num_hl },
{ cul_attrs, sp->sn_cul_hl },
{ NULL, -1 },
};
for (int i = 0; cattrs[i].dest; i++) {
if (cattrs[i].hl != 0 && sign->se_priority >= cattrs[i].dest->priority) {
*cattrs[i].dest = (HlPriAttr) {
.attr_id = syn_id2attr(cattrs[i].hl),
.priority = sign->se_priority
};
} }
} }
} }
return nr_matches; return sattr_matches;
} }
/// Delete sign 'id' in group 'group' from buffer 'buf'. /// Delete sign 'id' in group 'group' from buffer 'buf'.

View File

@@ -33,15 +33,11 @@ struct sign_entry {
}; };
/// Sign attributes. Used by the screen refresh routines. /// Sign attributes. Used by the screen refresh routines.
typedef struct sign_attrs_S { typedef struct {
int sat_typenr; char_u *text;
char_u *sat_text; int hl_attr_id;
int sat_texthl; int priority;
int sat_linehl; } SignTextAttrs;
int sat_culhl;
int sat_numhl;
int sat_prio; // Used for inserting extmark signs
} sign_attrs_T;
#define SIGN_SHOW_MAX 9 #define SIGN_SHOW_MAX 9