From 290bb4c64bdcc475c29b857dc8626f5c51aa2b8d Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 3 Feb 2025 21:52:53 +0800 Subject: vim-patch:9.1.1009: diff feature can be improved Problem: diff feature can be improved Solution: include the linematch diff alignment algorithm (Jonathon) closes: vim/vim#9661 https://github.com/vim/vim/commit/7c7a4e6d1ad50d5b25b42aa2d5a33a8d04a4cc8a Co-authored-by: Jonathon --- src/nvim/diff.c | 22 ++++++++--- src/nvim/options.lua | 102 +++++++++++++++++++++++++-------------------------- 2 files changed, 68 insertions(+), 56 deletions(-) (limited to 'src/nvim') diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 99f70793b3..4ead58c74f 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -1820,7 +1820,8 @@ static void find_top_diff_block(diff_T **thistopdiff, diff_T **nextblockblock, i topdiffchange = 0; } - // check if the fromwin topline is matched by the current diff. if so, set it to the top of the diff block + // check if the fromwin topline is matched by the current diff. if so, + // set it to the top of the diff block if (topline >= topdiff->df_lnum[fromidx] && topline <= (topdiff->df_lnum[fromidx] + topdiff->df_count[fromidx])) { // this line is inside the current diff block, so we will save the @@ -2021,10 +2022,15 @@ static void run_linematch_algorithm(diff_T *dp) size_t ndiffs = 0; for (int i = 0; i < DB_COUNT; i++) { if (curtab->tp_diffbuf[i] != NULL) { - // write the contents of the entire buffer to - // diffbufs_mm[diffbuffers_count] - diff_write_buffer(curtab->tp_diffbuf[i], &diffbufs_mm[ndiffs], - dp->df_lnum[i], dp->df_lnum[i] + dp->df_count[i] - 1); + if (dp->df_count[i] > 0) { + // write the contents of the entire buffer to + // diffbufs_mm[diffbuffers_count] + diff_write_buffer(curtab->tp_diffbuf[i], &diffbufs_mm[ndiffs], + dp->df_lnum[i], dp->df_lnum[i] + dp->df_count[i] - 1); + } else { + diffbufs_mm[ndiffs].size = 0; + diffbufs_mm[ndiffs].ptr = NULL; + } diffbufs[ndiffs] = &diffbufs_mm[ndiffs]; @@ -2060,6 +2066,12 @@ static void run_linematch_algorithm(diff_T *dp) /// Returns > 0 for inserting that many filler lines above it (never happens /// when 'diffopt' doesn't contain "filler"). /// This should only be used for windows where 'diff' is set. +/// When diffopt contains linematch, a changed/added/deleted line +/// may also have filler lines above it. In such a case, the possibilities +/// are no longer mutually exclusive. The number of filler lines is +/// returned from diff_check, and the integer 'linestatus' passed by +/// pointer is set to -1 to indicate a changed line, and -2 to indicate an +/// added line /// /// @param wp /// @param lnum diff --git a/src/nvim/options.lua b/src/nvim/options.lua index fdd5799b46..4406ae28a8 100644 --- a/src/nvim/options.lua +++ b/src/nvim/options.lua @@ -2178,11 +2178,20 @@ local options = { Option settings for diff mode. It can consist of the following items. All are optional. Items must be separated by a comma. - filler Show filler lines, to keep the text - synchronized with a window that has inserted - lines at the same position. Mostly useful - when windows are side-by-side and 'scrollbind' - is set. + algorithm:{text} Use the specified diff algorithm with the + internal diff engine. Currently supported + algorithms are: + myers the default algorithm + minimal spend extra time to generate the + smallest possible diff + patience patience diff algorithm + histogram histogram diff algorithm + + closeoff When a window is closed where 'diff' is set + and there is only one window remaining in the + same tab page with 'diff' set, execute + `:diffoff` in that window. This undoes a + `:diffsplit` command. context:{n} Use a context of {n} lines between a change and a fold that contains unchanged lines. @@ -2193,6 +2202,23 @@ local options = { value (999999) to disable folding completely. See |fold-diff|. + filler Show filler lines, to keep the text + synchronized with a window that has inserted + lines at the same position. Mostly useful + when windows are side-by-side and 'scrollbind' + is set. + + foldcolumn:{n} Set the 'foldcolumn' option to {n} when + starting diff mode. Without this 2 is used. + + followwrap Follow the 'wrap' option and leave as it is. + + horizontal Start diff mode with horizontal splits (unless + explicitly specified otherwise). + + hiddenoff Do not use diff mode for a buffer when it + becomes hidden. + iblank Ignore changes where lines are all blank. Adds the "-B" flag to the "diff" command if 'diffexpr' is empty. Check the documentation @@ -2206,6 +2232,17 @@ local options = { are considered the same. Adds the "-i" flag to the "diff" command if 'diffexpr' is empty. + indent-heuristic + Use the indent heuristic for the internal + diff library. + + internal Use the internal diff library. This is + ignored when 'diffexpr' is set. *E960* + When running out of memory when writing a + buffer this item will be ignored for diffs + involving that buffer. Set the 'verbose' + option to see when this happens. + iwhite Ignore changes in amount of white space. Adds the "-b" flag to the "diff" command if 'diffexpr' is empty. Check the documentation @@ -2225,56 +2262,19 @@ local options = { of the "diff" command for what this does exactly. - horizontal Start diff mode with horizontal splits (unless - explicitly specified otherwise). + linematch:{n} Align and mark changes between the most + similar lines between the buffers. When the + total number of lines in the diff hunk exceeds + {n}, the lines will not be aligned because for + very large diff hunks there will be a + noticeable lag. A reasonable setting is + "linematch:60", as this will enable alignment + for a 2 buffer diff hunk of 30 lines each, + or a 3 buffer diff hunk of 20 lines each. vertical Start diff mode with vertical splits (unless explicitly specified otherwise). - closeoff When a window is closed where 'diff' is set - and there is only one window remaining in the - same tab page with 'diff' set, execute - `:diffoff` in that window. This undoes a - `:diffsplit` command. - - hiddenoff Do not use diff mode for a buffer when it - becomes hidden. - - foldcolumn:{n} Set the 'foldcolumn' option to {n} when - starting diff mode. Without this 2 is used. - - followwrap Follow the 'wrap' option and leave as it is. - - internal Use the internal diff library. This is - ignored when 'diffexpr' is set. *E960* - When running out of memory when writing a - buffer this item will be ignored for diffs - involving that buffer. Set the 'verbose' - option to see when this happens. - - indent-heuristic - Use the indent heuristic for the internal - diff library. - - linematch:{n} Enable a second stage diff on each generated - hunk in order to align lines. When the total - number of lines in a hunk exceeds {n}, the - second stage diff will not be performed as - very large hunks can cause noticeable lag. A - recommended setting is "linematch:60", as this - will enable alignment for a 2 buffer diff with - hunks of up to 30 lines each, or a 3 buffer - diff with hunks of up to 20 lines each. - - algorithm:{text} Use the specified diff algorithm with the - internal diff engine. Currently supported - algorithms are: - myers the default algorithm - minimal spend extra time to generate the - smallest possible diff - patience patience diff algorithm - histogram histogram diff algorithm - Examples: >vim set diffopt=internal,filler,context:4 set diffopt= -- cgit From 1c3bda7e92f6162800bc013851c8570800026420 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 4 Feb 2025 08:25:10 +0800 Subject: vim-patch:9.1.1022: linematch option value not completed Problem: linematch option value not completed (after v9.1.1009) Solution: Update diffoption completion values related: vim/vim#9661 closes: vim/vim#16437 https://github.com/vim/vim/commit/9162e636b31dcac57876cbdec15a683cedd9760e Co-authored-by: Christian Brabandt --- src/nvim/options.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim') diff --git a/src/nvim/options.lua b/src/nvim/options.lua index 4406ae28a8..c61c6032c4 100644 --- a/src/nvim/options.lua +++ b/src/nvim/options.lua @@ -2170,8 +2170,8 @@ local options = { 'followwrap', 'internal', 'indent-heuristic', - 'linematch:', { 'algorithm:', { 'myers', 'minimal', 'patience', 'histogram' } }, + 'linematch:', }, deny_duplicates = true, desc = [=[ -- cgit From 4d0c6cae72716eb88d74361c49dc8baefba22c63 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 3 Feb 2025 22:26:05 +0800 Subject: vim-patch:9.1.1023: Coverity complains about dereferencing NULL pointer Problem: Coverity complains about dereferencing NULL pointer Solution: Verify curdiff is not null before dereferencing it closes: vim/vim#16437 https://github.com/vim/vim/commit/a9f77be9223f8b886d89f7fac778d363586beb85 Co-authored-by: Christian Brabandt --- src/nvim/diff.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/nvim') diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 4ead58c74f..ee063a330e 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -1866,8 +1866,10 @@ static void count_filler_lines_and_topline(int *curlinenum_to, int *linesfiller, } } else { (*linesfiller) = 0; - ch_virtual_lines = get_max_diff_length(curdif); - isfiller = (curdif->df_count[toidx] ? false : true); + if (curdif) { + ch_virtual_lines = get_max_diff_length(curdif); + isfiller = (curdif->df_count[toidx] ? false : true); + } if (isfiller) { while (curdif && curdif->df_next && curdif->df_lnum[toidx] == curdif->df_next->df_lnum[toidx] -- cgit From bd145a6c8398fb7a3fd037bc71c1bacaeba49584 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 3 Feb 2025 22:26:32 +0800 Subject: vim-patch:9.1.1027: no sanitize check when running linematch Problem: no sanitize check when running linematch Solution: add sanitize check before applying the linematch algorithm, similar to diff_find_change() (Jonathon) closes: vim/vim#16446 https://github.com/vim/vim/commit/ca307efe486670b76563a4a287bc94dace57fb74 Co-authored-by: Jonathon --- src/nvim/diff.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/nvim') diff --git a/src/nvim/diff.c b/src/nvim/diff.c index ee063a330e..68441f7adc 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -2127,7 +2127,8 @@ int diff_check_with_linestatus(win_T *wp, linenr_T lnum, int *linestatus) // Useful for scrollbind calculations which need to count all the filler lines // above the screen. if (lnum >= wp->w_topline && lnum < wp->w_botline - && !dp->is_linematched && diff_linematch(dp)) { + && !dp->is_linematched && diff_linematch(dp) + && diff_check_sanity(curtab, dp)) { run_linematch_algorithm(dp); } -- cgit