diff options
author | zeertzjq <zeertzjq@outlook.com> | 2022-11-12 07:43:36 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-12 07:43:36 +0800 |
commit | eee956051637a5dff02ba6c6083fbffc87c0c96e (patch) | |
tree | bd08a4983f10ffe914ce0c668e1d7b4a5ed4337d | |
parent | 0d7cc5ee85c2722ab68e276a2bfda336ef9429d5 (diff) | |
download | rneovim-eee956051637a5dff02ba6c6083fbffc87c0c96e.tar.gz rneovim-eee956051637a5dff02ba6c6083fbffc87c0c96e.tar.bz2 rneovim-eee956051637a5dff02ba6c6083fbffc87c0c96e.zip |
vim-patch:9.0.0861: solution for "!!sort" in closed fold is not optimal (#21027)
Problem: Solution for "!!sort" in closed fold is not optimal.
Solution: Use a different range instead of the subtle difference in handling
a range with an offset. (issue vim/vim#11487)
https://github.com/vim/vim/commit/9954dc39ea090cee6bf41c888c41e60d9f52c3b8
Co-authored-by: Bram Moolenaar <Bram@vim.org>
-rw-r--r-- | runtime/doc/cmdline.txt | 42 | ||||
-rw-r--r-- | src/nvim/ex_docmd.c | 21 | ||||
-rw-r--r-- | src/nvim/ops.c | 13 | ||||
-rw-r--r-- | src/nvim/testdir/test_fold.vim | 48 |
4 files changed, 104 insertions, 20 deletions
diff --git a/runtime/doc/cmdline.txt b/runtime/doc/cmdline.txt index c7ffa920b6..b4923b0d70 100644 --- a/runtime/doc/cmdline.txt +++ b/runtime/doc/cmdline.txt @@ -690,7 +690,9 @@ Line numbers may be specified with: *:range* *{address}* 'T position of mark T (uppercase); when the mark is in another file it cannot be used in a range /{pattern}[/] the next line where {pattern} matches *:/* + also see |:range-pattern| below ?{pattern}[?] the previous line where {pattern} matches *:?* + also see |:range-pattern| below \/ the next line where the previously used search pattern matches \? the previous line where the previously used search @@ -698,11 +700,49 @@ Line numbers may be specified with: *:range* *{address}* \& the next line where the previously used substitute pattern matches + *:range-offset* Each may be followed (several times) by '+' or '-' and an optional number. This number is added or subtracted from the preceding line number. If the number is omitted, 1 is used. If there is nothing before the '+' or '-' then the current line is used. - + *:range-closed-fold* +When a line number after the comma is in a closed fold it is adjusted to the +last line of the fold, thus the whole fold is included. + +When a number is added this is done after the adjustment to the last line of +the fold. This means these lines are additionally included in the range. For +example: > + :3,4+2print +On this text: + 1 one ~ + 2 two ~ + 3 three ~ + 4 four FOLDED ~ + 5 five FOLDED ~ + 6 six ~ + 7 seven ~ + 8 eight ~ +Where lines four and five are a closed fold, ends up printing lines 3 to 7. +The 7 comes from the "4" in the range, which is adjusted to the end of the +closed fold, which is 5, and then the offset 2 is added. + +An example for subtracting (which isn't very useful): > + :2,4-1print +On this text: + 1 one ~ + 2 two ~ + 3 three FOLDED~ + 4 four FOLDED ~ + 5 five FOLDED ~ + 6 six FOLDED ~ + 7 seven ~ + 8 eight ~ +Where lines three to six are a closed fold, ends up printing lines 2 to 6. +The 6 comes from the "4" in the range, which is adjusted to the end of the +closed fold, which is 6, and then 1 is subtracted, then this is still in the +closed fold and the last line of that fold is used, which is 6. + + *:range-pattern* The "/" and "?" after {pattern} are required to separate the pattern from anything that follows. diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index f4100272e1..b875aafaad 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -3224,8 +3224,6 @@ static linenr_T get_address(exarg_T *eap, char **ptr, cmd_addr_T addr_type, int char *cmd = skipwhite(*ptr); linenr_T lnum = MAXLNUM; do { - const int base_char = (uint8_t)(*cmd); - switch (*cmd) { case '.': // '.' - Cursor position cmd++; @@ -3502,16 +3500,10 @@ static linenr_T get_address(exarg_T *eap, char **ptr, cmd_addr_T addr_type, int } else if (addr_type == ADDR_LOADED_BUFFERS || addr_type == ADDR_BUFFERS) { lnum = compute_buffer_local_count(addr_type, lnum, (i == '-') ? -1 * n : n); } else { - // Relative line addressing: need to adjust for closed folds - // after the first address. - // Subtle difference: "number,+number" and "number,-number" - // adjusts to end of closed fold before adding/subtracting, - // while "number,.+number" adjusts to end of closed fold after - // adding to make "!!" expanded into ".,.+N" work correctly. - bool adjust_for_folding = addr_type == ADDR_LINES - && (i == '-' || i == '+') - && address_count >= 2; - if (adjust_for_folding && (i == '-' || base_char != '.')) { + // Relative line addressing: need to adjust for lines in a + // closed fold after the first address. + if (addr_type == ADDR_LINES && (i == '-' || i == '+') + && address_count >= 2) { (void)hasFolding(lnum, NULL, &lnum); } if (i == '-') { @@ -3522,11 +3514,6 @@ static linenr_T get_address(exarg_T *eap, char **ptr, cmd_addr_T addr_type, int goto error; } lnum += n; - // ".+number" rounds up to the end of a closed fold after - // adding, so that ":!!sort" sorts one closed fold. - if (adjust_for_folding && base_char == '.') { - (void)hasFolding(lnum, NULL, &lnum); - } } } } diff --git a/src/nvim/ops.c b/src/nvim/ops.c index 65fc42bc08..8656b4265d 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -5555,13 +5555,22 @@ static void op_colon(oparg_T *oap) } else { stuffnumReadbuff((long)oap->start.lnum); } - if (oap->end.lnum != oap->start.lnum) { + + // When using !! on a closed fold the range ".!" works best to operate + // on, it will be made the whole closed fold later. + linenr_T endOfStartFold = oap->start.lnum; + (void)hasFolding(oap->start.lnum, NULL, &endOfStartFold); + if (oap->end.lnum != oap->start.lnum && oap->end.lnum != endOfStartFold) { + // Make it a range with the end line. stuffcharReadbuff(','); if (oap->end.lnum == curwin->w_cursor.lnum) { stuffcharReadbuff('.'); } else if (oap->end.lnum == curbuf->b_ml.ml_line_count) { stuffcharReadbuff('$'); - } else if (oap->start.lnum == curwin->w_cursor.lnum) { + } else if (oap->start.lnum == curwin->w_cursor.lnum + // do not use ".+number" for a closed fold, it would count + // folded lines twice + && !hasFolding(oap->end.lnum, NULL, NULL)) { stuffReadbuff(".+"); stuffnumReadbuff(oap->line_count - 1); } else { diff --git a/src/nvim/testdir/test_fold.vim b/src/nvim/testdir/test_fold.vim index 1ee9165308..1b979dbc03 100644 --- a/src/nvim/testdir/test_fold.vim +++ b/src/nvim/testdir/test_fold.vim @@ -72,6 +72,54 @@ func Test_address_fold() quit! endfunc +func Test_address_offsets() + " check the help for :range-closed-fold + enew + call setline(1, [ + \ '1 one', + \ '2 two', + \ '3 three', + \ '4 four FOLDED', + \ '5 five FOLDED', + \ '6 six', + \ '7 seven', + \ '8 eight', + \]) + set foldmethod=manual + normal 4Gvjzf + 3,4+2yank + call assert_equal([ + \ '3 three', + \ '4 four FOLDED', + \ '5 five FOLDED', + \ '6 six', + \ '7 seven', + \ ], getreg(0,1,1)) + + enew! + call setline(1, [ + \ '1 one', + \ '2 two', + \ '3 three FOLDED', + \ '4 four FOLDED', + \ '5 five FOLDED', + \ '6 six FOLDED', + \ '7 seven', + \ '8 eight', + \]) + normal 3Gv3jzf + 2,4-1yank + call assert_equal([ + \ '2 two', + \ '3 three FOLDED', + \ '4 four FOLDED', + \ '5 five FOLDED', + \ '6 six FOLDED', + \ ], getreg(0,1,1)) + + bwipe! +endfunc + func Test_indent_fold() new call setline(1, ['', 'a', ' b', ' c']) |