From 743ddaa764a6949e9fb7132222a26c51a19a16cb Mon Sep 17 00:00:00 2001 From: Luuk van Baal Date: Sat, 23 Nov 2024 23:03:46 +0100 Subject: [PATCH 1/2] feat(marks): add conceal_lines to nvim_buf_set_extmark() Implement an extmark property that conceals lines vertically. --- runtime/doc/api.txt | 4 + runtime/doc/news.txt | 1 + runtime/lua/vim/_meta/api.lua | 3 + runtime/lua/vim/_meta/api_keysets.lua | 1 + src/nvim/api/extmark.c | 12 ++ src/nvim/api/keysets_defs.h | 1 + src/nvim/buffer_defs.h | 1 + src/nvim/change.c | 3 +- src/nvim/decoration.c | 63 +++++++- src/nvim/decoration_defs.h | 1 + src/nvim/diff.c | 3 +- src/nvim/drawscreen.c | 52 ++++-- src/nvim/edit.c | 23 +-- src/nvim/fold.c | 4 +- src/nvim/marktree.c | 36 +++-- src/nvim/marktree.h | 10 +- src/nvim/marktree_defs.h | 3 +- src/nvim/move.c | 93 +++++------ src/nvim/normal.c | 30 ++-- src/nvim/plines.c | 19 ++- src/nvim/window.c | 10 +- test/functional/api/extmark_spec.lua | 2 + test/functional/ui/decorations_spec.lua | 203 ++++++++++++++++++++++++ 23 files changed, 450 insertions(+), 128 deletions(-) diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index cb3b2a3f77..83fcf71709 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -2774,6 +2774,10 @@ nvim_buf_set_extmark({buffer}, {ns_id}, {line}, {col}, {opts}) When a character is supplied it is used as |:syn-cchar|. "hl_group" is used as highlight for the cchar if provided, otherwise it defaults to |hl-Conceal|. + • conceal_lines: string which should be either empty or a + single character. When provided, lines in the range are + not drawn at all; the next unconcealed line is drawn + instead. • spell: boolean indicating that spell checking should be performed within this extmark • ui_watched: boolean that indicates the mark should be diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index e7d4b92f7e..072e4e2321 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -172,6 +172,7 @@ API • |nvim__ns_set()| can set properties for a namespace • |vim.json.encode()| has an option to enable forward slash escaping +• New |nvim_buf_set_extmark()| flag `conceal_lines` to conceal an entire line. DEFAULTS diff --git a/runtime/lua/vim/_meta/api.lua b/runtime/lua/vim/_meta/api.lua index b2385197bd..85e91220de 100644 --- a/runtime/lua/vim/_meta/api.lua +++ b/runtime/lua/vim/_meta/api.lua @@ -686,6 +686,9 @@ function vim.api.nvim_buf_line_count(buffer) end --- When a character is supplied it is used as `:syn-cchar`. --- "hl_group" is used as highlight for the cchar if provided, --- otherwise it defaults to `hl-Conceal`. +--- - conceal_lines: string which should be either empty or a single +--- character. When provided, lines in the range are not drawn +--- at all; the next unconcealed line is drawn instead. --- - spell: boolean indicating that spell checking should be --- performed within this extmark --- - ui_watched: boolean that indicates the mark should be drawn diff --git a/runtime/lua/vim/_meta/api_keysets.lua b/runtime/lua/vim/_meta/api_keysets.lua index e11dddb2d3..d149a75294 100644 --- a/runtime/lua/vim/_meta/api_keysets.lua +++ b/runtime/lua/vim/_meta/api_keysets.lua @@ -263,6 +263,7 @@ error('Cannot require a meta file') --- @field line_hl_group? integer|string --- @field cursorline_hl_group? integer|string --- @field conceal? string +--- @field conceal_lines? string --- @field spell? boolean --- @field ui_watched? boolean --- @field undo_restore? boolean diff --git a/src/nvim/api/extmark.c b/src/nvim/api/extmark.c index c94b8df9ea..93b3006781 100644 --- a/src/nvim/api/extmark.c +++ b/src/nvim/api/extmark.c @@ -475,6 +475,9 @@ Array nvim_buf_get_extmarks(Buffer buffer, Integer ns_id, Object start, Object e /// When a character is supplied it is used as |:syn-cchar|. /// "hl_group" is used as highlight for the cchar if provided, /// otherwise it defaults to |hl-Conceal|. +/// - conceal_lines: string which should be either empty or a single +/// character. When provided, lines in the range are not drawn +/// at all; the next unconcealed line is drawn instead. /// - spell: boolean indicating that spell checking should be /// performed within this extmark /// - ui_watched: boolean that indicates the mark should be drawn @@ -575,6 +578,11 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer } } + if (HAS_KEY(opts, set_extmark, conceal_lines)) { + hl.flags |= kSHConcealLines; + has_hl = true; + } + if (HAS_KEY(opts, set_extmark, virt_text)) { virt_text.data.virt_text = parse_virt_text(opts->virt_text, err, &virt_text.width); if (ERROR_SET(err)) { @@ -793,6 +801,10 @@ Integer nvim_buf_set_extmark(Buffer buffer, Integer ns_id, Integer line, Integer } } + if (hl.flags & kSHConcealLines) { + decor_flags |= MT_FLAG_DECOR_CONCEAL_LINES; + } + DecorInline decor = DECOR_INLINE_INIT; if (decor_alloc || decor_indexed != DECOR_ID_INVALID || url != NULL || schar_high(hl.conceal_char)) { diff --git a/src/nvim/api/keysets_defs.h b/src/nvim/api/keysets_defs.h index 48f5f7246c..147521b272 100644 --- a/src/nvim/api/keysets_defs.h +++ b/src/nvim/api/keysets_defs.h @@ -51,6 +51,7 @@ typedef struct { HLGroupID line_hl_group; HLGroupID cursorline_hl_group; String conceal; + String conceal_lines; Boolean spell; Boolean ui_watched; Boolean undo_restore; diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index bb6eef3c29..94956268bc 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -813,6 +813,7 @@ typedef struct { uint16_t wl_size; // height in screen lines char wl_valid; // true values are valid for text in buffer char wl_folded; // true when this is a range of folded lines + linenr_T wl_foldend; // last buffer line number for folded line linenr_T wl_lastlnum; // last buffer line number for logical line } wline_T; diff --git a/src/nvim/change.c b/src/nvim/change.c index faaae96af9..a3d9765f83 100644 --- a/src/nvim/change.c +++ b/src/nvim/change.c @@ -206,9 +206,10 @@ static void changed_lines_invalidate_win(win_T *wp, linenr_T lnum, colnr_T col, } else if (xtra != 0) { // line below change wp->w_lines[i].wl_lnum += xtra; + wp->w_lines[i].wl_foldend += xtra; wp->w_lines[i].wl_lastlnum += xtra; } - } else if (wp->w_lines[i].wl_lastlnum >= lnum) { + } else if (wp->w_lines[i].wl_foldend >= lnum) { // change somewhere inside this range of folded lines, // may need to be redrawn wp->w_lines[i].wl_valid = false; diff --git a/src/nvim/decoration.c b/src/nvim/decoration.c index 0808f13491..91648adad8 100644 --- a/src/nvim/decoration.c +++ b/src/nvim/decoration.c @@ -125,6 +125,13 @@ void decor_redraw_sh(buf_T *buf, int row1, int row2, DecorSignHighlight sh) redraw_buf_range_later(buf, row1 + 1, row2 + 1); } } + if (sh.flags & kSHConcealLines) { + FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { + if (wp->w_buffer == buf) { + changed_window_setting(wp); + } + } + } if (sh.flags & kSHUIWatched) { redraw_buf_line_later(buf, row1 + 1, false); } @@ -833,8 +840,47 @@ int sign_item_cmp(const void *p1, const void *p2) return 0; } -static const uint32_t sign_filter[4] = {[kMTMetaSignText] = kMTFilterSelect, - [kMTMetaSignHL] = kMTFilterSelect }; +static const uint32_t conceal_filter[kMTMetaCount] = {[kMTMetaConcealLines] = kMTFilterSelect }; + +bool decor_conceal_line(win_T *wp, int row, bool check_cursor) +{ + if (wp->w_p_cole < 2 || !buf_meta_total(wp->w_buffer, kMTMetaConcealLines) + || (!check_cursor && ((row + 1 == wp->w_cursor.lnum) && !conceal_cursor_line(wp)))) { + return false; + } + + MTPair pair; + MarkTreeIter itr[1]; + marktree_itr_get_overlap(wp->w_buffer->b_marktree, row, 0, itr); + while (marktree_itr_step_overlap(wp->w_buffer->b_marktree, itr, &pair)) { + if (mt_conceal_lines(pair.start) && ns_in_win(pair.start.ns, wp)) { + return true; + } + } + + marktree_itr_step_out_filter(wp->w_buffer->b_marktree, itr, conceal_filter); + + while (itr->x) { + MTKey mark = marktree_itr_current(itr); + if (mark.pos.row > row) { + break; + } + if (mt_conceal_lines(mark) && ns_in_win(pair.start.ns, wp)) { + return true; + } + marktree_itr_next_filter(wp->w_buffer->b_marktree, itr, row + 1, 0, conceal_filter); + } + return false; +} + +bool win_lines_concealed(win_T *wp) +{ + return hasAnyFolding(wp) + || (wp->w_p_cole >= 2 && buf_meta_total(wp->w_buffer, kMTMetaConcealLines)); +} + +static const uint32_t sign_filter[kMTMetaCount] = {[kMTMetaSignText] = kMTFilterSelect, + [kMTMetaSignHL] = kMTFilterSelect }; /// Return the sign attributes on the currently refreshed row. /// @@ -856,7 +902,7 @@ void decor_redraw_signs(win_T *wp, buf_T *buf, int row, SignTextAttrs sattrs[], // TODO(bfredl): integrate with main decor loop. marktree_itr_get_overlap(buf->b_marktree, row, 0, itr); while (marktree_itr_step_overlap(buf->b_marktree, itr, &pair)) { - if (!mt_invalid(pair.start) && mt_decor_sign(pair.start)) { + if (!mt_invalid(pair.start) && mt_decor_sign(pair.start) && ns_in_win(pair.start.ns, wp)) { DecorSignHighlight *sh = decor_find_sign(mt_decor(pair.start)); num_text += (sh->text[0] != NUL); kv_push(signs, ((SignItem){ sh, pair.start.id })); @@ -923,7 +969,7 @@ DecorSignHighlight *decor_find_sign(DecorInline decor) } } -static const uint32_t signtext_filter[4] = {[kMTMetaSignText] = kMTFilterSelect }; +static const uint32_t signtext_filter[kMTMetaCount] = {[kMTMetaSignText] = kMTFilterSelect }; /// Count the number of signs in a range after adding/removing a sign, or to /// (re-)initialize a range in "b_signcols.count". @@ -1018,7 +1064,7 @@ bool decor_redraw_eol(win_T *wp, DecorState *state, int *eol_attr, int eol_col) return has_virt_pos; } -static const uint32_t lines_filter[4] = {[kMTMetaLines] = kMTFilterSelect }; +static const uint32_t lines_filter[kMTMetaCount] = {[kMTMetaLines] = kMTFilterSelect }; /// @param apply_folds Only count virtual lines that are not in folds. int decor_virt_lines(win_T *wp, int start_row, int end_row, VirtLines *lines, bool apply_folds) @@ -1049,7 +1095,8 @@ int decor_virt_lines(win_T *wp, int start_row, int end_row, VirtLines *lines, bo int mrow = mark.pos.row; int draw_row = mrow + (above ? 0 : 1); if (draw_row >= start_row && draw_row < end_row - && (!apply_folds || !hasFolding(wp, mrow + 1, NULL, NULL))) { + && (!apply_folds || !(hasFolding(wp, mrow + 1, NULL, NULL) + || decor_conceal_line(wp, mrow, false)))) { virt_lines += (int)kv_size(vt->data.virt_lines); if (lines) { kv_splice(*lines, vt->data.virt_lines); @@ -1116,6 +1163,10 @@ void decor_to_dict_legacy(Dict *dict, DecorInline decor, bool hl_name, Arena *ar PUT_C(*dict, "conceal", CSTR_TO_ARENA_OBJ(arena, buf)); } + if (sh_hl.flags & kSHConcealLines) { + PUT_C(*dict, "conceal_lines", STRING_OBJ(cstr_as_string(""))); + } + if (sh_hl.flags & kSHSpellOn) { PUT_C(*dict, "spell", BOOLEAN_OBJ(true)); } else if (sh_hl.flags & kSHSpellOff) { diff --git a/src/nvim/decoration_defs.h b/src/nvim/decoration_defs.h index 58ba93a7ba..1973f1ce86 100644 --- a/src/nvim/decoration_defs.h +++ b/src/nvim/decoration_defs.h @@ -46,6 +46,7 @@ enum { kSHSpellOn = 16, kSHSpellOff = 32, kSHConceal = 64, + kSHConcealLines = 128, }; typedef struct { diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 20f93049c1..235462339c 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -24,6 +24,7 @@ #include "nvim/change.h" #include "nvim/charset.h" #include "nvim/cursor.h" +#include "nvim/decoration.h" #include "nvim/diff.h" #include "nvim/drawscreen.h" #include "nvim/errors.h" @@ -2082,7 +2083,7 @@ int diff_check_with_linestatus(win_T *wp, linenr_T lnum, int *linestatus) } // A closed fold never has filler lines. - if (hasFolding(wp, lnum, NULL, NULL)) { + if (hasFolding(wp, lnum, NULL, NULL) || decor_conceal_line(wp, lnum - 1, false)) { return 0; } diff --git a/src/nvim/drawscreen.c b/src/nvim/drawscreen.c index 0961aabf21..a2b3448754 100644 --- a/src/nvim/drawscreen.c +++ b/src/nvim/drawscreen.c @@ -148,6 +148,11 @@ void conceal_check_cursor_line(void) } redrawWinline(curwin, curwin->w_cursor.lnum); + + // Concealed line visibility toggled. + if (decor_conceal_line(curwin, curwin->w_cursor.lnum - 1, true)) { + changed_window_setting(curwin); + } // Need to recompute cursor column, e.g., when starting Visual mode // without concealing. curs_columns(curwin, true); @@ -1622,7 +1627,7 @@ static void win_update(win_T *wp) } } - if (mod_top != 0 && hasAnyFolding(wp)) { + if (mod_top != 0 && (win_lines_concealed(wp))) { // A change in a line can cause lines above it to become folded or // unfolded. Find the top most buffer line that may be affected. // If the line was previously folded and displayed, get the first @@ -1719,12 +1724,12 @@ static void win_update(win_T *wp) && wp->w_topfill > wp->w_old_topfill))) { // New topline is above old topline: May scroll down. int j; - if (hasAnyFolding(wp)) { - // count the number of lines we are off, counting a sequence - // of folded lines as one + if (win_lines_concealed(wp)) { + // Count the number of lines we are off, counting a sequence + // of folded lines as one, and skip concealed lines. j = 0; for (linenr_T ln = wp->w_topline; ln < wp->w_lines[0].wl_lnum; ln++) { - j++; + j += !decor_conceal_line(wp, ln - 1, false); if (j >= wp->w_grid.rows - 2) { break; } @@ -2109,6 +2114,28 @@ static void win_update(win_T *wp) top_to_mod = false; } + // When lines are folded, display one line for all of them. + // Otherwise, display normally (can be several display lines when + // 'wrap' is on). + foldinfo_T foldinfo = wp->w_p_cul && lnum == wp->w_cursor.lnum + ? cursorline_fi : fold_info(wp, lnum); + + // Concealed line: continue to next line on the same row. + if (decor_conceal_line(wp, lnum - 1, false)) { + if (wp == curwin && lnum == curwin->w_cursor.lnum) { + conceal_cursor_used = conceal_cursor_line(curwin); + } + if (idx > 0) { + wp->w_lines[idx - 1].wl_lastlnum = lnum + foldinfo.fi_lines - 1; + } + if (lnum == mod_top && lnum < mod_bot) { + mod_top += foldinfo.fi_lines ? foldinfo.fi_lines : 1; + } + lnum += foldinfo.fi_lines ? foldinfo.fi_lines : 1; + spv.spv_capcol_lnum = 0; + continue; + } + // When at start of changed lines: May scroll following lines // up or down to minimize redrawing. // Don't do this when the change continues until the end. @@ -2158,8 +2185,10 @@ static void win_update(win_T *wp) // rows, and may insert/delete lines int j = idx; for (l = lnum; l < mod_bot; l++) { + linenr_T first = l; + int prev_rows = new_rows; if (hasFolding(wp, l, NULL, &l)) { - new_rows++; + new_rows += !decor_conceal_line(wp, first - 1, false); } else if (l == wp->w_topline) { int n = plines_win_nofill(wp, l, false) + wp->w_topfill - adjust_plines_for_skipcol(wp); @@ -2168,7 +2197,8 @@ static void win_update(win_T *wp) } else { new_rows += plines_win(wp, l, true); } - j++; + // Do not increment when height was 0 (for a concealed line). + j += (prev_rows != new_rows); if (new_rows > wp->w_grid.rows - row - 2) { // it's getting too much, must redraw the rest new_rows = 9999; @@ -2250,12 +2280,6 @@ static void win_update(win_T *wp) } } - // When lines are folded, display one line for all of them. - // Otherwise, display normally (can be several display lines when - // 'wrap' is on). - foldinfo_T foldinfo = wp->w_p_cul && lnum == wp->w_cursor.lnum - ? cursorline_fi : fold_info(wp, lnum); - if (foldinfo.fi_lines == 0 && idx < wp->w_lines_valid && wp->w_lines[idx].wl_valid @@ -2290,11 +2314,13 @@ static void win_update(win_T *wp) if (foldinfo.fi_lines == 0) { wp->w_lines[idx].wl_folded = false; + wp->w_lines[idx].wl_foldend = lnum; wp->w_lines[idx].wl_lastlnum = lnum; did_update = DID_LINE; } else { foldinfo.fi_lines--; wp->w_lines[idx].wl_folded = true; + wp->w_lines[idx].wl_foldend = lnum + foldinfo.fi_lines; wp->w_lines[idx].wl_lastlnum = lnum + foldinfo.fi_lines; did_update = DID_FOLD; } diff --git a/src/nvim/edit.c b/src/nvim/edit.c index f5e11a188f..5a4b8cb3db 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -15,6 +15,7 @@ #include "nvim/change.h" #include "nvim/charset.h" #include "nvim/cursor.h" +#include "nvim/decoration.h" #include "nvim/digraph.h" #include "nvim/drawscreen.h" #include "nvim/edit.h" @@ -2577,15 +2578,15 @@ int oneleft(void) return OK; } -/// Move the cursor up "n" lines in window "wp". -/// Takes care of closed folds. -void cursor_up_inner(win_T *wp, linenr_T n) +/// Move the cursor up "n" lines in window "wp". Takes care of closed folds. +/// Skips over concealed lines when "skip_conceal" is true. +void cursor_up_inner(win_T *wp, linenr_T n, bool skip_conceal) { linenr_T lnum = wp->w_cursor.lnum; if (n >= lnum) { lnum = 1; - } else if (hasAnyFolding(wp)) { + } else if (win_lines_concealed(wp)) { // Count each sequence of folded lines as one logical line. // go to the start of the current fold @@ -2594,6 +2595,7 @@ void cursor_up_inner(win_T *wp, linenr_T n) while (n--) { // move up one line lnum--; + n += skip_conceal && decor_conceal_line(wp, lnum - 1, true); if (lnum <= 1) { break; } @@ -2619,7 +2621,7 @@ int cursor_up(linenr_T n, bool upd_topline) if (n > 0 && curwin->w_cursor.lnum <= 1) { return FAIL; } - cursor_up_inner(curwin, n); + cursor_up_inner(curwin, n, false); // try to advance to the column we want to be at coladvance(curwin, curwin->w_curswant); @@ -2631,16 +2633,16 @@ int cursor_up(linenr_T n, bool upd_topline) return OK; } -/// Move the cursor down "n" lines in window "wp". -/// Takes care of closed folds. -void cursor_down_inner(win_T *wp, int n) +/// Move the cursor down "n" lines in window "wp". Takes care of closed folds. +/// Skips over concealed lines when "skip_conceal" is true. +void cursor_down_inner(win_T *wp, int n, bool skip_conceal) { linenr_T lnum = wp->w_cursor.lnum; linenr_T line_count = wp->w_buffer->b_ml.ml_line_count; if (lnum + n >= line_count) { lnum = line_count; - } else if (hasAnyFolding(wp)) { + } else if (win_lines_concealed(wp)) { linenr_T last; // count each sequence of folded lines as one logical line @@ -2650,6 +2652,7 @@ void cursor_down_inner(win_T *wp, int n) } else { lnum++; } + n += skip_conceal && decor_conceal_line(wp, lnum - 1, true); if (lnum >= line_count) { break; } @@ -2671,7 +2674,7 @@ int cursor_down(int n, bool upd_topline) if (n > 0 && lnum >= curwin->w_buffer->b_ml.ml_line_count) { return FAIL; } - cursor_down_inner(curwin, n); + cursor_down_inner(curwin, n, false); // try to advance to the column we want to be at coladvance(curwin, curwin->w_curswant); diff --git a/src/nvim/fold.c b/src/nvim/fold.c index c9699cb161..6f73d033bd 100644 --- a/src/nvim/fold.c +++ b/src/nvim/fold.c @@ -194,7 +194,7 @@ bool hasFoldingWin(win_T *const win, const linenr_T lnum, linenr_T *const firstp const int x = find_wl_entry(win, lnum); if (x >= 0) { first = win->w_lines[x].wl_lnum; - last = win->w_lines[x].wl_lastlnum; + last = win->w_lines[x].wl_foldend; had_folded = win->w_lines[x].wl_folded; } } @@ -971,7 +971,7 @@ int find_wl_entry(win_T *win, linenr_T lnum) if (lnum < win->w_lines[i].wl_lnum) { return -1; } - if (lnum <= win->w_lines[i].wl_lastlnum) { + if (lnum <= win->w_lines[i].wl_foldend) { return i; } } diff --git a/src/nvim/marktree.c b/src/nvim/marktree.c index 5ccd4fd45d..e2b4296fcd 100644 --- a/src/nvim/marktree.c +++ b/src/nvim/marktree.c @@ -250,7 +250,7 @@ static inline void split_node(MarkTree *b, MTNode *x, const int i, MTKey next) refkey(b, x, i); x->n++; - uint32_t meta_inc[4]; + uint32_t meta_inc[kMTMetaCount]; meta_describe_key(meta_inc, x->key[i]); for (int m = 0; m < kMTMetaCount; m++) { // y used contain all of z and x->key[i], discount those @@ -451,6 +451,7 @@ static void meta_describe_key_inc(uint32_t *meta_inc, MTKey *k) meta_inc[kMTMetaLines] += (k->flags & MT_FLAG_DECOR_VIRT_LINES) ? 1 : 0; meta_inc[kMTMetaSignHL] += (k->flags & MT_FLAG_DECOR_SIGNHL) ? 1 : 0; meta_inc[kMTMetaSignText] += (k->flags & MT_FLAG_DECOR_SIGNTEXT) ? 1 : 0; + meta_inc[kMTMetaConcealLines] += (k->flags & MT_FLAG_DECOR_CONCEAL_LINES) ? 1 : 0; } } @@ -505,10 +506,10 @@ void marktree_put_key(MarkTree *b, MTKey k) r = s; } - uint32_t meta_inc[4]; + uint32_t meta_inc[kMTMetaCount]; meta_describe_key(meta_inc, k); marktree_putp_aux(b, r, k, meta_inc); - for (int m = 0; m < 4; m++) { + for (int m = 0; m < kMTMetaCount; m++) { b->meta_root[m] += meta_inc[m]; } b->n_keys++; @@ -579,7 +580,7 @@ uint64_t marktree_del_itr(MarkTree *b, MarkTreeIter *itr, bool rev) assert(x->level == 0); MTKey intkey = x->key[itr->i]; - uint32_t meta_inc[4]; + uint32_t meta_inc[kMTMetaCount]; meta_describe_key(meta_inc, intkey); if (x->n > itr->i + 1) { memmove(&x->key[itr->i], &x->key[itr->i + 1], @@ -776,7 +777,7 @@ uint64_t marktree_del_itr(MarkTree *b, MarkTreeIter *itr, bool rev) void marktree_revise_meta(MarkTree *b, MarkTreeIter *itr, MTKey old_key) { - uint32_t meta_old[4], meta_new[4]; + uint32_t meta_old[kMTMetaCount], meta_new[kMTMetaCount]; meta_describe_key(meta_old, old_key); meta_describe_key(meta_new, rawkey(itr)); @@ -1038,7 +1039,7 @@ static MTNode *merge_node(MarkTree *b, MTNode *p, int i) relative(p->key[i - 1].pos, &x->key[x->n].pos); } - uint32_t meta_inc[4]; + uint32_t meta_inc[kMTMetaCount]; meta_describe_key(meta_inc, x->key[x->n]); memmove(&x->key[x->n + 1], y->key, (size_t)y->n * sizeof(MTKey)); @@ -1128,9 +1129,9 @@ static void pivot_right(MarkTree *b, MTPos p_pos, MTNode *p, const int i) p->key[i] = x->key[x->n - 1]; refkey(b, p, i); - uint32_t meta_inc_y[4]; + uint32_t meta_inc_y[kMTMetaCount]; meta_describe_key(meta_inc_y, y->key[0]); - uint32_t meta_inc_x[4]; + uint32_t meta_inc_x[kMTMetaCount]; meta_describe_key(meta_inc_x, p->key[i]); for (int m = 0; m < kMTMetaCount; m++) { @@ -1214,9 +1215,9 @@ static void pivot_left(MarkTree *b, MTPos p_pos, MTNode *p, int i) p->key[i] = y->key[0]; refkey(b, p, i); - uint32_t meta_inc_x[4]; + uint32_t meta_inc_x[kMTMetaCount]; meta_describe_key(meta_inc_x, x->key[x->n]); - uint32_t meta_inc_y[4]; + uint32_t meta_inc_y[kMTMetaCount]; meta_describe_key(meta_inc_y, p->key[i]); for (int m = 0; m < kMTMetaCount; m++) { p->meta[i][m] += meta_inc_x[m]; @@ -1619,8 +1620,13 @@ bool marktree_itr_next_filter(MarkTree *b, MarkTreeIter *itr, int stop_row, int return marktree_itr_check_filter(b, itr, stop_row, stop_col, meta_filter); } -const uint32_t meta_map[4] = { MT_FLAG_DECOR_VIRT_TEXT_INLINE, MT_FLAG_DECOR_VIRT_LINES, - MT_FLAG_DECOR_SIGNHL, MT_FLAG_DECOR_SIGNTEXT }; +const uint32_t meta_map[kMTMetaCount] = { + MT_FLAG_DECOR_VIRT_TEXT_INLINE, + MT_FLAG_DECOR_VIRT_LINES, + MT_FLAG_DECOR_SIGNHL, + MT_FLAG_DECOR_SIGNTEXT, + MT_FLAG_DECOR_CONCEAL_LINES +}; static bool marktree_itr_check_filter(MarkTree *b, MarkTreeIter *itr, int stop_row, int stop_col, MetaFilter meta_filter) { @@ -1860,9 +1866,9 @@ static void swap_keys(MarkTree *b, MarkTreeIter *itr1, MarkTreeIter *itr2, Damag itr2->i, itr1->i })); } - uint32_t meta_inc_1[4]; + uint32_t meta_inc_1[kMTMetaCount]; meta_describe_key(meta_inc_1, rawkey(itr1)); - uint32_t meta_inc_2[4]; + uint32_t meta_inc_2[kMTMetaCount]; meta_describe_key(meta_inc_2, rawkey(itr2)); if (memcmp(meta_inc_1, meta_inc_2, sizeof(meta_inc_1)) != 0) { @@ -2373,7 +2379,7 @@ size_t marktree_check_node(MarkTree *b, MTNode *x, MTPos *last, bool *last_right *last = x->key[x->n - 1].pos; } - uint32_t meta_node[4]; + uint32_t meta_node[kMTMetaCount]; meta_describe_node(meta_node, x); for (int m = 0; m < kMTMetaCount; m++) { assert(meta_node_ref[m] == meta_node[m]); diff --git a/src/nvim/marktree.h b/src/nvim/marktree.h index 15df57ef63..d48d7969a7 100644 --- a/src/nvim/marktree.h +++ b/src/nvim/marktree.h @@ -34,6 +34,7 @@ #define MT_FLAG_DECOR_SIGNHL (((uint16_t)1) << 10) #define MT_FLAG_DECOR_VIRT_LINES (((uint16_t)1) << 11) #define MT_FLAG_DECOR_VIRT_TEXT_INLINE (((uint16_t)1) << 12) +#define MT_FLAG_DECOR_CONCEAL_LINES (((uint16_t)1) << 13) // These _must_ be last to preserve ordering of marks #define MT_FLAG_RIGHT_GRAVITY (((uint16_t)1) << 14) @@ -43,8 +44,8 @@ | MT_FLAG_DECOR_SIGNHL | MT_FLAG_DECOR_VIRT_LINES \ | MT_FLAG_DECOR_VIRT_TEXT_INLINE) -#define MT_FLAG_EXTERNAL_MASK (MT_FLAG_DECOR_MASK | MT_FLAG_NO_UNDO \ - | MT_FLAG_INVALIDATE | MT_FLAG_INVALID) +#define MT_FLAG_EXTERNAL_MASK (MT_FLAG_DECOR_MASK | MT_FLAG_NO_UNDO | MT_FLAG_INVALIDATE \ + | MT_FLAG_INVALID | MT_FLAG_DECOR_CONCEAL_LINES) // this is defined so that start and end of the same range have adjacent ids #define MARKTREE_END_FLAG ((uint64_t)1) @@ -108,6 +109,11 @@ static inline bool mt_decor_sign(MTKey key) return key.flags & (MT_FLAG_DECOR_SIGNTEXT | MT_FLAG_DECOR_SIGNHL); } +static inline bool mt_conceal_lines(MTKey key) +{ + return key.flags & MT_FLAG_DECOR_CONCEAL_LINES; +} + static inline uint16_t mt_flags(bool right_gravity, bool no_undo, bool invalidate, bool decor_ext) { return (uint16_t)((right_gravity ? MT_FLAG_RIGHT_GRAVITY : 0) diff --git a/src/nvim/marktree_defs.h b/src/nvim/marktree_defs.h index d43130db6f..32a27a51a1 100644 --- a/src/nvim/marktree_defs.h +++ b/src/nvim/marktree_defs.h @@ -22,13 +22,12 @@ typedef struct { } MTPos; #define MTPos(r, c) ((MTPos){ .row = (r), .col = (c) }) -// Currently there are four counts, which makes for a uint32_t[4] per node -// which makes for nice autovectorization into a single XMM or NEON register typedef enum { kMTMetaInline, kMTMetaLines, kMTMetaSignHL, kMTMetaSignText, + kMTMetaConcealLines, kMTMetaCount, // sentinel, must be last } MetaIndex; diff --git a/src/nvim/move.c b/src/nvim/move.c index dbd86bb0c8..29fe358067 100644 --- a/src/nvim/move.c +++ b/src/nvim/move.c @@ -17,6 +17,7 @@ #include "nvim/buffer.h" #include "nvim/buffer_defs.h" #include "nvim/cursor.h" +#include "nvim/decoration.h" #include "nvim/diff.h" #include "nvim/drawscreen.h" #include "nvim/edit.h" @@ -318,14 +319,13 @@ void update_topline(win_T *wp) halfheight = 2; } int64_t n; - if (hasAnyFolding(wp)) { + if (win_lines_concealed(wp)) { // Count the number of logical lines between the cursor and // topline + p_so (approximation of how much will be // scrolled). n = 0; - for (linenr_T lnum = wp->w_cursor.lnum; - lnum < wp->w_topline + *so_ptr; lnum++) { - n++; + for (linenr_T lnum = wp->w_cursor.lnum; lnum < wp->w_topline + *so_ptr; lnum++) { + n += !decor_conceal_line(wp, lnum, false); // stop at end of file or when we know we are far off assert(wp->w_buffer != 0); if (lnum >= wp->w_buffer->b_ml.ml_line_count || n >= halfheight) { @@ -367,7 +367,7 @@ void update_topline(win_T *wp) assert(wp->w_buffer != 0); if (wp->w_botline <= wp->w_buffer->b_ml.ml_line_count) { if (wp->w_cursor.lnum < wp->w_botline) { - if ((wp->w_cursor.lnum >= wp->w_botline - *so_ptr || hasAnyFolding(wp))) { + if ((wp->w_cursor.lnum >= wp->w_botline - *so_ptr || win_lines_concealed(wp))) { lineoff_T loff; // Cursor is (a few lines) above botline, check if there are @@ -399,13 +399,12 @@ void update_topline(win_T *wp) } if (check_botline) { int line_count = 0; - if (hasAnyFolding(wp)) { + if (win_lines_concealed(wp)) { // Count the number of logical lines between the cursor and // botline - p_so (approximation of how much will be // scrolled). - for (linenr_T lnum = wp->w_cursor.lnum; - lnum >= wp->w_botline - *so_ptr; lnum--) { - line_count++; + for (linenr_T lnum = wp->w_cursor.lnum; lnum >= wp->w_botline - *so_ptr; lnum--) { + line_count += !decor_conceal_line(wp, lnum - 1, false); // stop at end of file or when we know we are far off if (lnum <= 0 || line_count > wp->w_height_inner + 1) { break; @@ -462,7 +461,7 @@ static int scrolljump_value(win_T *wp) static bool check_top_offset(win_T *wp) { int so = get_scrolloff_value(wp); - if (wp->w_cursor.lnum < wp->w_topline + so || hasAnyFolding(wp)) { + if (wp->w_cursor.lnum < wp->w_topline + so || win_lines_concealed(wp)) { lineoff_T loff; loff.lnum = wp->w_cursor.lnum; loff.fill = 0; @@ -506,6 +505,13 @@ void check_cursor_moved(win_T *wp) if (wp->w_cursor.lnum != wp->w_valid_cursor.lnum) { wp->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL |VALID_CHEIGHT|VALID_CROW|VALID_TOPLINE); + + // Concealed line visibility toggled. + if (wp->w_p_cole >= 2 && !conceal_cursor_line(wp) + && (decor_conceal_line(wp, wp->w_cursor.lnum - 1, true) + || decor_conceal_line(wp, wp->w_valid_cursor.lnum - 1, true))) { + changed_window_setting(wp); + } wp->w_valid_cursor = wp->w_cursor; wp->w_valid_leftcol = wp->w_leftcol; wp->w_valid_skipcol = wp->w_skipcol; @@ -1341,13 +1347,14 @@ bool scrolldown(win_T *wp, linenr_T line_count, int byfold) // A sequence of folded lines only counts for one logical line linenr_T first; if (hasFolding(wp, wp->w_topline, &first, NULL)) { - done++; + done += !decor_conceal_line(wp, first - 1, false); if (!byfold) { todo -= wp->w_topline - first - 1; } wp->w_botline -= wp->w_topline - first; wp->w_topline = first; } else { + todo += decor_conceal_line(wp, wp->w_topline - 1, false); if (do_sms) { int size = win_linetabsize(wp, wp->w_topline, ml_get_buf(wp->w_buffer, wp->w_topline), MAXCOL); @@ -1391,12 +1398,8 @@ bool scrolldown(win_T *wp, linenr_T line_count, int byfold) while (wrow >= wp->w_height_inner && wp->w_cursor.lnum > 1) { linenr_T first; if (hasFolding(wp, wp->w_cursor.lnum, &first, NULL)) { - wrow--; - if (first == 1) { - wp->w_cursor.lnum = 1; - } else { - wp->w_cursor.lnum = first - 1; - } + wrow -= !decor_conceal_line(wp, wp->w_cursor.lnum - 1, false); + wp->w_cursor.lnum = MAX(first - 1, 1); } else { wrow -= plines_win(wp, wp->w_cursor.lnum--, true); } @@ -1424,7 +1427,7 @@ bool scrollup(win_T *wp, linenr_T line_count, bool byfold) linenr_T botline = wp->w_botline; bool do_sms = wp->w_p_wrap && wp->w_p_sms; - if (do_sms || (byfold && hasAnyFolding(wp)) || win_may_fill(wp)) { + if (do_sms || (byfold && win_lines_concealed(wp)) || win_may_fill(wp)) { int width1 = wp->w_width_inner - win_col_off(wp); int width2 = width1 + win_col_off2(wp); int size = 0; @@ -1439,6 +1442,7 @@ bool scrollup(win_T *wp, linenr_T line_count, bool byfold) // the line, then advance to the next line. // folding: count each sequence of folded lines as one logical line. for (int todo = line_count; todo > 0; todo--) { + todo += decor_conceal_line(wp, wp->w_topline - 1, false); if (wp->w_topfill > 0) { wp->w_topfill--; } else { @@ -1495,10 +1499,8 @@ bool scrollup(win_T *wp, linenr_T line_count, bool byfold) check_topfill(wp, false); - if (hasAnyFolding(wp)) { - // Make sure w_topline is at the first of a sequence of folded lines. - hasFolding(wp, wp->w_topline, &wp->w_topline, NULL); - } + // Make sure w_topline is at the first of a sequence of folded lines. + hasFolding(wp, wp->w_topline, &wp->w_topline, NULL); wp->w_valid &= ~(VALID_WROW|VALID_CROW|VALID_BOTLINE); if (wp->w_cursor.lnum < wp->w_topline) { @@ -1702,8 +1704,8 @@ static void topline_back_winheight(win_T *wp, lineoff_T *lp, int winheight) if (lp->lnum < 1) { lp->height = MAXCOL; } else if (hasFolding(wp, lp->lnum, &lp->lnum, NULL)) { - // Add a closed fold - lp->height = 1; + // Add a closed fold unless concealed. + lp->height = !decor_conceal_line(wp, lp->lnum - 1, false); } else { lp->height = plines_win_nofill(wp, lp->lnum, winheight); } @@ -1732,8 +1734,8 @@ static void botline_forw(win_T *wp, lineoff_T *lp) if (lp->lnum > wp->w_buffer->b_ml.ml_line_count) { lp->height = MAXCOL; } else if (hasFolding(wp, lp->lnum, NULL, &lp->lnum)) { - // Add a closed fold - lp->height = 1; + // Add a closed fold unless concealed. + lp->height = !decor_conceal_line(wp, lp->lnum - 1, false); } else { lp->height = plines_win_nofill(wp, lp->lnum, true); } @@ -1785,9 +1787,8 @@ void scroll_cursor_top(win_T *wp, int min_scroll, int always) // Check if the lines from "top" to "bot" fit in the window. If they do, // set new_topline and advance "top" and "bot" to include more lines. while (top > 0) { - int i = hasFolding(wp, top, &top, NULL) - ? 1 // count one logical line for a sequence of folded lines - : plines_win_nofill(wp, top, true); + int i = plines_win_nofill(wp, top, true); + hasFolding(wp, top, &top, NULL); if (top < wp->w_topline) { scrolled += i; } @@ -1799,12 +1800,7 @@ void scroll_cursor_top(win_T *wp, int min_scroll, int always) used += i; if (extra + i <= off && bot < wp->w_buffer->b_ml.ml_line_count) { - if (hasFolding(wp, bot, NULL, &bot)) { - // count one logical line for a sequence of folded lines - used++; - } else { - used += plines_win(wp, bot, true); - } + used += plines_win_full(wp, bot, &bot, NULL, true, true); } if (used > wp->w_height_inner) { break; @@ -2265,7 +2261,7 @@ void cursor_correct(win_T *wp) linenr_T cln = wp->w_cursor.lnum; // Cursor Line Number if (cln >= wp->w_topline + above_wanted && cln < wp->w_botline - below_wanted - && !hasAnyFolding(wp)) { + && !win_lines_concealed(wp)) { return; } @@ -2290,19 +2286,12 @@ void cursor_correct(win_T *wp) int below = wp->w_filler_rows; // screen lines below botline while ((above < above_wanted || below < below_wanted) && topline < botline) { if (below < below_wanted && (below <= above || above >= above_wanted)) { - if (hasFolding(wp, botline, &botline, NULL)) { - below++; - } else { - below += plines_win(wp, botline, true); - } + below += plines_win_full(wp, botline, &botline, NULL, true, true); botline--; } if (above < above_wanted && (above < below || below >= below_wanted)) { - if (hasFolding(wp, topline, NULL, &topline)) { - above++; - } else { - above += plines_win_nofill(wp, topline, true); - } + above += plines_win_nofill(wp, topline, true); + hasFolding(wp, topline, NULL, &topline); // Count filler lines below this line as context. if (topline < botline) { @@ -2456,7 +2445,8 @@ int pagescroll(Direction dir, int count, bool half) int curscount = count; // Adjust count so as to not reveal end of buffer lines. if (dir == FORWARD - && (curwin->w_topline + curwin->w_height_inner + count > buflen || hasAnyFolding(curwin))) { + && (curwin->w_topline + curwin->w_height_inner + count > buflen + || win_lines_concealed(curwin))) { int n = plines_correct_topline(curwin, curwin->w_topline, NULL, false, NULL); if (n - count < curwin->w_height_inner && curwin->w_topline < buflen) { n += plines_m_win(curwin, curwin->w_topline + 1, buflen, curwin->w_height_inner + count); @@ -2474,13 +2464,14 @@ int pagescroll(Direction dir, int count, bool half) curwin->w_curswant = prev_curswant; } - // Move the cursor the same amount of screen lines. + // Move the cursor the same amount of screen lines, skipping over + // concealed lines as those were not included in "curscount". if (curwin->w_p_wrap) { - nv_screengo(&oa, dir, curscount); + nv_screengo(&oa, dir, curscount, true); } else if (dir == FORWARD) { - cursor_down_inner(curwin, curscount); + cursor_down_inner(curwin, curscount, true); } else { - cursor_up_inner(curwin, curscount); + cursor_up_inner(curwin, curscount, true); } } else { // Scroll [count] times 'window' or current window height lines. diff --git a/src/nvim/normal.c b/src/nvim/normal.c index 4d2abf1c8c..32371e9a7e 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -24,6 +24,7 @@ #include "nvim/charset.h" #include "nvim/cmdhist.h" #include "nvim/cursor.h" +#include "nvim/decoration.h" #include "nvim/diff.h" #include "nvim/digraph.h" #include "nvim/drawscreen.h" @@ -2486,7 +2487,7 @@ bool find_decl(char *ptr, size_t len, bool locally, bool thisblock, int flags_ar /// 'dist' must be positive. /// /// @return true if able to move cursor, false otherwise. -bool nv_screengo(oparg_T *oap, int dir, int dist) +bool nv_screengo(oparg_T *oap, int dir, int dist, bool skip_conceal) { int linelen = linetabsize(curwin, curwin->w_cursor.lnum); bool retval = true; @@ -2548,7 +2549,7 @@ bool nv_screengo(oparg_T *oap, int dir, int dist) retval = false; break; } - cursor_up_inner(curwin, 1); + cursor_up_inner(curwin, 1, skip_conceal); linelen = linetabsize(curwin, curwin->w_cursor.lnum); if (linelen > width1) { @@ -2573,7 +2574,7 @@ bool nv_screengo(oparg_T *oap, int dir, int dist) retval = false; break; } - cursor_down_inner(curwin, 1); + cursor_down_inner(curwin, 1, skip_conceal); curwin->w_curswant %= width2; // Check if the cursor has moved below the number display @@ -3616,12 +3617,11 @@ static void nv_scroll(cmdarg_T *cap) if (cap->count1 - 1 >= curwin->w_cursor.lnum) { curwin->w_cursor.lnum = 1; } else { - if (hasAnyFolding(curwin)) { + if (win_lines_concealed(curwin)) { // Count a fold for one screen line. - for (n = cap->count1 - 1; n > 0 - && curwin->w_cursor.lnum > curwin->w_topline; n--) { - hasFolding(curwin, curwin->w_cursor.lnum, - &curwin->w_cursor.lnum, NULL); + for (n = cap->count1 - 1; n > 0 && curwin->w_cursor.lnum > curwin->w_topline; n--) { + hasFolding(curwin, curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL); + n += decor_conceal_line(curwin, curwin->w_cursor.lnum, true); if (curwin->w_cursor.lnum > curwin->w_topline) { curwin->w_cursor.lnum--; } @@ -3634,8 +3634,7 @@ static void nv_scroll(cmdarg_T *cap) if (cap->cmdchar == 'M') { int used = 0; // Don't count filler lines above the window. - used -= win_get_fill(curwin, curwin->w_topline) - - curwin->w_topfill; + used -= win_get_fill(curwin, curwin->w_topline) - curwin->w_topfill; validate_botline(curwin); // make sure w_empty_rows is valid int half = (curwin->w_height_inner - curwin->w_empty_rows + 1) / 2; for (n = 0; curwin->w_topline + n < curbuf->b_ml.ml_line_count; n++) { @@ -3658,10 +3657,11 @@ static void nv_scroll(cmdarg_T *cap) } } else { // (cap->cmdchar == 'H') n = cap->count1 - 1; - if (hasAnyFolding(curwin)) { + if (win_lines_concealed(curwin)) { // Count a fold for one screen line. lnum = curwin->w_topline; - while (n-- > 0 && lnum < curwin->w_botline - 1) { + while ((decor_conceal_line(curwin, lnum - 1, true) || n-- > 0) + && lnum < curwin->w_botline - 1) { hasFolding(curwin, lnum, NULL, &lnum); lnum++; } @@ -5313,7 +5313,7 @@ static void nv_g_dollar_cmd(cmdarg_T *cap) curwin->w_cursor.col--; } } - } else if (nv_screengo(oap, FORWARD, cap->count1 - 1) == false) { + } else if (nv_screengo(oap, FORWARD, cap->count1 - 1, false) == false) { clearopbeep(oap); } } else { @@ -5440,7 +5440,7 @@ static void nv_g_cmd(cmdarg_T *cap) oap->motion_type = kMTLineWise; i = cursor_down(cap->count1, oap->op_type == OP_NOP); } else { - i = nv_screengo(oap, FORWARD, cap->count1); + i = nv_screengo(oap, FORWARD, cap->count1, false); } if (!i) { clearopbeep(oap); @@ -5454,7 +5454,7 @@ static void nv_g_cmd(cmdarg_T *cap) oap->motion_type = kMTLineWise; i = cursor_up(cap->count1, oap->op_type == OP_NOP); } else { - i = nv_screengo(oap, BACKWARD, cap->count1); + i = nv_screengo(oap, BACKWARD, cap->count1, false); } if (!i) { clearopbeep(oap); diff --git a/src/nvim/plines.c b/src/nvim/plines.c index 9bf486fb06..e0daf11001 100644 --- a/src/nvim/plines.c +++ b/src/nvim/plines.c @@ -80,7 +80,7 @@ int linetabsize(win_T *wp, linenr_T lnum) return win_linetabsize(wp, lnum, ml_get_buf(wp->w_buffer, lnum), MAXCOL); } -static const uint32_t inline_filter[4] = {[kMTMetaInline] = kMTFilterSelect }; +static const uint32_t inline_filter[kMTMetaCount] = {[kMTMetaInline] = kMTFilterSelect }; /// Prepare the structure passed to charsize functions. /// @@ -749,6 +749,10 @@ int plines_win(win_T *wp, linenr_T lnum, bool limit_winheight) /// @param limit_winheight when true limit to window height int plines_win_nofill(win_T *wp, linenr_T lnum, bool limit_winheight) { + if (decor_conceal_line(wp, lnum - 1, false)) { + return 0; + } + if (!wp->w_p_wrap) { return 1; } @@ -885,6 +889,11 @@ int plines_win_full(win_T *wp, linenr_T lnum, linenr_T *const nextp, bool *const if (foldedp != NULL) { *foldedp = folded; } + + if (decor_conceal_line(wp, lnum - 1, false)) { + return 0; + } + return ((folded ? 1 : plines_win_nofill(wp, lnum, limit_winheight)) + (lnum == wp->w_topline ? wp->w_topfill : win_get_fill(wp, lnum))); } @@ -963,8 +972,8 @@ int64_t win_text_height(win_T *const wp, const linenr_T start_lnum, const int64_ if (start_vcol >= 0) { linenr_T lnum_next = lnum; - const bool folded = hasFolding(wp, lnum, &lnum, &lnum_next); - height_cur_nofill = folded ? 1 : plines_win_nofill(wp, lnum, false); + hasFolding(wp, lnum, &lnum, &lnum_next); + height_cur_nofill = plines_win_nofill(wp, lnum, false); height_sum_nofill += height_cur_nofill; const int64_t row_off = (start_vcol < width1 || width2 <= 0) ? 0 @@ -975,9 +984,9 @@ int64_t win_text_height(win_T *const wp, const linenr_T start_lnum, const int64_ while (lnum <= end_lnum) { linenr_T lnum_next = lnum; - const bool folded = hasFolding(wp, lnum, &lnum, &lnum_next); + hasFolding(wp, lnum, &lnum, &lnum_next); height_sum_fill += win_get_fill(wp, lnum); - height_cur_nofill = folded ? 1 : plines_win_nofill(wp, lnum, false); + height_cur_nofill = plines_win_nofill(wp, lnum, false); height_sum_nofill += height_cur_nofill; lnum = lnum_next + 1; } diff --git a/src/nvim/window.c b/src/nvim/window.c index 938d9d7618..bd607c8830 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -6483,9 +6483,9 @@ void win_fix_scroll(bool resize) // Add difference in height and row to botline. if (diff > 0) { - cursor_down_inner(wp, diff); + cursor_down_inner(wp, diff, false); } else { - cursor_up_inner(wp, -diff); + cursor_up_inner(wp, -diff, false); } // Scroll to put the new cursor position at the bottom of the @@ -6532,11 +6532,11 @@ static void win_fix_cursor(bool normal) linenr_T lnum = wp->w_cursor.lnum; wp->w_cursor.lnum = wp->w_topline; - cursor_down_inner(wp, so); + cursor_down_inner(wp, so, false); linenr_T top = wp->w_cursor.lnum; wp->w_cursor.lnum = wp->w_botline - 1; - cursor_up_inner(wp, so); + cursor_up_inner(wp, so, false); linenr_T bot = wp->w_cursor.lnum; wp->w_cursor.lnum = lnum; @@ -6630,7 +6630,7 @@ void scroll_to_fraction(win_T *wp, int prev_height) hasFolding(wp, lnum, &lnum, NULL); if (lnum == 1) { // first line in buffer is folded - line_size = 1; + line_size = !decor_conceal_line(wp, lnum - 1, false); sline--; break; } diff --git a/test/functional/api/extmark_spec.lua b/test/functional/api/extmark_spec.lua index 49c55288e8..6d5f57742a 100644 --- a/test/functional/api/extmark_spec.lua +++ b/test/functional/api/extmark_spec.lua @@ -1552,6 +1552,7 @@ describe('API/extmarks', function() it('can get details', function() set_extmark(ns, marks[1], 0, 0, { conceal = 'c', + conceal_lines = '', cursorline_hl_group = 'Statement', end_col = 0, end_right_gravity = true, @@ -1586,6 +1587,7 @@ describe('API/extmarks', function() 0, { conceal = 'c', + conceal_lines = '', cursorline_hl_group = 'Statement', end_col = 0, end_right_gravity = true, diff --git a/test/functional/ui/decorations_spec.lua b/test/functional/ui/decorations_spec.lua index fbf16f3afe..cdee20c0c2 100644 --- a/test/functional/ui/decorations_spec.lua +++ b/test/functional/ui/decorations_spec.lua @@ -2492,6 +2492,209 @@ describe('extmark decorations', function() | ]]) end) + + it ('conceal_lines', function() + insert(example_text) + exec('set number conceallevel=3') + feed('ggj') + local not_concealed = { + grid = [[ + {2: 1 }for _,item in ipairs(items) do | + {2: 2 }^ local text, hl_id_cell, count = unpack(ite| + {2: }m) | + {2: 3 } if hl_id_cell ~= nil then | + {2: 4 } hl_id = hl_id_cell | + {2: 5 } end | + {2: 6 } for _ = 1, (count or 1) do | + {2: 7 } local cell = line[colpos] | + {2: 8 } cell.text = text | + {2: 9 } cell.hl_id = hl_id | + {2: 10 } colpos = colpos+1 | + {2: 11 } end | + {2: 12 }end | + {1:~ }| + | + ]] + } + screen:expect(not_concealed) + api.nvim_buf_set_extmark(0, ns, 1, 0, { conceal_lines = "" }) + screen:expect_unchanged() + feed('j') + local concealed = { + grid = [[ + {2: 1 }for _,item in ipairs(items) do | + {2: 3 }^ if hl_id_cell ~= nil then | + {2: 4 } hl_id = hl_id_cell | + {2: 5 } end | + {2: 6 } for _ = 1, (count or 1) do | + {2: 7 } local cell = line[colpos] | + {2: 8 } cell.text = text | + {2: 9 } cell.hl_id = hl_id | + {2: 10 } colpos = colpos+1 | + {2: 11 } end | + {2: 12 }end | + {1:~ }|*3 + | + ]] + } + screen:expect(concealed) + feed('k') + screen:expect(not_concealed) + exec('set concealcursor=n') + screen:expect(concealed) + api.nvim_buf_set_extmark(0, ns, 3, 0, { conceal_lines = "" }) + screen:expect({ + grid = [[ + {2: 1 }for _,item in ipairs(items) do | + {2: 3 }^ if hl_id_cell ~= nil then | + {2: 5 } end | + {2: 6 } for _ = 1, (count or 1) do | + {2: 7 } local cell = line[colpos] | + {2: 8 } cell.text = text | + {2: 9 } cell.hl_id = hl_id | + {2: 10 } colpos = colpos+1 | + {2: 11 } end | + {2: 12 }end | + {1:~ }|*4 + | + ]] + }) + feed('kjj') + screen:expect_unchanged() + api.nvim_buf_set_extmark(0, ns, 4, 0, { conceal_lines = "" }) + feed('kjjjC') + screen:expect({ + grid = [[ + {2: 1 }for _,item in ipairs(items) do | + {2: 3 } if hl_id_cell ~= nil then | + {2: 5 }^ | + {2: 6 } for _ = 1, (count or 1) do | + {2: 7 } local cell = line[colpos] | + {2: 8 } cell.text = text | + {2: 9 } cell.hl_id = hl_id | + {2: 10 } colpos = colpos+1 | + {2: 11 } end | + {2: 12 }end | + {1:~ }|*4 + {24:-- INSERT --} | + ]] + }) + feed('') + screen:expect({ + grid = [[ + {2: 1 }for _,item in ipairs(items) do | + {2: 3 } if hl_id_cell ~= nil then | + {2: 6 }^ for _ = 1, (count or 1) do | + {2: 7 } local cell = line[colpos] | + {2: 8 } cell.text = text | + {2: 9 } cell.hl_id = hl_id | + {2: 10 } colpos = colpos+1 | + {2: 11 } end | + {2: 12 }end | + {1:~ }|*5 + | + ]] + }) + feed('kji') + screen:expect({ + grid = [[ + {2: 1 }for _,item in ipairs(items) do | + {2: 3 } if hl_id_cell ~= nil then | + {2: 5 }^ | + {2: 6 } for _ = 1, (count or 1) do | + {2: 7 } local cell = line[colpos] | + {2: 8 } cell.text = text | + {2: 9 } cell.hl_id = hl_id | + {2: 10 } colpos = colpos+1 | + {2: 11 } end | + {2: 12 }end | + {1:~ }|*4 + {24:-- INSERT --} | + ]] + }) + feed('conceal text') + screen:expect({ + grid = [[ + {2: 1 }for _,item in ipairs(items) do | + {2: 3 } if hl_id_cell ~= nil then | + {2: 5 }conceal text^ | + {2: 6 } for _ = 1, (count or 1) do | + {2: 7 } local cell = line[colpos] | + {2: 8 } cell.text = text | + {2: 9 } cell.hl_id = hl_id | + {2: 10 } colpos = colpos+1 | + {2: 11 } end | + {2: 12 }end | + {1:~ }|*4 + {24:-- INSERT --} | + ]] + }) + feed('') + screen:expect({ + grid = [[ + {2: 1 }for _,item in ipairs(items) do | + {2: 3 } if hl_id_cell ~= nil then | + {2: 6 } for _ =^ 1, (count or 1) do | + {2: 7 } local cell = line[colpos] | + {2: 8 } cell.text = text | + {2: 9 } cell.hl_id = hl_id | + {2: 10 } colpos = colpos+1 | + {2: 11 } end | + {2: 12 }end | + {1:~ }|*5 + | + ]] + }) + feed('ggzfj') + screen:expect({ + grid = [[ + {2: 1 }{33:^+-- 2 lines: for _,item in ipairs(items) do··}| + {2: 3 } if hl_id_cell ~= nil then | + {2: 6 } for _ = 1, (count or 1) do | + {2: 7 } local cell = line[colpos] | + {2: 8 } cell.text = text | + {2: 9 } cell.hl_id = hl_id | + {2: 10 } colpos = colpos+1 | + {2: 11 } end | + {2: 12 }end | + {1:~ }|*5 + | + ]] + }) + feed('j') + screen:expect({ + grid = [[ + {2: 1 }{33:+-- 2 lines: for _,item in ipairs(items) do··}| + {2: 3 }^ if hl_id_cell ~= nil then | + {2: 6 } for _ = 1, (count or 1) do | + {2: 7 } local cell = line[colpos] | + {2: 8 } cell.text = text | + {2: 9 } cell.hl_id = hl_id | + {2: 10 } colpos = colpos+1 | + {2: 11 } end | + {2: 12 }end | + {1:~ }|*5 + | + ]] + }) + feed('ggzdjzfj') + screen:expect({ + grid = [[ + {2: 1 }for _,item in ipairs(items) do | + {2: 6 }^ for _ = 1, (count or 1) do | + {2: 7 } local cell = line[colpos] | + {2: 8 } cell.text = text | + {2: 9 } cell.hl_id = hl_id | + {2: 10 } colpos = colpos+1 | + {2: 11 } end | + {2: 12 }end | + {1:~ }|*6 + | + ]] + }) + feed('jj') + screen:expect_unchanged() + end) end) describe('decorations: inline virtual text', function() From 10298f4533039a7d9870bd6691f354fa92f3010d Mon Sep 17 00:00:00 2001 From: Luuk van Baal Date: Sun, 24 Nov 2024 14:46:20 +0100 Subject: [PATCH 2/2] feat(treesitter): vertical conceal for conceal nodes TSHighlighter now places marks for conceal_lines metadata. A new internal decor provider callback _on_conceal_line was added that instructs the highlighter to place conceal_lines marks whenever the editor needs to know whether a line is concealed. The bundled markdown queries use `conceal_lines` metadata to conceal code block fence lines. --- runtime/doc/news.txt | 2 + runtime/doc/treesitter.txt | 16 ++- runtime/lua/vim/_meta/api_keysets.lua | 1 + runtime/lua/vim/treesitter/highlighter.lua | 93 ++++++++++++--- runtime/lua/vim/treesitter/query.lua | 12 ++ runtime/queries/markdown/highlights.scm | 4 +- src/nvim/api/extmark.c | 1 + src/nvim/api/keysets_defs.h | 1 + src/nvim/decoration.c | 16 ++- src/nvim/decoration_defs.h | 1 + src/nvim/decoration_provider.c | 18 ++- src/nvim/decoration_provider.h | 1 + test/functional/treesitter/highlight_spec.lua | 107 +++++++++++++++++- 13 files changed, 241 insertions(+), 32 deletions(-) diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 072e4e2321..c8eb0a4e0e 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -291,6 +291,8 @@ TREESITTER • |treesitter-directive-trim!| can trim all whitespace (not just empty lines) from both sides of a node. • |vim.treesitter.get_captures_at_pos()| now returns the `id` of each capture +• Bundled markdown highlight queries use `conceal_lines` metadata to conceal + code block fence lines vertically. TUI diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt index a0860c60a6..d14510f73a 100644 --- a/runtime/doc/treesitter.txt +++ b/runtime/doc/treesitter.txt @@ -493,10 +493,10 @@ capture marks comments as to be checked: >query There is also `@nospell` which disables spellchecking regions with `@spell`. *treesitter-highlight-conceal* -Treesitter highlighting supports |conceal| via the `conceal` metadata. By -convention, nodes to be concealed are captured as `@conceal`, but any capture -can be used. For example, the following query can be used to hide code block -delimiters in Markdown: >query +Treesitter highlighting supports |conceal| via the `conceal` and `conceal_lines` +metadata. By convention, nodes to be concealed are captured as `@conceal`, but +any capture can be used. For example, the following query can be used to hide +code block delimiters in Markdown: >query (fenced_code_block_delimiter @conceal (#set! conceal "")) < @@ -507,7 +507,13 @@ still highlighted the same as other operators: >query "!=" @operator (#set! conceal "≠") < -Conceals specified in this way respect 'conceallevel'. +To conceal an entire line (do not draw it at all), a query with `conceal_lines` +metadata can be used: >query + + ((comment) @comment @spell + (#set! conceal_lines "")) +< +Conceals specified in this way respect 'conceallevel' and 'concealcursor'. *treesitter-highlight-priority* Treesitter uses |nvim_buf_set_extmark()| to set highlights with a default diff --git a/runtime/lua/vim/_meta/api_keysets.lua b/runtime/lua/vim/_meta/api_keysets.lua index d149a75294..3a518cc948 100644 --- a/runtime/lua/vim/_meta/api_keysets.lua +++ b/runtime/lua/vim/_meta/api_keysets.lua @@ -234,6 +234,7 @@ error('Cannot require a meta file') --- @field on_end? fun(_: "end", tick: integer) --- @field _on_hl_def? fun(_: "hl_def") --- @field _on_spell_nav? fun(_: "spell_nav") +--- @field _on_conceal_line? fun(_: "conceal_line") --- @class vim.api.keyset.set_extmark --- @field id? integer diff --git a/runtime/lua/vim/treesitter/highlighter.lua b/runtime/lua/vim/treesitter/highlighter.lua index 8ce8652f7d..e985916ac7 100644 --- a/runtime/lua/vim/treesitter/highlighter.lua +++ b/runtime/lua/vim/treesitter/highlighter.lua @@ -67,6 +67,8 @@ end --- This state is kept during rendering across each line update. ---@field private _highlight_states vim.treesitter.highlighter.State[] ---@field private _queries table +---@field _conceal_line boolean? +---@field _conceal_checked table ---@field tree vim.treesitter.LanguageTree ---@field private redraw_count integer local TSHighlighter = { @@ -114,7 +116,7 @@ function TSHighlighter.new(tree, opts) self.bufnr = source self.redraw_count = 0 - self._highlight_states = {} + self._conceal_checked = {} self._queries = {} -- Queries for a specific language can be overridden by a custom @@ -122,6 +124,7 @@ function TSHighlighter.new(tree, opts) if opts.queries then for lang, query_string in pairs(opts.queries) do self._queries[lang] = TSHighlighterQuery.new(lang, query_string) + self._conceal_line = self._conceal_line or self._queries[lang]:query().conceal_line end end @@ -140,7 +143,7 @@ function TSHighlighter.new(tree, opts) -- immediately afterwards will not error. if vim.g.syntax_on ~= 1 then vim.cmd.runtime({ 'syntax/synload.vim', bang = true }) - vim.api.nvim_create_augroup('syntaxset', { clear = false }) + api.nvim_create_augroup('syntaxset', { clear = false }) end vim._with({ buf = self.bufnr }, function() @@ -148,6 +151,7 @@ function TSHighlighter.new(tree, opts) end) self.tree:parse() + self:set_decoration_provider() return self end @@ -160,6 +164,7 @@ function TSHighlighter:destroy() if api.nvim_buf_is_loaded(self.bufnr) then vim.bo[self.bufnr].spelloptions = self.orig_spelloptions vim.b[self.bufnr].ts_highlight = nil + api.nvim_buf_clear_namespace(self.bufnr, ns, 0, -1) if vim.g.syntax_on == 1 then api.nvim_exec_autocmds('FileType', { group = 'syntaxset', buffer = self.bufnr }) end @@ -185,10 +190,14 @@ function TSHighlighter:prepare_highlight_states(srow, erow) return end - local highlighter_query = self:get_query(tree:lang()) + local hl_query = self:get_query(tree:lang()) + if hl_query:query().conceal_line and not self._conceal_line then + self._conceal_line = true + self:set_decoration_provider() + end -- Some injected languages may not have highlight queries. - if not highlighter_query:query() then + if not hl_query:query() then return end @@ -198,7 +207,7 @@ function TSHighlighter:prepare_highlight_states(srow, erow) tstree = tstree, next_row = 0, iter = nil, - highlighter_query = highlighter_query, + highlighter_query = hl_query, }) end) end @@ -220,7 +229,10 @@ end ---@param changes Range6[] function TSHighlighter:on_changedtree(changes) for _, ch in ipairs(changes) do - api.nvim__redraw({ buf = self.bufnr, range = { ch[1], ch[4] + 1 }, flush = false }) + api.nvim__redraw({ buf = self.bufnr, range = { ch[1], ch[4] }, flush = false }) + for i = ch[1], self._conceal_line and ch[4] or 0 do + self._conceal_checked[i] = false + end end end @@ -279,8 +291,10 @@ end ---@param self vim.treesitter.highlighter ---@param buf integer ---@param line integer ----@param is_spell_nav boolean -local function on_line_impl(self, buf, line, is_spell_nav) +---@param on_spell boolean +---@param on_conceal boolean +local function on_line_impl(self, buf, line, on_spell, on_conceal) + self._conceal_checked[line] = true self:for_each_highlight_state(function(state) local root_node = state.tstree:root() local root_start_row, _, root_end_row, _ = root_node:range() @@ -326,7 +340,7 @@ local function on_line_impl(self, buf, line, is_spell_nav) local url = get_url(match, buf, capture, metadata) - if hl and end_row >= line and (not is_spell_nav or spell ~= nil) then + if hl and end_row >= line and not on_conceal and (not on_spell or spell ~= nil) then api.nvim_buf_set_extmark(buf, ns, start_row, start_col, { end_line = end_row, end_col = end_col, @@ -338,6 +352,16 @@ local function on_line_impl(self, buf, line, is_spell_nav) url = url, }) end + + if + (metadata.conceal_lines or metadata[capture] and metadata[capture].conceal_lines) + and #api.nvim_buf_get_extmarks(buf, ns, { start_row, 0 }, { start_row, 0 }, {}) == 0 + then + api.nvim_buf_set_extmark(buf, ns, start_row, 0, { + end_line = end_row, + conceal_lines = '', + }) + end end if start_row > line then @@ -357,7 +381,7 @@ function TSHighlighter._on_line(_, _win, buf, line, _) return end - on_line_impl(self, buf, line, false) + on_line_impl(self, buf, line, false, false) end ---@private @@ -376,17 +400,45 @@ function TSHighlighter._on_spell_nav(_, _, buf, srow, _, erow, _) self:prepare_highlight_states(srow, erow) for row = srow, erow do - on_line_impl(self, buf, row, true) + on_line_impl(self, buf, row, true, false) end self._highlight_states = highlight_states end ---@private ----@param _win integer +---@param buf integer +---@param row integer +function TSHighlighter._on_conceal_line(_, _, buf, row) + local self = TSHighlighter.active[buf] + if not self or self._conceal_checked[row] then + return + end + + -- Do not affect potentially populated highlight state. + local highlight_states = self._highlight_states + self:prepare_highlight_states(row, row) + on_line_impl(self, buf, row, false, true) + self._highlight_states = highlight_states +end + +---@private +--- Clear conceal_lines marks whenever we redraw for a buffer change. Marks are +--- added back as either the _conceal_line or on_win callback comes across them. +function TSHighlighter._on_buf(_, buf) + local self = TSHighlighter.active[buf] + if not self then + return + end + + api.nvim_buf_clear_namespace(buf, ns, 0, -1) + self._conceal_checked = {} +end + +---@private ---@param buf integer ---@param topline integer ---@param botline integer -function TSHighlighter._on_win(_, _win, buf, topline, botline) +function TSHighlighter._on_win(_, _, buf, topline, botline) local self = TSHighlighter.active[buf] if not self then return false @@ -397,10 +449,15 @@ function TSHighlighter._on_win(_, _win, buf, topline, botline) return true end -api.nvim_set_decoration_provider(ns, { - on_win = TSHighlighter._on_win, - on_line = TSHighlighter._on_line, - _on_spell_nav = TSHighlighter._on_spell_nav, -}) +---@private +function TSHighlighter:set_decoration_provider() + api.nvim_set_decoration_provider(ns, { + on_win = TSHighlighter._on_win, + on_line = TSHighlighter._on_line, + _on_spell_nav = TSHighlighter._on_spell_nav, + _on_conceal_line = self._conceal_line and TSHighlighter._on_conceal_line or nil, + on_buf = self._conceal_line and TSHighlighter._on_buf or nil, + }) +end return TSHighlighter diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua index dbe3d54c2f..99ee27e2bb 100644 --- a/runtime/lua/vim/treesitter/query.lua +++ b/runtime/lua/vim/treesitter/query.lua @@ -11,6 +11,7 @@ local M = {} ---@field lang string name of the language for this parser ---@field captures string[] list of (unique) capture names defined in query ---@field info vim.treesitter.QueryInfo contains information used in the query (e.g. captures, predicates, directives) +---@field conceal_line boolean whether this query sets conceal_lines metadata. ---@field query TSQuery userdata query object local Query = {} Query.__index = Query @@ -30,6 +31,17 @@ function Query.new(lang, ts_query) patterns = query_info.patterns, } self.captures = self.info.captures + + -- Instruct highlighter to place marks when query contains conceal_lines metadata. + for _, preds in pairs(self.info.patterns) do + for _, pred in ipairs(preds) do + if vim.deep_equal(pred, { 'set!', 'conceal_lines', '' }) then + self.conceal_line = true + break + end + end + end + return self end diff --git a/runtime/queries/markdown/highlights.scm b/runtime/queries/markdown/highlights.scm index a12669ca2b..c1cd2ab7d7 100644 --- a/runtime/queries/markdown/highlights.scm +++ b/runtime/queries/markdown/highlights.scm @@ -49,12 +49,12 @@ (fenced_code_block (fenced_code_block_delimiter) @markup.raw.block - (#set! conceal "")) + (#set! conceal_lines "")) (fenced_code_block (info_string (language) @label - (#set! conceal ""))) + (#set! conceal_lines ""))) (link_destination) @markup.link.url diff --git a/src/nvim/api/extmark.c b/src/nvim/api/extmark.c index 93b3006781..e3d4d37032 100644 --- a/src/nvim/api/extmark.c +++ b/src/nvim/api/extmark.c @@ -1064,6 +1064,7 @@ void nvim_set_decoration_provider(Integer ns_id, Dict(set_decoration_provider) * { "on_end", &opts->on_end, &p->redraw_end }, { "_on_hl_def", &opts->_on_hl_def, &p->hl_def }, { "_on_spell_nav", &opts->_on_spell_nav, &p->spell_nav }, + { "_on_conceal_line", &opts->_on_conceal_line, &p->conceal_line }, { NULL, NULL, NULL }, }; diff --git a/src/nvim/api/keysets_defs.h b/src/nvim/api/keysets_defs.h index 147521b272..3d52f30c3e 100644 --- a/src/nvim/api/keysets_defs.h +++ b/src/nvim/api/keysets_defs.h @@ -20,6 +20,7 @@ typedef struct { LuaRefOf(("end" _, Integer tick)) on_end; LuaRefOf(("hl_def" _)) _on_hl_def; LuaRefOf(("spell_nav" _)) _on_spell_nav; + LuaRefOf(("conceal_line" _)) _on_conceal_line; } Dict(set_decoration_provider); typedef struct { diff --git a/src/nvim/decoration.c b/src/nvim/decoration.c index 91648adad8..a74ad1d669 100644 --- a/src/nvim/decoration.c +++ b/src/nvim/decoration.c @@ -12,6 +12,7 @@ #include "nvim/buffer_defs.h" #include "nvim/change.h" #include "nvim/decoration.h" +#include "nvim/decoration_provider.h" #include "nvim/drawscreen.h" #include "nvim/extmark.h" #include "nvim/fold.h" @@ -844,11 +845,17 @@ static const uint32_t conceal_filter[kMTMetaCount] = {[kMTMetaConcealLines] = kM bool decor_conceal_line(win_T *wp, int row, bool check_cursor) { - if (wp->w_p_cole < 2 || !buf_meta_total(wp->w_buffer, kMTMetaConcealLines) + if (wp->w_p_cole < 2 || (!check_cursor && ((row + 1 == wp->w_cursor.lnum) && !conceal_cursor_line(wp)))) { return false; } + size_t keys = wp->w_buffer->b_marktree->n_keys; + if (!buf_meta_total(wp->w_buffer, kMTMetaConcealLines)) { + decor_providers_invoke_conceal_line(wp, row); + return wp->w_buffer->b_marktree->n_keys > keys; + } + MTPair pair; MarkTreeIter itr[1]; marktree_itr_get_overlap(wp->w_buffer->b_marktree, row, 0, itr); @@ -870,13 +877,16 @@ bool decor_conceal_line(win_T *wp, int row, bool check_cursor) } marktree_itr_next_filter(wp->w_buffer->b_marktree, itr, row + 1, 0, conceal_filter); } - return false; + + decor_providers_invoke_conceal_line(wp, row); + return wp->w_buffer->b_marktree->n_keys > keys; } bool win_lines_concealed(win_T *wp) { return hasAnyFolding(wp) - || (wp->w_p_cole >= 2 && buf_meta_total(wp->w_buffer, kMTMetaConcealLines)); + || (wp->w_p_cole >= 2 + && (conceal_provider || buf_meta_total(wp->w_buffer, kMTMetaConcealLines))); } static const uint32_t sign_filter[kMTMetaCount] = {[kMTMetaSignText] = kMTFilterSelect, diff --git a/src/nvim/decoration_defs.h b/src/nvim/decoration_defs.h index 1973f1ce86..b289b24f45 100644 --- a/src/nvim/decoration_defs.h +++ b/src/nvim/decoration_defs.h @@ -144,6 +144,7 @@ typedef struct { LuaRef redraw_end; LuaRef hl_def; LuaRef spell_nav; + LuaRef conceal_line; int hl_valid; bool hl_cached; diff --git a/src/nvim/decoration_provider.c b/src/nvim/decoration_provider.c index e5d2658720..98fca6ccc2 100644 --- a/src/nvim/decoration_provider.c +++ b/src/nvim/decoration_provider.c @@ -30,7 +30,7 @@ static kvec_t(DecorProvider) decor_providers = KV_INITIAL_VALUE; #define DECORATION_PROVIDER_INIT(ns_id) (DecorProvider) \ { ns_id, kDecorProviderDisabled, LUA_NOREF, LUA_NOREF, \ LUA_NOREF, LUA_NOREF, LUA_NOREF, \ - LUA_NOREF, -1, false, false, 0 } + LUA_NOREF, LUA_NOREF, -1, false, false, 0 } static void decor_provider_error(DecorProvider *provider, const char *name, const char *msg) { @@ -92,6 +92,21 @@ void decor_providers_invoke_spell(win_T *wp, int start_row, int start_col, int e } } +void decor_providers_invoke_conceal_line(win_T *wp, int row) +{ + for (size_t i = 0; i < kv_size(decor_providers); i++) { + DecorProvider *p = &kv_A(decor_providers, i); + if (p->state != kDecorProviderDisabled && p->conceal_line != LUA_NOREF) { + conceal_provider = true; + MAXSIZE_TEMP_ARRAY(args, 4); + ADD_C(args, INTEGER_OBJ(wp->handle)); + ADD_C(args, INTEGER_OBJ(wp->w_buffer->handle)); + ADD_C(args, INTEGER_OBJ(row)); + decor_provider_invoke((int)i, "conceal_line", p->conceal_line, args, true); + } + } +} + /// For each provider invoke the 'start' callback /// /// @param[out] providers Decoration providers @@ -264,6 +279,7 @@ void decor_provider_clear(DecorProvider *p) NLUA_CLEAR_REF(p->redraw_line); NLUA_CLEAR_REF(p->redraw_end); NLUA_CLEAR_REF(p->spell_nav); + NLUA_CLEAR_REF(p->conceal_line); p->state = kDecorProviderDisabled; } diff --git a/src/nvim/decoration_provider.h b/src/nvim/decoration_provider.h index ad6fb7ac19..de24a719f1 100644 --- a/src/nvim/decoration_provider.h +++ b/src/nvim/decoration_provider.h @@ -7,6 +7,7 @@ #include "nvim/types_defs.h" // IWYU pragma: keep EXTERN bool provider_active INIT( = false); +EXTERN bool conceal_provider INIT( = false); #ifdef INCLUDE_GENERATED_DECLARATIONS # include "decoration_provider.h.generated.h" diff --git a/test/functional/treesitter/highlight_spec.lua b/test/functional/treesitter/highlight_spec.lua index 7f0a3cb342..1103d7238a 100644 --- a/test/functional/treesitter/highlight_spec.lua +++ b/test/functional/treesitter/highlight_spec.lua @@ -1119,14 +1119,16 @@ describe('treesitter highlighting (markdown)', function() }) end) - it('works with spellchecked and smoothscrolled topline', function() - insert([[ + local code_block = [[ - $f(0)=\sum_{k=1}^{\infty}\frac{2}{\pi^{2}k^{2}}+\lim_{w \to 0}x$. ```c printf('Hello World!'); ``` - ]]) + ]] + + it('works with spellchecked and smoothscrolled topline', function() + insert(code_block) command('set spell smoothscroll') feed('gg') screen:add_extra_attr_ids({ [100] = { undercurl = true, special = Screen.colors.Red } }) @@ -1141,6 +1143,105 @@ printf('Hello World!'); ]], }) end) + + it('works with concealed lines', function() + insert(code_block) + screen:expect({ + grid = [[ + | + {18:```}{15:c} | + {25:printf}{16:(}{26:'Hello World!'}{16:);} | + {18:```} | + ^ | + | + ]], + }) + feed('ggj') + command('set number conceallevel=3') + screen:expect({ + grid = [[ + {8: 1 }{16:- }$f(0)=\sum_{k=1}^{\infty}\frac{2}{| + {8: }\pi^{2}k^{2}}+\lim_{w \to 0}x$. | + {8: 2 }^ | + {8: 4 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8: 6 } | + | + ]], + }) + feed('j') + screen:expect({ + grid = [[ + {8: 1 }{16:- }$f(0)=\sum_{k=1}^{\infty}\frac{2}{| + {8: }\pi^{2}k^{2}}+\lim_{w \to 0}x$. | + {8: 2 } | + {8: 3 }{18:^```}{15:c} | + {8: 4 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + | + ]], + }) + feed('j') + screen:expect({ + grid = [[ + {8: 1 }{16:- }$f(0)=\sum_{k=1}^{\infty}\frac{2}{| + {8: }\pi^{2}k^{2}}+\lim_{w \to 0}x$. | + {8: 2 } | + {8: 4 }{25:^printf}{16:(}{26:'Hello World!'}{16:);} | + {8: 6 } | + | + ]], + }) + feed('j') + screen:expect({ + grid = [[ + {8: 1 }{16:- }$f(0)=\sum_{k=1}^{\infty}\frac{2}{| + {8: }\pi^{2}k^{2}}+\lim_{w \to 0}x$. | + {8: 2 } | + {8: 4 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8: 5 }{18:^```} | + | + ]], + }) + -- Concealed lines highlight until changed botline + screen:try_resize(screen._width, 16) + feed('y3k30P:') + screen:expect([[ + {8: 1 }{16:- }$f(0)=\sum_{k=1}^{\infty}\frac{2}{| + {8: }\pi^{2}k^{2}}+\lim_{w \to 0}x$. | + {8: 2 } | + {8: 4 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8: 6 } | + {8: 8 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8: 10 } | + {8: 12 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8: 14 } | + {8: 16 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8: 18 } | + {8: 20 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8: 22 } | + {8: 24 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8: 25 }{18:^```} | + | + ]]) + feed('G') + screen:expect([[ + {8: 98 } | + {8:100 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8:102 } | + {8:104 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8:106 } | + {8:108 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8:110 } | + {8:112 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8:114 } | + {8:116 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8:118 } | + {8:120 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8:122 } | + {8:124 }{25:printf}{16:(}{26:'Hello World!'}{16:);} | + {8:126 } ^ | + | + ]]) + end) end) it('starting and stopping treesitter highlight in init.lua works #29541', function()