diff options
author | zeertzjq <zeertzjq@outlook.com> | 2023-08-25 20:05:00 +0800 |
---|---|---|
committer | zeertzjq <zeertzjq@outlook.com> | 2023-08-25 20:20:42 +0800 |
commit | ff67bb3d05d25ab4fb353839d1570d86f88dbfef (patch) | |
tree | 036a8cf9eaafca3a3a474881707f2fa7198e12f1 /src | |
parent | 93af6d9ed0af984be7fc383f25ca20b595961c46 (diff) | |
download | rneovim-ff67bb3d05d25ab4fb353839d1570d86f88dbfef.tar.gz rneovim-ff67bb3d05d25ab4fb353839d1570d86f88dbfef.tar.bz2 rneovim-ff67bb3d05d25ab4fb353839d1570d86f88dbfef.zip |
refactor(plines.c): deduplicate code for virtual text cursor offset
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/plines.c | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/src/nvim/plines.c b/src/nvim/plines.c index 4fba3b4c4e..8ecefc805a 100644 --- a/src/nvim/plines.c +++ b/src/nvim/plines.c @@ -369,13 +369,7 @@ int win_lbr_chartabsize(chartabsize_T *cts, int *headp) head += (max_head_vcol - (vcol + head_prev + prev_rem) + width2 - 1) / width2 * head_mid; } else if (max_head_vcol < 0) { - int off = 0; - if (c != NUL || !(State & MODE_NORMAL)) { - off += cts->cts_cur_text_width_left; - } - if (c != NUL && (State & MODE_NORMAL)) { - off += cts->cts_cur_text_width_right; - } + int off = virt_text_cursor_off(cts, c == NUL); if (off >= prev_rem) { head += (1 + (off - prev_rem) / width) * head_mid; } @@ -453,6 +447,23 @@ static bool in_win_border(win_T *wp, colnr_T vcol) return (vcol - width1) % width2 == width2 - 1; } +/// Get how many virtual columns inline virtual text should offset the cursor. +/// +/// @param cts should contain information stored by win_lbr_chartabsize() +/// about widths of left and right gravity virtual text +/// @param on_NUL whether this is the end of the line +static int virt_text_cursor_off(chartabsize_T *cts, bool on_NUL) +{ + int off = 0; + if (!on_NUL || !(State & MODE_NORMAL)) { + off += cts->cts_cur_text_width_left; + } + if (!on_NUL && (State & MODE_NORMAL)) { + off += cts->cts_cur_text_width_right; + } + return off; +} + /// Get virtual column number of pos. /// start: on the first position of this character (TAB, ctrl) /// cursor: where the cursor is on this character (first char, except for TAB) @@ -594,12 +605,7 @@ void getvcol(win_T *wp, pos_T *pos, colnr_T *start, colnr_T *cursor, colnr_T *en // cursor at end *cursor = vcol + incr - 1; } else { - if (!on_NUL || !(State & MODE_NORMAL)) { - vcol += cts.cts_cur_text_width_left; - } - if (!on_NUL && (State & MODE_NORMAL)) { - vcol += cts.cts_cur_text_width_right; - } + vcol += virt_text_cursor_off(&cts, on_NUL); // cursor at start *cursor = vcol + head; } |