1
0
forked from aniani/vim

patch 9.1.0099: Not able to use diff() with 'diffexpr'

Problem:  Not able to use diff() with 'diffexpr'
          (rickhowe, after v9.1.0096)
Solution: Use a default context length of 0, update diff() help text,
          add a test for using diff() with 'diffexpr'
          (Yegappan Lakshmanan)

closes: #14013

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Yegappan Lakshmanan 2024-02-12 20:21:26 +01:00 committed by Christian Brabandt
parent 6d91227267
commit a0010a186d
No known key found for this signature in database
GPG Key ID: F3F92DA383FDDE09
7 changed files with 77 additions and 24 deletions

View File

@ -1,4 +1,4 @@
*builtin.txt* For Vim version 9.1. Last change: 2024 Feb 11 *builtin.txt* For Vim version 9.1. Last change: 2024 Feb 12
VIM REFERENCE MANUAL by Bram Moolenaar VIM REFERENCE MANUAL by Bram Moolenaar
@ -2078,8 +2078,7 @@ diff({fromlist}, {tolist} [, {options}]) *diff()*
use. Supported boolean items are use. Supported boolean items are
"myers", "minimal", "patience" and "myers", "minimal", "patience" and
"histogram". "histogram".
context unified diff context length. Default context diff context length. Default is 0.
is 1.
iblank ignore changes where lines are all iblank ignore changes where lines are all
blank. blank.
icase ignore changes in case of text. icase ignore changes in case of text.
@ -2092,6 +2091,10 @@ diff({fromlist}, {tolist} [, {options}]) *diff()*
line. line.
For more information about these options, refer to 'diffopt'. For more information about these options, refer to 'diffopt'.
To compute the unified diff, all the items in {fromlist} are
concatenated into a string using a newline separator and the
same for {tolist}. The unified diff output uses line numbers.
Returns an empty List or String if {fromlist} and {tolist} are Returns an empty List or String if {fromlist} and {tolist} are
identical. identical.

View File

@ -510,9 +510,14 @@ between two Lists of strings are below.
[{'from_idx': 1, 'from_count': 2, 'to_idx': 1, 'to_count': 0}] [{'from_idx': 1, 'from_count': 2, 'to_idx': 1, 'to_count': 0}]
" disjointed changes " disjointed changes
:echo diff(['ab', 'def', 'ghi', 'jkl'], ['abc', 'def', 'ghi', 'jk'], {'output': 'indices'}) :echo diff(['ab', 'def', 'ghi', 'jkl'], ['abc', 'def', 'ghi', 'jk'], {'output': 'indices', 'context': 0})
[{'from_idx': 0, 'from_count': 1, 'to_idx': 0, 'to_count': 1}, [{'from_idx': 0, 'from_count': 1, 'to_idx': 0, 'to_count': 1},
{'from_idx': 3, 'from_count': 1, 'to_idx': 3, 'to_count': 1}] {'from_idx': 3, 'from_count': 1, 'to_idx': 3, 'to_count': 1}]
" disjointed changes with context length 1
:echo diff(['ab', 'def', 'ghi', 'jkl'], ['abc', 'def', 'ghi', 'jk'], {'output': 'indices', 'context': 1})
[{'from_idx': 0, 'from_count': 4, 'to_idx': 0, 'to_count': 4}]
< <
vim:tw=78:ts=8:noet:ft=help:norl: vim:tw=78:ts=8:noet:ft=help:norl:

View File

@ -3511,9 +3511,9 @@ parse_diff_optarg(
} }
} }
*diff_ctxlen = dict_get_number_def(d, "context", 1); *diff_ctxlen = dict_get_number_def(d, "context", 0);
if (*diff_ctxlen < 0) if (*diff_ctxlen < 0)
*diff_ctxlen = 1; *diff_ctxlen = 0;
if (dict_get_bool(d, "iblank", FALSE)) if (dict_get_bool(d, "iblank", FALSE))
*diffopts |= DIFF_IBLANK; *diffopts |= DIFF_IBLANK;

View File

@ -1952,7 +1952,7 @@ static funcentry_T global_functions[] =
{"did_filetype", 0, 0, 0, NULL, {"did_filetype", 0, 0, 0, NULL,
ret_number_bool, f_did_filetype}, ret_number_bool, f_did_filetype},
{"diff", 2, 3, FEARG_1, arg3_diff, {"diff", 2, 3, FEARG_1, arg3_diff,
ret_list_dict_any, f_diff}, ret_any, f_diff},
{"diff_filler", 1, 1, FEARG_1, arg1_lnum, {"diff_filler", 1, 1, FEARG_1, arg1_lnum,
ret_number, f_diff_filler}, ret_number, f_diff_filler},
{"diff_hlID", 2, 2, FEARG_1, arg2_lnum_number, {"diff_hlID", 2, 2, FEARG_1, arg2_lnum_number,

View File

@ -0,0 +1,12 @@
| +0#0000e05#a8a8a8255@1|-+0#4040ff13#afffff255@34||+1#0000000#ffffff0| +0#0000e05#a8a8a8255@1|z+0#0000000#5fd7ff255|e|r|o| @30
| +0#0000e05#a8a8a8255@1>o+0#0000000#ffffff0|n|e| @31||+1&&| +0#0000e05#a8a8a8255@1|o+0#0000000#ffffff0|n|e| @31
| +0#0000e05#a8a8a8255@1|t+0#0000000#ffd7ff255|w|o|x+2&#ff404010| +0&#ffd7ff255@30||+1&#ffffff0| +0#0000e05#a8a8a8255@1|t+0#0000000#ffd7ff255|w|o| @31
| +0#0000e05#a8a8a8255@1|t+0#0000000#ffffff0|h|r|e@1| @29||+1&&| +0#0000e05#a8a8a8255@1|t+0#0000000#ffffff0|h|r|e@1| @29
| +0#0000e05#a8a8a8255@1|f+0#0000000#5fd7ff255|o|u|r| @30||+1&#ffffff0| +0#0000e05#a8a8a8255@1|-+0#4040ff13#afffff255@34
|~+0&#ffffff0| @35||+1#0000000&|~+0#4040ff13&| @35
|~| @35||+1#0000000&|~+0#4040ff13&| @35
|~| @35||+1#0000000&|~+0#4040ff13&| @35
|~| @35||+1#0000000&|~+0#4040ff13&| @35
|~| @35||+1#0000000&|~+0#4040ff13&| @35
|X+3#0000000&|d|i|f@2|u|n|c|2|.|t|x|t| @4|1|,|1| @11|A|l@1| |X+1&&|d|i|f@2|u|n|c|1|.|t|x|t| @4|1|,|1| @11|A|l@1
|"+0&&|X|d|i|f@2|u|n|c|2|.|t|x|t|"| |4|L|,| |2|0|B| @50

View File

@ -1719,43 +1719,43 @@ endfunc
" Test for the diff() function " Test for the diff() function
def Test_diff_func() def Test_diff_func()
# string is added/removed/modified at the beginning # string is added/removed/modified at the beginning
assert_equal("@@ -1 +1,2 @@\n+abc\n def\n", assert_equal("@@ -0,0 +1 @@\n+abc\n",
diff(['def'], ['abc', 'def'], {output: 'unified'})) diff(['def'], ['abc', 'def'], {output: 'unified'}))
assert_equal([{from_idx: 0, from_count: 0, to_idx: 0, to_count: 1}], assert_equal([{from_idx: 0, from_count: 0, to_idx: 0, to_count: 1}],
diff(['def'], ['abc', 'def'], {output: 'indices'})) diff(['def'], ['abc', 'def'], {output: 'indices'}))
assert_equal("@@ -1,2 +1 @@\n-abc\n def\n", assert_equal("@@ -1 +0,0 @@\n-abc\n",
diff(['abc', 'def'], ['def'], {output: 'unified'})) diff(['abc', 'def'], ['def'], {output: 'unified'}))
assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 0}], assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 0}],
diff(['abc', 'def'], ['def'], {output: 'indices'})) diff(['abc', 'def'], ['def'], {output: 'indices'}))
assert_equal("@@ -1,2 +1,2 @@\n-abc\n+abx\n def\n", assert_equal("@@ -1 +1 @@\n-abc\n+abx\n",
diff(['abc', 'def'], ['abx', 'def'], {output: 'unified'})) diff(['abc', 'def'], ['abx', 'def'], {output: 'unified'}))
assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}], assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
diff(['abc', 'def'], ['abx', 'def'], {output: 'indices'})) diff(['abc', 'def'], ['abx', 'def'], {output: 'indices'}))
# string is added/removed/modified at the end # string is added/removed/modified at the end
assert_equal("@@ -1 +1,2 @@\n abc\n+def\n", assert_equal("@@ -1,0 +2 @@\n+def\n",
diff(['abc'], ['abc', 'def'], {output: 'unified'})) diff(['abc'], ['abc', 'def'], {output: 'unified'}))
assert_equal([{from_idx: 1, from_count: 0, to_idx: 1, to_count: 1}], assert_equal([{from_idx: 1, from_count: 0, to_idx: 1, to_count: 1}],
diff(['abc'], ['abc', 'def'], {output: 'indices'})) diff(['abc'], ['abc', 'def'], {output: 'indices'}))
assert_equal("@@ -1,2 +1 @@\n abc\n-def\n", assert_equal("@@ -2 +1,0 @@\n-def\n",
diff(['abc', 'def'], ['abc'], {output: 'unified'})) diff(['abc', 'def'], ['abc'], {output: 'unified'}))
assert_equal([{from_idx: 1, from_count: 1, to_idx: 1, to_count: 0}], assert_equal([{from_idx: 1, from_count: 1, to_idx: 1, to_count: 0}],
diff(['abc', 'def'], ['abc'], {output: 'indices'})) diff(['abc', 'def'], ['abc'], {output: 'indices'}))
assert_equal("@@ -1,2 +1,2 @@\n abc\n-def\n+xef\n", assert_equal("@@ -2 +2 @@\n-def\n+xef\n",
diff(['abc', 'def'], ['abc', 'xef'], {output: 'unified'})) diff(['abc', 'def'], ['abc', 'xef'], {output: 'unified'}))
assert_equal([{from_idx: 1, from_count: 1, to_idx: 1, to_count: 1}], assert_equal([{from_idx: 1, from_count: 1, to_idx: 1, to_count: 1}],
diff(['abc', 'def'], ['abc', 'xef'], {output: 'indices'})) diff(['abc', 'def'], ['abc', 'xef'], {output: 'indices'}))
# string is added/removed/modified in the middle # string is added/removed/modified in the middle
assert_equal("@@ -2,2 +2,3 @@\n 222\n+xxx\n 333\n", assert_equal("@@ -2,0 +3 @@\n+xxx\n",
diff(['111', '222', '333'], ['111', '222', 'xxx', '333'], {output: 'unified'})) diff(['111', '222', '333'], ['111', '222', 'xxx', '333'], {output: 'unified'}))
assert_equal([{from_idx: 2, from_count: 0, to_idx: 2, to_count: 1}], assert_equal([{from_idx: 2, from_count: 0, to_idx: 2, to_count: 1}],
diff(['111', '222', '333'], ['111', '222', 'xxx', '333'], {output: 'indices'})) diff(['111', '222', '333'], ['111', '222', 'xxx', '333'], {output: 'indices'}))
assert_equal("@@ -2,3 +2,2 @@\n 222\n-333\n 444\n", assert_equal("@@ -3 +2,0 @@\n-333\n",
diff(['111', '222', '333', '444'], ['111', '222', '444'], {output: 'unified'})) diff(['111', '222', '333', '444'], ['111', '222', '444'], {output: 'unified'}))
assert_equal([{from_idx: 2, from_count: 1, to_idx: 2, to_count: 0}], assert_equal([{from_idx: 2, from_count: 1, to_idx: 2, to_count: 0}],
diff(['111', '222', '333', '444'], ['111', '222', '444'], {output: 'indices'})) diff(['111', '222', '333', '444'], ['111', '222', '444'], {output: 'indices'}))
assert_equal("@@ -2,3 +2,3 @@\n 222\n-333\n+xxx\n 444\n", assert_equal("@@ -3 +3 @@\n-333\n+xxx\n",
diff(['111', '222', '333', '444'], ['111', '222', 'xxx', '444'], {output: 'unified'})) diff(['111', '222', '333', '444'], ['111', '222', 'xxx', '444'], {output: 'unified'}))
assert_equal([{from_idx: 2, from_count: 1, to_idx: 2, to_count: 1}], assert_equal([{from_idx: 2, from_count: 1, to_idx: 2, to_count: 1}],
diff(['111', '222', '333', '444'], ['111', '222', 'xxx', '444'], {output: 'indices'})) diff(['111', '222', '333', '444'], ['111', '222', 'xxx', '444'], {output: 'indices'}))
@ -1795,10 +1795,12 @@ def Test_diff_func()
diff(['abcd'], ['abc'], {output: 'unified'})) diff(['abcd'], ['abc'], {output: 'unified'}))
assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}], assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
diff(['abcd'], ['abc'], {output: 'indices'})) diff(['abcd'], ['abc'], {output: 'indices'}))
assert_equal("@@ -1 +1 @@\n-abc\n+abx\n", var diff_unified: string = diff(['abc'], ['abx'], {output: 'unified'})
diff(['abc'], ['abx'], {output: 'unified'})) assert_equal("@@ -1 +1 @@\n-abc\n+abx\n", diff_unified)
var diff_indices: list<dict<number>> =
diff(['abc'], ['abx'], {output: 'indices'})
assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}], assert_equal([{from_idx: 0, from_count: 1, to_idx: 0, to_count: 1}],
diff(['abc'], ['abx'], {output: 'indices'})) diff_indices)
# partial string modification at the start and at the end. # partial string modification at the start and at the end.
var fromlist =<< trim END var fromlist =<< trim END
@ -1825,17 +1827,18 @@ def Test_diff_func()
three four three four
five six five six
END END
assert_equal("@@ -1,3 +1,3 @@\n-one two\n+one abc two\n three four\n-five abc six\n+five six\n", assert_equal("@@ -1 +1 @@\n-one two\n+one abc two\n@@ -3 +3 @@\n-five abc six\n+five six\n",
diff(fromlist, tolist, {output: 'unified'})) diff(fromlist, tolist, {output: 'unified'}))
assert_equal([{from_idx: 0, from_count: 3, to_idx: 0, to_count: 3}], assert_equal([{'from_count': 1, 'to_idx': 0, 'to_count': 1, 'from_idx': 0},
{'from_count': 1, 'to_idx': 2, 'to_count': 1, 'from_idx': 2}],
diff(fromlist, tolist, {output: 'indices'})) diff(fromlist, tolist, {output: 'indices'}))
# add/remove blank lines # add/remove blank lines
assert_equal("@@ -1,4 +1,2 @@\n one\n-\n-\n two\n", assert_equal("@@ -2,2 +1,0 @@\n-\n-\n",
diff(['one', '', '', 'two'], ['one', 'two'], {output: 'unified'})) diff(['one', '', '', 'two'], ['one', 'two'], {output: 'unified'}))
assert_equal([{from_idx: 1, from_count: 2, to_idx: 1, to_count: 0}], assert_equal([{from_idx: 1, from_count: 2, to_idx: 1, to_count: 0}],
diff(['one', '', '', 'two'], ['one', 'two'], {output: 'indices'})) diff(['one', '', '', 'two'], ['one', 'two'], {output: 'indices'}))
assert_equal("@@ -1,2 +1,4 @@\n one\n+\n+\n two\n", assert_equal("@@ -1,0 +2,2 @@\n+\n+\n",
diff(['one', 'two'], ['one', '', '', 'two'], {output: 'unified'})) diff(['one', 'two'], ['one', '', '', 'two'], {output: 'unified'}))
assert_equal([{'from_idx': 1, 'from_count': 0, 'to_idx': 1, 'to_count': 2}], assert_equal([{'from_idx': 1, 'from_count': 0, 'to_idx': 1, 'to_count': 2}],
diff(['one', 'two'], ['one', '', '', 'two'], {output: 'indices'})) diff(['one', 'two'], ['one', '', '', 'two'], {output: 'indices'}))
@ -1911,7 +1914,7 @@ def Test_diff_func()
diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 3})) diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 3}))
assert_equal("@@ -1,3 +1,4 @@\n+x\n a\n b\n c\n", assert_equal("@@ -1,3 +1,4 @@\n+x\n a\n b\n c\n",
diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 4})) diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: 4}))
assert_equal("@@ -1 +1,2 @@\n+x\n a\n", assert_equal("@@ -0,0 +1 @@\n+x\n",
diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: -1})) diff(['a', 'b', 'c'], ['x', 'a', 'b', 'c'], {context: -1}))
# Error cases # Error cases
@ -1922,4 +1925,32 @@ def Test_diff_func()
assert_fails('call diff(["a"], ["a"], {context: []})', 'E745: Using a List as a Number') assert_fails('call diff(["a"], ["a"], {context: []})', 'E745: Using a List as a Number')
enddef enddef
" Test for using the diff() function with 'diffexpr'
func Test_diffexpr_with_diff_func()
CheckScreendump
let lines =<< trim END
def DiffFuncExpr()
var in: list<string> = readfile(v:fname_in)
var new: list<string> = readfile(v:fname_new)
var out: string = diff(in, new)
writefile(split(out, "\n"), v:fname_out)
enddef
set diffexpr=DiffFuncExpr()
edit Xdifffunc1.txt
diffthis
vert split Xdifffunc2.txt
diffthis
END
call writefile(lines, 'XsetupDiffFunc.vim', 'D')
call writefile(['zero', 'one', 'two', 'three'], 'Xdifffunc1.txt', 'D')
call writefile(['one', 'twox', 'three', 'four'], 'Xdifffunc2.txt', 'D')
let buf = RunVimInTerminal('-S XsetupDiffFunc.vim', {'rows': 12})
call VerifyScreenDump(buf, 'Test_difffunc_diffexpr_1', {})
call StopVimInTerminal(buf)
endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab

View File

@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
99,
/**/ /**/
98, 98,
/**/ /**/