From 8db55aedb52883f4b024ddd05524e18bc93d5c0b Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 23 Apr 2022 06:55:24 +0800 Subject: [PATCH 1/2] vim-patch:8.2.4805: CurSearch used for all matches in current line Problem: CurSearch used for all matches in current line. Solution: Don't use the non-zero line count. (closes vim/vim#10247) https://github.com/vim/vim/commit/9b36750640e8e89f18afa1446ed80fdbdf0fcac0 --- src/nvim/match.c | 35 ++++++++++++++++++++-------- src/nvim/testdir/test_search.vim | 11 +++++++-- test/functional/ui/searchhl_spec.lua | 29 +++++++++++++++++++---- 3 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/nvim/match.c b/src/nvim/match.c index f6d2fe13c4..c946ed5e02 100644 --- a/src/nvim/match.c +++ b/src/nvim/match.c @@ -560,6 +560,22 @@ void prepare_search_hl(win_T *wp, match_T *search_hl, linenr_T lnum) } } +/// Update "shl->has_cursor" based on the match in "shl" and the cursor +/// position. +static void check_cur_search_hl(win_T *wp, match_T *shl) +{ + long linecount = shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum; + + if (wp->w_cursor.lnum >= shl->lnum + && wp->w_cursor.lnum <= shl->lnum + shl->rm.endpos[0].lnum + && (wp->w_cursor.lnum > shl->lnum || wp->w_cursor.col >= shl->rm.startpos[0].col) + && (wp->w_cursor.lnum < shl->lnum + linecount || wp->w_cursor.col < shl->rm.endpos[0].col)) { + shl->has_cursor = true; + } else { + shl->has_cursor = false; + } +} + /// Prepare for 'hlsearch' and match highlighting in one window line. /// Return true if there is such highlighting and set "search_attr" to the /// current highlight attribute. @@ -609,20 +625,14 @@ bool prepare_search_hl_line(win_T *wp, linenr_T lnum, colnr_T mincol, char_u **l } else { shl->endcol = MAXCOL; } - if (shl->rm.endpos[0].lnum != shl->rm.startpos[0].lnum) { - shl->lines = shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum; - } else { + shl->lines = shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum; + if (shl->lines == 0) { shl->lines = 1; } // check if the cursor is in the match before changing the columns - if (wp->w_cursor.lnum >= shl->lnum - && wp->w_cursor.lnum <= shl->lnum + shl->rm.endpos[0].lnum - && (wp->w_cursor.lnum > shl->lnum - || wp->w_cursor.col >= shl->rm.startpos[0].col) - && (wp->w_cursor.lnum < shl->lnum + shl->lines - || wp->w_cursor.col < shl->rm.endpos[0].col)) { - shl->has_cursor = true; + if (shl == search_hl) { + check_cur_search_hl(wp, shl); } // Highlight one character for an empty match. @@ -723,6 +733,11 @@ int update_search_hl(win_T *wp, linenr_T lnum, colnr_T col, char_u **line, match shl->endcol = MAXCOL; } + // check if the cursor is in the match + if (shl == search_hl) { + check_cur_search_hl(wp, shl); + } + if (shl->startcol == shl->endcol) { // highlight empty match, try again after it shl->endcol += utfc_ptr2len(*line + shl->endcol); diff --git a/src/nvim/testdir/test_search.vim b/src/nvim/testdir/test_search.vim index 24ecc58281..d359e69f91 100644 --- a/src/nvim/testdir/test_search.vim +++ b/src/nvim/testdir/test_search.vim @@ -951,7 +951,7 @@ func Test_hlsearch_cursearch() let lines =<< trim END set hlsearch scrolloff=0 - call setline(1, ['one', 'foo', 'bar', 'baz', 'foo', 'bar']) + call setline(1, ['one', 'foo', 'bar', 'baz', 'foo the foo and foo', 'bar']) hi Search ctermbg=yellow hi CurSearch ctermbg=blue END @@ -964,7 +964,14 @@ func Test_hlsearch_cursearch() call term_sendkeys(buf, "n") call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2', {}) - call term_sendkeys(buf, "?\") + call term_sendkeys(buf, "n") + call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2a', {}) + + call term_sendkeys(buf, "n") + call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_2b', {}) + + call term_sendkeys(buf, ":call setline(5, 'foo')\") + call term_sendkeys(buf, "0?\") call VerifyScreenDump(buf, 'Test_hlsearch_cursearch_single_line_3', {}) call term_sendkeys(buf, "gg/foo\\nbar\") diff --git a/test/functional/ui/searchhl_spec.lua b/test/functional/ui/searchhl_spec.lua index 84dc3c59bb..56ff8a4101 100644 --- a/test/functional/ui/searchhl_spec.lua +++ b/test/functional/ui/searchhl_spec.lua @@ -163,14 +163,14 @@ describe('search highlighting', function() end) it('works for multiline match', function() - command([[call setline(1, ['one', 'foo', 'bar', 'baz', 'foo', 'bar'])]]) + command([[call setline(1, ['one', 'foo', 'bar', 'baz', 'foo the foo and foo', 'bar'])]]) feed('gg/foo') screen:expect([[ one | {2:^foo} | bar | baz | - {1:foo} | + {1:foo} the {1:foo} and {1:foo} | bar | /foo | ]]) @@ -180,11 +180,32 @@ describe('search highlighting', function() {1:foo} | bar | baz | - {2:^foo} | + {2:^foo} the {1:foo} and {1:foo} | bar | /foo | ]]) - feed('?') + feed('n') + screen:expect([[ + one | + {1:foo} | + bar | + baz | + {1:foo} the {2:^foo} and {1:foo} | + bar | + /foo | + ]]) + feed('n') + screen:expect([[ + one | + {1:foo} | + bar | + baz | + {1:foo} the {1:foo} and {2:^foo} | + bar | + /foo | + ]]) + command([[call setline(5, 'foo')]]) + feed('0?') screen:expect([[ one | {2:^foo} | From 79ab277011e4668e2a6368be8ed908dc572af80f Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 23 Apr 2022 19:23:41 +0800 Subject: [PATCH 2/2] vim-patch:8.2.4812: unused struct item Problem: Unused struct item. Solution: Remove "lines" match_T. Simplify the code. (closes vim/vim#10256) https://github.com/vim/vim/commit/8279cfe49961b3711c84c66a9954c9f70e9b78c8 --- src/nvim/buffer_defs.h | 1 - src/nvim/match.c | 9 ++------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index c16a9c0282..0a1c92a9a4 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -1020,7 +1020,6 @@ typedef struct { // match (may continue in next line) buf_T *buf; // the buffer to search for a match linenr_T lnum; // the line to search for a match - linenr_T lines; // number of lines starting from lnum int attr; // attributes to be used for a match int attr_cur; // attributes currently active in win_line() linenr_T first_lnum; // first lnum to search for multi-line pat diff --git a/src/nvim/match.c b/src/nvim/match.c index c946ed5e02..ed320eb6fc 100644 --- a/src/nvim/match.c +++ b/src/nvim/match.c @@ -564,10 +564,10 @@ void prepare_search_hl(win_T *wp, match_T *search_hl, linenr_T lnum) /// position. static void check_cur_search_hl(win_T *wp, match_T *shl) { - long linecount = shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum; + linenr_T linecount = shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum; if (wp->w_cursor.lnum >= shl->lnum - && wp->w_cursor.lnum <= shl->lnum + shl->rm.endpos[0].lnum + && wp->w_cursor.lnum <= shl->lnum + linecount && (wp->w_cursor.lnum > shl->lnum || wp->w_cursor.col >= shl->rm.startpos[0].col) && (wp->w_cursor.lnum < shl->lnum + linecount || wp->w_cursor.col < shl->rm.endpos[0].col)) { shl->has_cursor = true; @@ -599,7 +599,6 @@ bool prepare_search_hl_line(win_T *wp, linenr_T lnum, colnr_T mincol, char_u **l } shl->startcol = MAXCOL; shl->endcol = MAXCOL; - shl->lines = 0; shl->attr_cur = 0; shl->is_addpos = false; shl->has_cursor = false; @@ -625,10 +624,6 @@ bool prepare_search_hl_line(win_T *wp, linenr_T lnum, colnr_T mincol, char_u **l } else { shl->endcol = MAXCOL; } - shl->lines = shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum; - if (shl->lines == 0) { - shl->lines = 1; - } // check if the cursor is in the match before changing the columns if (shl == search_hl) {