diff options
author | zeertzjq <zeertzjq@outlook.com> | 2023-03-03 17:37:59 +0800 |
---|---|---|
committer | zeertzjq <zeertzjq@outlook.com> | 2023-03-03 19:09:53 +0800 |
commit | bf445569062b410105bcb65e75c9a0ea88866b5b (patch) | |
tree | 4c3a31286dd7c29698e2247ce96c1d543fe450c0 | |
parent | f0a2ffab2923202f4454860ba1a7c7bd0e035ed2 (diff) | |
download | rneovim-bf445569062b410105bcb65e75c9a0ea88866b5b.tar.gz rneovim-bf445569062b410105bcb65e75c9a0ea88866b5b.tar.bz2 rneovim-bf445569062b410105bcb65e75c9a0ea88866b5b.zip |
refactor(drawline.c): move number column helpers function together
-rw-r--r-- | src/nvim/drawline.c | 123 |
1 files changed, 61 insertions, 62 deletions
diff --git a/src/nvim/drawline.c b/src/nvim/drawline.c index 7ed0c1b3c0..4c1f2f90fa 100644 --- a/src/nvim/drawline.c +++ b/src/nvim/drawline.c @@ -429,6 +429,67 @@ static int get_sign_attrs(buf_T *buf, winlinevars_T *wlv, int *sign_num_attrp, i return num_signs; } +static inline void get_line_number_str(win_T *wp, linenr_T lnum, char *buf, size_t buf_len) +{ + long num; + char *fmt = "%*ld "; + + if (wp->w_p_nu && !wp->w_p_rnu) { + // 'number' + 'norelativenumber' + num = (long)lnum; + } else { + // 'relativenumber', don't use negative numbers + num = labs((long)get_cursor_rel_lnum(wp, lnum)); + if (num == 0 && wp->w_p_nu && wp->w_p_rnu) { + // 'number' + 'relativenumber' + num = lnum; + fmt = "%-*ld "; + } + } + + snprintf(buf, buf_len, fmt, number_width(wp), num); +} + +/// Return true if CursorLineNr highlight is to be used for the number column. +/// - 'cursorline' must be set +/// - "wlv->lnum" must be the cursor line +/// - 'cursorlineopt' has "number" +/// - don't highlight filler lines (when in diff mode) +/// - When line is wrapped and 'cursorlineopt' does not have "line", only highlight the line number +/// itself on the first screenline of the wrapped line, otherwise highlight the number column of +/// all screenlines of the wrapped line. +static bool use_cursor_line_nr(win_T *wp, winlinevars_T *wlv) +{ + return wp->w_p_cul + && wlv->lnum == wp->w_cursorline + && (wp->w_p_culopt_flags & CULOPT_NBR) + && (wlv->row == wlv->startrow + wlv->filler_lines + || (wlv->row > wlv->startrow + wlv->filler_lines + && (wp->w_p_culopt_flags & CULOPT_LINE))); +} + +static int get_line_number_attr(win_T *wp, winlinevars_T *wlv) +{ + if (use_cursor_line_nr(wp, wlv)) { + // TODO(vim): Can we use CursorLine instead of CursorLineNr + // when CursorLineNr isn't set? + return win_hl_attr(wp, HLF_CLN); + } + + if (wp->w_p_rnu) { + if (wlv->lnum < wp->w_cursor.lnum) { + // Use LineNrAbove + return win_hl_attr(wp, HLF_LNA); + } + if (wlv->lnum > wp->w_cursor.lnum) { + // Use LineNrBelow + return win_hl_attr(wp, HLF_LNB); + } + } + + return win_hl_attr(wp, HLF_N); +} + /// Display the absolute or relative line number. After the first row fill with /// blanks when the 'n' flag isn't in 'cpo'. static void handle_lnum_col(win_T *wp, winlinevars_T *wlv, int num_signs, int sign_idx, @@ -554,68 +615,6 @@ static void get_statuscol_display_info(statuscol_T *stcp, winlinevars_T *wlv) } while (wlv->n_extra == 0 && stcp->textp < stcp->text_end); } -/// Return true if CursorLineNr highlight is to be used for the number column. -/// -/// - 'cursorline' must be set -/// - lnum must be the cursor line -/// - 'cursorlineopt' has "number" -/// - don't highlight filler lines (when in diff mode) -/// - When line is wrapped and 'cursorlineopt' does not have "line", only highlight the line number -/// itself on the first screenline of the wrapped line, otherwise highlight the number column of -/// all screenlines of the wrapped line. -static bool use_cursor_line_nr(win_T *wp, winlinevars_T *wlv) -{ - return wp->w_p_cul - && wlv->lnum == wp->w_cursorline - && (wp->w_p_culopt_flags & CULOPT_NBR) - && (wlv->row == wlv->startrow + wlv->filler_lines - || (wlv->row > wlv->startrow + wlv->filler_lines - && (wp->w_p_culopt_flags & CULOPT_LINE))); -} - -static inline void get_line_number_str(win_T *wp, linenr_T lnum, char *buf, size_t buf_len) -{ - long num; - char *fmt = "%*ld "; - - if (wp->w_p_nu && !wp->w_p_rnu) { - // 'number' + 'norelativenumber' - num = (long)lnum; - } else { - // 'relativenumber', don't use negative numbers - num = labs((long)get_cursor_rel_lnum(wp, lnum)); - if (num == 0 && wp->w_p_nu && wp->w_p_rnu) { - // 'number' + 'relativenumber' - num = lnum; - fmt = "%-*ld "; - } - } - - snprintf(buf, buf_len, fmt, number_width(wp), num); -} - -static int get_line_number_attr(win_T *wp, winlinevars_T *wlv) -{ - if (use_cursor_line_nr(wp, wlv)) { - // TODO(vim): Can we use CursorLine instead of CursorLineNr - // when CursorLineNr isn't set? - return win_hl_attr(wp, HLF_CLN); - } - - if (wp->w_p_rnu) { - if (wlv->lnum < wp->w_cursor.lnum) { - // Use LineNrAbove - return win_hl_attr(wp, HLF_LNA); - } - if (wlv->lnum > wp->w_cursor.lnum) { - // Use LineNrBelow - return win_hl_attr(wp, HLF_LNB); - } - } - - return win_hl_attr(wp, HLF_N); -} - static void apply_cursorline_highlight(win_T *wp, winlinevars_T *wlv) { wlv->cul_attr = win_hl_attr(wp, HLF_CUL); |