diff options
author | zeertzjq <zeertzjq@outlook.com> | 2025-03-27 09:24:26 +0800 |
---|---|---|
committer | zeertzjq <zeertzjq@outlook.com> | 2025-03-28 14:45:01 +0800 |
commit | 2331c52affe64070ad59c0ef63ddcc8f7ca41781 (patch) | |
tree | a2438a9e9cecad08f85890c959a02523a1861260 | |
parent | ae98d0a560b08d901ee9aae85df634de0ae3fe0a (diff) | |
download | rneovim-2331c52affe64070ad59c0ef63ddcc8f7ca41781.tar.gz rneovim-2331c52affe64070ad59c0ef63ddcc8f7ca41781.tar.bz2 rneovim-2331c52affe64070ad59c0ef63ddcc8f7ca41781.zip |
vim-patch:9.1.1243: diff mode is lacking for changes within lines
Problem: Diff mode's inline highlighting is lackluster. It only
performs a line-by-line comparison, and calculates a single
shortest range within a line that could encompass all the
changes. In lines with multiple changes, or those that span
multiple lines, this approach tends to end up highlighting
much more than necessary.
Solution: Implement new inline highlighting modes by doing per-character
or per-word diff within the diff block, and highlight only the
relevant parts, add "inline:simple" to the defaults (which is
the old behaviour)
This change introduces a new diffopt option "inline:<type>". Setting to
"none" will disable all inline highlighting, "simple" (the default) will
use the old behavior, "char" / "word" will perform a character/word-wise
diff of the texts within each diff block and only highlight the
differences.
The new char/word inline diff only use the internal xdiff, and will
respect diff options such as algorithm choice, icase, and misc iwhite
options. indent-heuristics is always on to perform better sliding.
For character highlight, a post-process of the diff results is first
applied before we show the highlight. This is because a naive diff will
create a result with a lot of small diff chunks and gaps, due to the
repetitive nature of individual characters. The post-process is a
heuristic-based refinement that attempts to merge adjacent diff blocks
if they are separated by a short gap (1-3 characters), and can be
further tuned in the future for better results. This process results in
more characters than necessary being highlighted but overall less visual
noise.
For word highlight, always use first buffer's iskeyword definition.
Otherwise if each buffer has different iskeyword settings we would not
be able to group words properly.
The char/word diffing is always per-diff block, not per line, meaning
that changes that span multiple lines will show up correctly.
Added/removed newlines are not shown by default, but if the user has
'list' set (with "eol" listchar defined), the eol character will be be
highlighted correctly for the specific newline characters.
Also, add a new "DiffTextAdd" highlight group linked to "DiffText" by
default. It allows color schemes to use different colors for texts that
have been added within a line versus modified.
This doesn't interact with linematch perfectly currently. The linematch
feature splits up diff blocks into multiple smaller blocks for better
visual matching, which makes inline highlight less useful especially for
multi-line change (e.g. a line is broken into two lines). This could be
addressed in the future.
As a side change, this also removes the bounds checking introduced to
diff_read() as they were added to mask existing logic bugs that were
properly fixed in vim/vim#16768.
closes: vim/vim#16881
https://github.com/vim/vim/commit/9943d4790e42721a6777da9e12637aa595ba4965
Co-authored-by: Yee Cheng Chin <ychin.git@gmail.com>
-rw-r--r-- | runtime/colors/vim.lua | 1 | ||||
-rw-r--r-- | runtime/doc/diff.txt | 31 | ||||
-rw-r--r-- | runtime/doc/news.txt | 4 | ||||
-rw-r--r-- | runtime/doc/options.txt | 17 | ||||
-rw-r--r-- | runtime/doc/syntax.txt | 5 | ||||
-rw-r--r-- | runtime/doc/vim_diff.txt | 2 | ||||
-rw-r--r-- | runtime/lua/vim/_meta/options.lua | 17 | ||||
-rw-r--r-- | runtime/syntax/vim.vim | 2 | ||||
-rw-r--r-- | src/nvim/buffer_defs.h | 52 | ||||
-rw-r--r-- | src/nvim/change.c | 1 | ||||
-rw-r--r-- | src/nvim/diff.c | 719 | ||||
-rw-r--r-- | src/nvim/drawline.c | 71 | ||||
-rw-r--r-- | src/nvim/highlight.h | 1 | ||||
-rw-r--r-- | src/nvim/highlight_defs.h | 1 | ||||
-rw-r--r-- | src/nvim/highlight_group.c | 1 | ||||
-rw-r--r-- | src/nvim/option_vars.h | 10 | ||||
-rw-r--r-- | src/nvim/options.lua | 18 | ||||
-rw-r--r-- | src/nvim/optionstr.c | 10 | ||||
-rw-r--r-- | test/functional/ui/cmdline_spec.lua | 2 | ||||
-rw-r--r-- | test/functional/ui/cursor_spec.lua | 4 | ||||
-rw-r--r-- | test/functional/ui/diff_spec.lua | 534 | ||||
-rw-r--r-- | test/functional/ui/messages_spec.lua | 8 | ||||
-rw-r--r-- | test/old/testdir/gen_opt_test.vim | 6 | ||||
-rw-r--r-- | test/old/testdir/setup.vim | 2 | ||||
-rw-r--r-- | test/old/testdir/test_diffmode.vim | 203 | ||||
-rw-r--r-- | test/old/testdir/test_options.vim | 5 |
26 files changed, 1599 insertions, 128 deletions
diff --git a/runtime/colors/vim.lua b/runtime/colors/vim.lua index f10cdedee0..1b6ab01671 100644 --- a/runtime/colors/vim.lua +++ b/runtime/colors/vim.lua @@ -47,6 +47,7 @@ hi('WildMenu', { fg = 'Black', bg = 'Yellow', ctermfg = 'Black', cterm hi('VertSplit', { link = 'Normal' }) hi('WinSeparator', { link = 'VertSplit' }) hi('WinBarNC', { link = 'WinBar' }) +hi('DiffTextAdd', { link = 'DiffText' }) hi('EndOfBuffer', { link = 'NonText' }) hi('LineNrAbove', { link = 'LineNr' }) hi('LineNrBelow', { link = 'LineNr' }) diff --git a/runtime/doc/diff.txt b/runtime/doc/diff.txt index c9de54342e..2c2565985a 100644 --- a/runtime/doc/diff.txt +++ b/runtime/doc/diff.txt @@ -214,14 +214,29 @@ The diffs are highlighted with these groups: |hl-DiffAdd| DiffAdd Added (inserted) lines. These lines exist in this buffer but not in another. |hl-DiffChange| DiffChange Changed lines. -|hl-DiffText| DiffText Changed text inside a Changed line. Vim - finds the first character that is different, - and the last character that is different - (searching from the end of the line). The - text in between is highlighted. This means - that parts in the middle that are still the - same are highlighted anyway. The 'diffopt' - flags "iwhite" and "icase" are used here. +|hl-DiffText| DiffText Changed text inside a Changed line. Exact + behavior depends on the `inline:` setting in + 'diffopt'. + With `inline:` set to "simple", Vim finds the + first character that is different, and the + last character that is different (searching + from the end of the line). The text in + between is highlighted. This means that parts + in the middle that are still the same are + highlighted anyway. The 'diffopt' flags + "iwhite" and "icase" are used here. + With `inline:` set to "char" or "word", Vim + uses the internal diff library to perform a + detailed diff between the changed blocks and + highlight the exact difference between the + two. Will respect any 'diffopt' flag that + affects internal diff. + Not used when `inline:` set to "none". +|hl-DiffTextAdd| DiffTextAdd Added text inside a Changed line. Similar to + DiffText, but used when there is no + corresponding text in other buffers. Will not + be used when `inline:` is set to "simple" or + "none". |hl-DiffDelete| DiffDelete Deleted lines. Also called filler lines, because they don't really exist in this buffer. diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index ad1481e304..aa6af1ef55 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -66,7 +66,7 @@ EVENTS HIGHLIGHTS -• todo +• |hl-DiffTextAdd| highlights added text within a changed line. LSP @@ -78,7 +78,7 @@ LUA OPTIONS -• todo +• 'diffopt' `inline:` configures diff highlighting for changes within a line. PLUGINS diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index dd2460722e..09eeddb0f7 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -2056,7 +2056,7 @@ A jump table for the options with a short description can be found at |Q_op|. security reasons. *'diffopt'* *'dip'* -'diffopt' 'dip' string (default "internal,filler,closeoff,linematch:40") +'diffopt' 'dip' string (default "internal,filler,closeoff,inline:simple,linematch:40") global Option settings for diff mode. It can consist of the following items. All are optional. Items must be separated by a comma. @@ -2119,6 +2119,21 @@ A jump table for the options with a short description can be found at |Q_op|. Use the indent heuristic for the internal diff library. + inline:{text} Highlight inline differences within a change. + See |view-diffs|. Supported values are: + + none Do not perform inline highlighting. + simple Highlight from first different + character to the last one in each + line. This is the default if nothing + is set. + char Use internal diff to perform a + character-wise diff and highlight the + difference. + word Use internal diff to perform a + |word|-wise diff and highlight the + difference. + internal Use the internal diff library. This is ignored when 'diffexpr' is set. *E960* When running out of memory when writing a diff --git a/runtime/doc/syntax.txt b/runtime/doc/syntax.txt index faca3d88da..d44e3cea8e 100644 --- a/runtime/doc/syntax.txt +++ b/runtime/doc/syntax.txt @@ -5177,8 +5177,11 @@ DiffChange Diff mode: Changed line. |diff.txt| DiffDelete Diff mode: Deleted line. |diff.txt| *hl-DiffText* DiffText Diff mode: Changed text within a changed line. |diff.txt| + *hl-DiffTextAdd* +DiffTextAdd Diff mode: Added text within a changed line. Linked to + |hl-DiffText| by default. |diff.txt| *hl-EndOfBuffer* -EndOfBuffer Filler lines (~) after the end of the buffer. +EndOfBuffer Filler lines (~) after the last line in the buffer. By default, this is highlighted like |hl-NonText|. *hl-TermCursor* TermCursor Cursor in a focused terminal. diff --git a/runtime/doc/vim_diff.txt b/runtime/doc/vim_diff.txt index df79d25198..a5c72363b4 100644 --- a/runtime/doc/vim_diff.txt +++ b/runtime/doc/vim_diff.txt @@ -48,7 +48,7 @@ Defaults *defaults* *nvim-defaults* - 'complete' excludes "i" - 'completeopt' defaults to "menu,popup" - 'define' defaults to "". The C ftplugin sets it to "^\\s*#\\s*define" -- 'diffopt' defaults to "internal,filler,closeoff,linematch:40" +- 'diffopt' defaults to "internal,filler,closeoff,inline:simple,linematch:40" - 'directory' defaults to ~/.local/state/nvim/swap// (|xdg|), auto-created - 'display' defaults to "lastline" - 'encoding' is UTF-8 (cf. 'fileencoding' for file-content encoding) diff --git a/runtime/lua/vim/_meta/options.lua b/runtime/lua/vim/_meta/options.lua index 20d8ac3058..427bbd5124 100644 --- a/runtime/lua/vim/_meta/options.lua +++ b/runtime/lua/vim/_meta/options.lua @@ -1716,6 +1716,21 @@ vim.go.dex = vim.go.diffexpr --- Use the indent heuristic for the internal --- diff library. --- +--- inline:{text} Highlight inline differences within a change. +--- See `view-diffs`. Supported values are: +--- +--- none Do not perform inline highlighting. +--- simple Highlight from first different +--- character to the last one in each +--- line. This is the default if nothing +--- is set. +--- char Use internal diff to perform a +--- character-wise diff and highlight the +--- difference. +--- word Use internal diff to perform a +--- `word`-wise diff and highlight the +--- difference. +--- --- internal Use the internal diff library. This is --- ignored when 'diffexpr' is set. *E960* --- When running out of memory when writing a @@ -1766,7 +1781,7 @@ vim.go.dex = vim.go.diffexpr --- --- --- @type string -vim.o.diffopt = "internal,filler,closeoff,linematch:40" +vim.o.diffopt = "internal,filler,closeoff,inline:simple,linematch:40" vim.o.dip = vim.o.diffopt vim.go.diffopt = vim.o.diffopt vim.go.dip = vim.go.diffopt diff --git a/runtime/syntax/vim.vim b/runtime/syntax/vim.vim index 7fe22190b7..29d9856b3d 100644 --- a/runtime/syntax/vim.vim +++ b/runtime/syntax/vim.vim @@ -60,7 +60,7 @@ syn case ignore syn keyword vimGroup contained Comment Constant String Character Number Boolean Float Identifier Function Statement Conditional Repeat Label Operator Keyword Exception PreProc Include Define Macro PreCondit Type StorageClass Structure Typedef Special SpecialChar Tag Delimiter SpecialComment Debug Underlined Ignore Error Todo " Default highlighting groups {{{2 -syn keyword vimHLGroup contained ErrorMsg IncSearch ModeMsg NonText StatusLine StatusLineNC EndOfBuffer VertSplit DiffText PmenuSbar TabLineSel TabLineFill Cursor lCursor QuickFixLine CursorLineSign CursorLineFold CurSearch PmenuKind PmenuKindSel PmenuMatch PmenuMatchSel PmenuExtra PmenuExtraSel ComplMatchIns Normal Directory LineNr CursorLineNr MoreMsg Question Search SpellBad SpellCap SpellRare SpellLocal PmenuThumb Pmenu PmenuSel SpecialKey Title WarningMsg WildMenu Folded FoldColumn SignColumn Visual DiffAdd DiffChange DiffDelete TabLine CursorColumn CursorLine ColorColumn MatchParen StatusLineTerm StatusLineTermNC CursorIM LineNrAbove LineNrBelow +syn keyword vimHLGroup contained ErrorMsg IncSearch ModeMsg NonText StatusLine StatusLineNC EndOfBuffer VertSplit DiffText DiffTextAdd PmenuSbar TabLineSel TabLineFill Cursor lCursor QuickFixLine CursorLineSign CursorLineFold CurSearch PmenuKind PmenuKindSel PmenuMatch PmenuMatchSel PmenuExtra PmenuExtraSel ComplMatchIns Normal Directory LineNr CursorLineNr MoreMsg Question Search SpellBad SpellCap SpellRare SpellLocal PmenuThumb Pmenu PmenuSel SpecialKey Title WarningMsg WildMenu Folded FoldColumn SignColumn Visual DiffAdd DiffChange DiffDelete TabLine CursorColumn CursorLine ColorColumn MatchParen StatusLineTerm StatusLineTermNC CursorIM LineNrAbove LineNrBelow syn match vimHLGroup contained "\<Conceal\>" syn keyword vimOnlyHLGroup contained Menu Scrollbar ToolbarButton ToolbarLine Tooltip VisualNOS syn keyword nvimHLGroup contained FloatBorder FloatFooter FloatTitle MsgSeparator NormalFloat NormalNC Substitute TermCursor VisualNC Whitespace WinBar WinBarNC WinSeparator diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index c4241eed45..be28e08675 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -743,18 +743,21 @@ struct file_buffer { // Stuff for diff mode. #define DB_COUNT 8 // up to four buffers can be diff'ed -// Each diffblock defines where a block of lines starts in each of the buffers -// and how many lines it occupies in that buffer. When the lines are missing -// in the buffer the df_count[] is zero. This is all counted in -// buffer lines. -// There is always at least one unchanged line in between the diffs. -// Otherwise it would have been included in the diff above or below it. -// df_lnum[] + df_count[] is the lnum below the change. When in one buffer -// lines have been inserted, in the other buffer df_lnum[] is the line below -// the insertion and df_count[] is zero. When appending lines at the end of -// the buffer, df_lnum[] is one beyond the end! -// This is using a linked list, because the number of differences is expected -// to be reasonable small. The list is sorted on lnum. +/// Each diffblock defines where a block of lines starts in each of the buffers +/// and how many lines it occupies in that buffer. When the lines are missing +/// in the buffer the df_count[] is zero. This is all counted in +/// buffer lines. +/// There is always at least one unchanged line in between the diffs (unless +/// linematch is used). Otherwise it would have been included in the diff above +/// or below it. +/// df_lnum[] + df_count[] is the lnum below the change. When in one buffer +/// lines have been inserted, in the other buffer df_lnum[] is the line below +/// the insertion and df_count[] is zero. When appending lines at the end of +/// the buffer, df_lnum[] is one beyond the end! +/// This is using a linked list, because the number of differences is expected +/// to be reasonable small. The list is sorted on lnum. +/// Each diffblock also contains a cached list of inline diff of changes within +/// the block, used for highlighting. typedef struct diffblock_S diff_T; struct diffblock_S { diff_T *df_next; @@ -762,6 +765,31 @@ struct diffblock_S { linenr_T df_count[DB_COUNT]; // nr of inserted/changed lines bool is_linematched; // has the linematch algorithm ran on this diff hunk to divide it into // smaller diff hunks? + + bool has_changes; ///< has cached list of inline changes + garray_T df_changes; ///< list of inline changes (diffline_change_T) +}; + +/// Each entry stores a single inline change within a diff block. Line numbers +/// are recorded as relative offsets, and columns are byte offsets, not +/// character counts. +/// Ranges are [start,end), with the end being exclusive. +typedef struct diffline_change_S diffline_change_T; +struct diffline_change_S { + colnr_T dc_start[DB_COUNT]; ///< byte offset of start of range in the line + colnr_T dc_end[DB_COUNT]; ///< 1 paste byte offset of end of range in line + int dc_start_lnum_off[DB_COUNT]; ///< starting line offset + int dc_end_lnum_off[DB_COUNT]; ///< end line offset +}; + +/// Describes a single line's list of inline changes. Use diff_change_parse() to +/// parse this. +typedef struct diffline_S diffline_T; +struct diffline_S { + diffline_change_T *changes; + int num_changes; + int bufidx; + int lineoff; }; #define SNAP_HELP_IDX 0 diff --git a/src/nvim/change.c b/src/nvim/change.c index 192e0d9faa..cf7d5dfc4b 100644 --- a/src/nvim/change.c +++ b/src/nvim/change.c @@ -240,6 +240,7 @@ static void changed_common(buf_T *buf, linenr_T lnum, colnr_T col, linenr_T lnum FOR_ALL_WINDOWS_IN_TAB(win, curtab) { if (win->w_buffer == buf && win->w_p_diff && diff_internal()) { curtab->tp_diff_update = true; + diff_update_line(lnum); } } diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 4c5b86adc4..585a937558 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -87,7 +87,13 @@ static bool diff_need_update = false; // ex_diffupdate needs to be called #define DIFF_CLOSE_OFF 0x400 // diffoff when closing window #define DIFF_FOLLOWWRAP 0x800 // follow the wrap option #define DIFF_LINEMATCH 0x1000 // match most similar lines within diff +#define DIFF_INLINE_NONE 0x2000 // no inline highlight +#define DIFF_INLINE_SIMPLE 0x4000 // inline highlight with simple algorithm +#define DIFF_INLINE_CHAR 0x8000 // inline highlight with character diff +#define DIFF_INLINE_WORD 0x10000 // inline highlight with word diff #define ALL_WHITE_DIFF (DIFF_IWHITE | DIFF_IWHITEALL | DIFF_IWHITEEOL) +#define ALL_INLINE (DIFF_INLINE_NONE | DIFF_INLINE_SIMPLE | DIFF_INLINE_CHAR | DIFF_INLINE_WORD) +#define ALL_INLINE_DIFF (DIFF_INLINE_CHAR | DIFF_INLINE_WORD) static int diff_flags = DIFF_INTERNAL | DIFF_FILLER | DIFF_CLOSE_OFF; static int diff_algorithm = 0; @@ -137,6 +143,15 @@ typedef enum { # include "diff.c.generated.h" #endif +#define FOR_ALL_DIFFBLOCKS_IN_TAB(tp, dp) \ + for ((dp) = (tp)->tp_first_diff; (dp) != NULL; (dp) = (dp)->df_next) + +static void clear_diffblock(diff_T *dp) +{ + ga_clear(&dp->df_changes); + xfree(dp); +} + /// Called when deleting or unloading a buffer: No longer make a diff with it. /// /// @param buf @@ -523,7 +538,7 @@ static void diff_mark_adjust_tp(tabpage_T *tp, int idx, linenr_T line1, linenr_T /// @return The new diff block. static diff_T *diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp) { - diff_T *dnew = xmalloc(sizeof(*dnew)); + diff_T *dnew = xcalloc(1, sizeof(*dnew)); dnew->is_linematched = false; dnew->df_next = dp; @@ -533,13 +548,15 @@ static diff_T *diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp) dprev->df_next = dnew; } + dnew->has_changes = false; + ga_init(&dnew->df_changes, sizeof(diffline_change_T), 20); return dnew; } static diff_T *diff_free(tabpage_T *tp, diff_T *dprev, diff_T *dp) { diff_T *ret = dp->df_next; - xfree(dp); + clear_diffblock(dp); if (dprev == NULL) { tp->tp_first_diff = ret; @@ -764,15 +781,32 @@ static int diff_write_buffer(buf_T *buf, mmfile_t *m, linenr_T start, linenr_T e char *s = ml_get_buf(buf, lnum); if (diff_flags & DIFF_ICASE) { while (*s != NUL) { + int c; + int c_len = 1; char cbuf[MB_MAXBYTES + 1]; - // xdiff doesn't support ignoring case, fold-case the text. - int c = *s == NL ? NUL : utf_fold(utf_ptr2char(s)); + if (*s == NL) { + c = NUL; + } else { + // xdiff doesn't support ignoring case, fold-case the text. + c = utf_ptr2char(s); + c_len = utf_char2len(c); + c = utf_fold(c); + } const int orig_len = utfc_ptr2len(s); - // TODO(Bram): handle byte length difference - char *s1 = (utf_char2bytes(c, cbuf) != orig_len) ? s : cbuf; - memmove(ptr + len, s1, (size_t)orig_len); + if (utf_char2bytes(c, cbuf) != c_len) { + // TODO(Bram): handle byte length difference + // One example is Å (3 bytes) and å (2 bytes). + memmove(ptr + len, s, (size_t)orig_len); + } else { + memmove(ptr + len, cbuf, (size_t)c_len); + if (orig_len > c_len) { + // Copy remaining composing characters + memmove(ptr + len + c_len, s + c_len, (size_t)(orig_len - c_len)); + } + } + s += orig_len; len += (size_t)orig_len; } @@ -944,8 +978,7 @@ void ex_diffupdate(exarg_T *eap) } // Only use the internal method if it did not fail for one of the buffers. - diffio_T diffio; - CLEAR_FIELD(diffio); + diffio_T diffio = { 0 }; diffio.dio_internal = diff_internal(); diff_try_update(&diffio, idx_orig, eap); @@ -1640,11 +1673,6 @@ static void process_hunk(diff_T **dpp, diff_T **dprevp, int idx_orig, int idx_ne if (off > 0) { dp->df_count[idx_new] += off; } - if ((dp->df_lnum[idx_new] + dp->df_count[idx_new] - 1) - > curtab->tp_diffbuf[idx_new]->b_ml.ml_line_count) { - dp->df_count[idx_new] = curtab->tp_diffbuf[idx_new]->b_ml.ml_line_count - - dp->df_lnum[idx_new] + 1; - } } // Adjust the size of the block to include all the lines to the @@ -1662,11 +1690,6 @@ static void process_hunk(diff_T **dpp, diff_T **dprevp, int idx_orig, int idx_ne // overlap later. dp->df_count[idx_new] += -off; } - if ((dp->df_lnum[idx_new] + dp->df_count[idx_new] - 1) - > curtab->tp_diffbuf[idx_new]->b_ml.ml_line_count) { - dp->df_count[idx_new] = curtab->tp_diffbuf[idx_new]->b_ml.ml_line_count - - dp->df_lnum[idx_new] + 1; - } off = 0; } @@ -1683,7 +1706,7 @@ static void process_hunk(diff_T **dpp, diff_T **dprevp, int idx_orig, int idx_ne while (dn != dp->df_next) { dpl = dn->df_next; - xfree(dn); + clear_diffblock(dn); dn = dpl; } } else { @@ -1717,7 +1740,7 @@ static void process_hunk(diff_T **dpp, diff_T **dprevp, int idx_orig, int idx_ne static void diff_read(int idx_orig, int idx_new, diffio_T *dio) { FILE *fd = NULL; - int line_idx = 0; + int line_hunk_idx = 0; // line or hunk index diff_T *dprev = NULL; diff_T *dp = curtab->tp_first_diff; diffout_T *dout = &dio->dio_diff; @@ -1735,7 +1758,7 @@ static void diff_read(int idx_orig, int idx_new, diffio_T *dio) while (true) { diffhunk_T hunk = { 0 }; bool eof = dio->dio_internal - ? extract_hunk_internal(dout, &hunk, &line_idx) + ? extract_hunk_internal(dout, &hunk, &line_hunk_idx) : extract_hunk(fd, &hunk, &diffstyle); if (eof) { @@ -1789,7 +1812,7 @@ void diff_clear(tabpage_T *tp) diff_T *next_p; for (diff_T *p = tp->tp_first_diff; p != NULL; p = next_p) { next_p = p->df_next; - xfree(p); + clear_diffblock(p); } tp->tp_first_diff = NULL; } @@ -2532,6 +2555,28 @@ int diffopt_changed(void) } else { return FAIL; } + } else if (strncmp(p, "inline:", 7) == 0) { + // Note: Keep this in sync with opt_dip_inline_values. + p += 7; + if (strncmp(p, "none", 4) == 0) { + p += 4; + diff_flags_new &= ~(ALL_INLINE); + diff_flags_new |= DIFF_INLINE_NONE; + } else if (strncmp(p, "simple", 6) == 0) { + p += 6; + diff_flags_new &= ~(ALL_INLINE); + diff_flags_new |= DIFF_INLINE_SIMPLE; + } else if (strncmp(p, "char", 4) == 0) { + p += 4; + diff_flags_new &= ~(ALL_INLINE); + diff_flags_new |= DIFF_INLINE_CHAR; + } else if (strncmp(p, "word", 4) == 0) { + p += 4; + diff_flags_new &= ~(ALL_INLINE); + diff_flags_new |= DIFF_INLINE_WORD; + } else { + return FAIL; + } } else if ((strncmp(p, "linematch:", 10) == 0) && ascii_isdigit(p[10])) { p += 10; linematch_lines_new = getdigits_int(&p, false, linematch_lines_new); @@ -2604,48 +2649,101 @@ bool diffopt_filler(void) return (diff_flags & DIFF_FILLER) != 0; } -/// Find the difference within a changed line. -/// -/// @param wp window whose current buffer to check -/// @param lnum line number to check within the buffer -/// @param startp first char of the change -/// @param endp last char of the change -/// -/// @return true if the line was added, no other buffer has it. -bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) - FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL +/// Called when a line has been updated. Used for updating inline diff in Insert +/// mode without waiting for global diff update later. +void diff_update_line(linenr_T lnum) { - // Make a copy of the line, the next ml_get() will invalidate it. - char *line_org = xstrdup(ml_get_buf(wp->w_buffer, lnum)); + if (!(diff_flags & ALL_INLINE_DIFF)) { + // We only care if we are doing inline-diff where we cache the diff results + return; + } - int idx = diff_buf_idx(wp->w_buffer, curtab); + int idx = diff_buf_idx(curbuf, curtab); if (idx == DB_COUNT) { - // cannot happen - xfree(line_org); - return false; + return; } - - // search for a change that includes "lnum" in the list of diffblocks. diff_T *dp; - for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next) { + FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp) { if (lnum <= dp->df_lnum[idx] + dp->df_count[idx]) { break; } } - if (dp != NULL && dp->is_linematched) { - while (dp && dp->df_next - && lnum == dp->df_count[idx] + dp->df_lnum[idx] - && dp->df_next->df_lnum[idx] == lnum) { - dp = dp->df_next; - } + + // clear the inline change cache as it's invalid + if (dp != NULL) { + dp->has_changes = false; + dp->df_changes.ga_len = 0; } +} - if ((dp == NULL) || (diff_check_sanity(curtab, dp) == FAIL)) { - xfree(line_org); +/// used for simple inline diff algorithm +static diffline_change_T simple_diffline_change; + +/// Parse a diffline struct and returns the [start,end] byte offsets +/// +/// Returns true if this change was added, no other buffer has it. +bool diff_change_parse(diffline_T *diffline, diffline_change_T *change, int *change_start, + int *change_end) +{ + if (change->dc_start_lnum_off[diffline->bufidx] < diffline->lineoff) { + *change_start = 0; + } else { + *change_start = change->dc_start[diffline->bufidx]; + } + if (change->dc_end_lnum_off[diffline->bufidx] > diffline->lineoff) { + *change_end = INT_MAX; + } else { + *change_end = change->dc_end[diffline->bufidx]; + } + if (change == &simple_diffline_change) { + // This is what we returned from simple inline diff. We always consider + // the range to be changed, rather than added for now. return false; } + // Find out whether this is an addition. Note that for multi buffer diff, + // to tell whether lines are additions we check whether all the other diff + // lines are identical (in diff_check_with_linestatus). If so, we mark them + // as add. We don't do that for inline diff here for simplicity. + for (int i = 0; i < DB_COUNT; i++) { + if (i == diffline->bufidx) { + continue; + } + if (change->dc_start[i] != change->dc_end[i] + || change->dc_end_lnum_off[i] != change->dc_start_lnum_off[i]) { + return false; + } + } + return true; +} + +/// Find the difference within a changed line and returns [startp,endp] byte +/// positions. Performs a simple algorithm by finding a single range in the +/// middle. +/// +/// If diffopt has DIFF_INLINE_NONE set, then this will only calculate the return +/// value (added or changed), but startp/endp will not be calculated. +/// +/// @param wp window whose current buffer to check +/// @param lnum line number to check within the buffer +/// @param startp first char of the change +/// @param endp last char of the change +/// +/// @return true if the line was added, no other buffer has it. +static bool diff_find_change_simple(win_T *wp, linenr_T lnum, const diff_T *dp, int idx, + int *startp, int *endp) + FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL +{ + char *line_org; + if (diff_flags & DIFF_INLINE_NONE) { + // We only care about the return value, not the actual string comparisons. + line_org = NULL; + } else { + // Make a copy of the line, the next ml_get() will invalidate it. + line_org = xstrdup(ml_get_buf(wp->w_buffer, lnum)); + } + int si_org; int si_new; int ei_org; @@ -2660,6 +2758,10 @@ bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) continue; } added = false; + if (diff_flags & DIFF_INLINE_NONE) { + break; // early terminate as we only care about the return value + } + char *line_new = ml_get_buf(curtab->tp_diffbuf[i], dp->df_lnum[i] + off); // Search for start of difference @@ -2738,6 +2840,470 @@ bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) return added; } +/// Mapping used for mapping from temporary mmfile created for inline diff back +/// to original buffer's line/col. +typedef struct { + colnr_T byte_start; + colnr_T num_bytes; + int lineoff; +} linemap_entry_T; + +/// Refine inline character-wise diff blocks to create a more human readable +/// highlight. Otherwise a naive diff under existing algorithms tends to create +/// a messy output with lots of small gaps. +/// It does this by merging adjacent long diff blocks if they are only separated +/// by a couple characters. +/// These are done by heuristics and can be further tuned. +static void diff_refine_inline_char_highlight(diff_T *dp_orig, garray_T *linemap, int idx1) +{ + // Perform multiple passes so that newly merged blocks will now be long + // enough which may cause other previously unmerged gaps to be merged as + // well. + int pass = 1; + do { + bool has_unmerged_gaps = false; + bool has_merged_gaps = false; + diff_T *dp = dp_orig; + while (dp != NULL && dp->df_next != NULL) { + // Only use first buffer to calculate the gap because the gap is + // unchanged text, which would be the same in all buffers. + if (dp->df_lnum[idx1] + dp->df_count[idx1] - 1 >= linemap[idx1].ga_len + || dp->df_next->df_lnum[idx1] - 1 >= linemap[idx1].ga_len) { + dp = dp->df_next; + continue; + } + + // If the gap occurs over different lines, don't consider it + linemap_entry_T *entry1 = + &((linemap_entry_T *)linemap[idx1].ga_data)[dp->df_lnum[idx1] + + dp->df_count[idx1] - 1]; + linemap_entry_T *entry2 = + &((linemap_entry_T *)linemap[idx1].ga_data)[dp->df_next->df_lnum[idx1] - 1]; + if (entry1->lineoff != entry2->lineoff) { + dp = dp->df_next; + continue; + } + + linenr_T gap = dp->df_next->df_lnum[idx1] - (dp->df_lnum[idx1] + dp->df_count[idx1]); + if (gap <= 3) { + linenr_T max_df_count = 0; + for (int i = 0; i < DB_COUNT; i++) { + max_df_count = MAX(max_df_count, dp->df_count[i] + dp->df_next->df_count[i]); + } + + if (max_df_count >= gap * 4) { + // Merge current block with the next one. Don't advance the + // pointer so we try the same merged block against the next + // one. + for (int i = 0; i < DB_COUNT; i++) { + dp->df_count[i] = dp->df_next->df_lnum[i] + + dp->df_next->df_count[i] - dp->df_lnum[i]; + } + diff_T *dp_next = dp->df_next; + dp->df_next = dp_next->df_next; + clear_diffblock(dp_next); + has_merged_gaps = true; + continue; + } else { + has_unmerged_gaps = true; + } + } + dp = dp->df_next; + } + if (!has_unmerged_gaps || !has_merged_gaps) { + break; + } + } while (pass++ < 4); // use limited number of passes to avoid excessive looping +} + +/// Find the inline difference within a diff block among differnt buffers. Do +/// this by splitting each block's content into characters or words, and then +/// use internal xdiff to calculate the per-character/word diff. The result is +/// stored in dp instead of returned by the function. +static void diff_find_change_inline_diff(diff_T *dp) +{ + const int save_diff_algorithm = diff_algorithm; + + diffio_T dio = { 0 }; + ga_init(&dio.dio_diff.dout_ga, sizeof(char *), 1000); + + // inline diff only supports internal algo + dio.dio_internal = true; + + // always use indent-heuristics to slide diff splits along + // whitespace + diff_algorithm |= XDF_INDENT_HEURISTIC; + + // diff_read() has an implicit dependency on curtab->tp_first_diff + diff_T *orig_diff = curtab->tp_first_diff; + curtab->tp_first_diff = NULL; + + garray_T linemap[DB_COUNT]; + garray_T file1_str; + garray_T file2_str; + + // Buffers to populate mmfile 1/2 that would be passed to xdiff as memory + // files. Use a grow array as it is not obvious how much exact space we + // need. + ga_init(&file1_str, 1, 1024); + ga_init(&file2_str, 1, 1024); + + // Line map to map from generated mmfiles' line numbers back to original + // diff blocks' locations. Need this even for char diff because not all + // characters are 1-byte long / ASCII. + for (int i = 0; i < DB_COUNT; i++) { + ga_init(&linemap[i], sizeof(linemap_entry_T), 128); + } + + int file1_idx = -1; + for (int i = 0; i < DB_COUNT; i++) { + dio.dio_diff.dout_ga.ga_len = 0; + + buf_T *buf = curtab->tp_diffbuf[i]; + if (buf == NULL || buf->b_ml.ml_mfp == NULL) { + continue; // skip buffer that isn't loaded + } + if (dp->df_count[i] == 0) { + continue; // skip buffer that don't have any texts in this block + } + if (file1_idx == -1) { + file1_idx = i; + } + + garray_T *curstr = (file1_idx != i) ? &file2_str : &file1_str; + + linenr_T numlines = 0; + curstr->ga_len = 0; + + // Split each line into chars/words and populate fake file buffer as + // newline-delimited tokens as that's what xdiff requires. + for (int off = 0; off < dp->df_count[i]; off++) { + char *curline = ml_get_buf(curtab->tp_diffbuf[i], dp->df_lnum[i] + off); + + bool in_keyword = false; + + // iwhiteeol support vars + bool last_white = false; + int eol_ga_len = -1; + int eol_linemap_len = -1; + int eol_numlines = -1; + + char *s = curline; + while (*s != NUL) { + // Always use the first buffer's 'iskeyword' to have a consistent diff + bool new_in_keyword = false; + if (diff_flags & DIFF_INLINE_WORD) { + new_in_keyword = vim_iswordp_buf(s, curtab->tp_diffbuf[file1_idx]); + } + if (in_keyword && !new_in_keyword) { + ga_append(curstr, NL); + numlines++; + } + + if (ascii_iswhite(*s)) { + if (diff_flags & DIFF_IWHITEALL) { + in_keyword = false; + s = skipwhite(s); + continue; + } else if ((diff_flags & DIFF_IWHITEEOL) || (diff_flags & DIFF_IWHITE)) { + if (!last_white) { + eol_ga_len = curstr->ga_len; + eol_linemap_len = linemap[i].ga_len; + eol_numlines = numlines; + last_white = true; + } + } + } else { + if ((diff_flags & DIFF_IWHITEEOL) || (diff_flags & DIFF_IWHITE)) { + last_white = false; + eol_ga_len = -1; + eol_linemap_len = -1; + eol_numlines = -1; + } + } + + int char_len = 1; + if (*s == NL) { + // NL is internal substitute for NUL + ga_append(curstr, NUL); + } else { + char_len = utfc_ptr2len(s); + + if (ascii_iswhite(*s) && (diff_flags & DIFF_IWHITE)) { + // Treat the entire white space span as a single char. + char_len = (int)(skipwhite(s) - s); + } + + if (diff_flags & DIFF_ICASE) { + // xdiff doesn't support ignoring case, fold-case the text manually. + int c = utf_ptr2char(s); + int c_len = utf_char2len(c); + c = utf_fold(c); + char cbuf[MB_MAXBYTES + 1]; + int c_fold_len = utf_char2bytes(c, cbuf); + ga_concat_len(curstr, cbuf, (size_t)c_fold_len); + if (char_len > c_len) { + // There may be remaining composing characters. Write those back in. + // Composing characters don't need case folding. + ga_concat_len(curstr, s + c_len, (size_t)(char_len - c_len)); + } + } else { + ga_concat_len(curstr, s, (size_t)char_len); + } + } + + if (!new_in_keyword) { + ga_append(curstr, NL); + numlines++; + } + + if (!new_in_keyword || (new_in_keyword && !in_keyword)) { + // create a new mapping entry from the xdiff mmfile back to + // original line/col. + linemap_entry_T linemap_entry = { + .lineoff = off, + .byte_start = (colnr_T)(s - curline), + .num_bytes = char_len, + }; + GA_APPEND(linemap_entry_T, &linemap[i], linemap_entry); + } else { + // Still inside a keyword. Just increment byte count but + // don't make a new entry. + // linemap always has at least one entry here + ((linemap_entry_T *)linemap[i].ga_data)[linemap[i].ga_len - 1].num_bytes += char_len; + } + + in_keyword = new_in_keyword; + s += char_len; + } + if (in_keyword) { + ga_append(curstr, NL); + numlines++; + } + + if ((diff_flags & DIFF_IWHITEEOL) || (diff_flags & DIFF_IWHITE)) { + // Need to trim trailing whitespace. Do this simply by + // resetting arrays back to before we encountered them. + if (eol_ga_len != -1) { + curstr->ga_len = eol_ga_len; + linemap[i].ga_len = eol_linemap_len; + numlines = eol_numlines; + } + } + + if (!(diff_flags & DIFF_IWHITEALL)) { + // Add an empty line token mapped to the end-of-line in the + // original file. This helps diff newline differences among + // files, which will be visualized when using 'list' as the eol + // listchar will be highlighted. + ga_append(curstr, NL); + numlines++; + + linemap_entry_T linemap_entry = { + .lineoff = off, + .byte_start = (colnr_T)(s - curline), + .num_bytes = sizeof(NL), + }; + GA_APPEND(linemap_entry_T, &linemap[i], linemap_entry); + } + } + + if (file1_idx != i) { + dio.dio_new.din_mmfile.ptr = (char *)curstr->ga_data; + dio.dio_new.din_mmfile.size = curstr->ga_len; + } else { + dio.dio_orig.din_mmfile.ptr = (char *)curstr->ga_data; + dio.dio_orig.din_mmfile.size = curstr->ga_len; + } + if (file1_idx != i) { + // Perform diff with first file and read the results + int diff_status = diff_file_internal(&dio); + if (diff_status == FAIL) { + goto done; + } + + diff_read(0, i, &dio); + clear_diffout(&dio.dio_diff); + } + } + diff_T *new_diff = curtab->tp_first_diff; + + if (diff_flags & DIFF_INLINE_CHAR && file1_idx != -1) { + diff_refine_inline_char_highlight(new_diff, linemap, file1_idx); + } + + // After the diff, use the linemap to obtain the original line/col of the + // changes and cache them in dp. + dp->df_changes.ga_len = 0; // this should already be zero + for (; new_diff != NULL; new_diff = new_diff->df_next) { + diffline_change_T change = { 0 }; + for (int i = 0; i < DB_COUNT; i++) { + if (new_diff->df_lnum[i] == 0) { + continue; + } + linenr_T diff_lnum = new_diff->df_lnum[i] - 1; // use zero-index + linenr_T diff_lnum_end = diff_lnum + new_diff->df_count[i]; + + if (diff_lnum >= linemap[i].ga_len) { + change.dc_start[i] = MAXCOL; + change.dc_start_lnum_off[i] = INT_MAX; + } else { + change.dc_start[i] = ((linemap_entry_T *)linemap[i].ga_data)[diff_lnum].byte_start; + change.dc_start_lnum_off[i] = ((linemap_entry_T *)linemap[i].ga_data)[diff_lnum].lineoff; + } + + if (diff_lnum == diff_lnum_end) { + change.dc_end[i] = change.dc_start[i]; + change.dc_end_lnum_off[i] = change.dc_start_lnum_off[i]; + } else if (diff_lnum_end - 1 >= linemap[i].ga_len) { + change.dc_end[i] = MAXCOL; + change.dc_end_lnum_off[i] = INT_MAX; + } else { + change.dc_end[i] = ((linemap_entry_T *)linemap[i].ga_data)[diff_lnum_end - 1].byte_start + + ((linemap_entry_T *)linemap[i].ga_data)[diff_lnum_end - 1].num_bytes; + change.dc_end_lnum_off[i] = ((linemap_entry_T *)linemap[i].ga_data)[diff_lnum_end - + 1].lineoff; + } + } + GA_APPEND(diffline_change_T, &dp->df_changes, change); + } + +done: + diff_algorithm = save_diff_algorithm; + + dp->has_changes = true; + + diff_clear(curtab); + curtab->tp_first_diff = orig_diff; + + ga_clear(&file1_str); + ga_clear(&file2_str); + // No need to clear dio.dio_orig/dio_new because they were referencing + // strings that are now cleared. + clear_diffout(&dio.dio_diff); + for (int i = 0; i < DB_COUNT; i++) { + ga_clear(&linemap[i]); + } +} + +/// Find the difference within a changed line. +/// Returns true if the line was added, no other buffer has it. +bool diff_find_change(win_T *wp, linenr_T lnum, diffline_T *diffline) + FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL +{ + int idx = diff_buf_idx(wp->w_buffer, curtab); + if (idx == DB_COUNT) { // cannot happen + return false; + } + + // search for a change that includes "lnum" in the list of diffblocks. + diff_T *dp; + FOR_ALL_DIFFBLOCKS_IN_TAB(curtab, dp) { + if (lnum <= dp->df_lnum[idx] + dp->df_count[idx]) { + break; + } + } + if (dp && dp->is_linematched) { + while (dp && dp->df_next + && lnum == dp->df_count[idx] + dp->df_lnum[idx] + && dp->df_next->df_lnum[idx] == lnum) { + dp = dp->df_next; + } + } + if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL) { + return false; + } + + if (lnum - dp->df_lnum[idx] > INT_MAX) { + // Integer overflow protection + return false; + } + int off = lnum - dp->df_lnum[idx]; + + if (!(diff_flags & ALL_INLINE_DIFF)) { + // Use simple algorithm + int change_start = MAXCOL; // first col of changed area + int change_end = -1; // last col of changed area + + int ret = diff_find_change_simple(wp, lnum, dp, idx, &change_start, &change_end); + + // convert from inclusive end to exclusive end per diffline's contract + change_end += 1; + + // Create a mock diffline struct. We always only have one so no need to + // allocate memory. + idx = diff_buf_idx(wp->w_buffer, curtab); + CLEAR_FIELD(simple_diffline_change); + diffline->changes = &simple_diffline_change; + diffline->num_changes = 1; + diffline->bufidx = idx; + diffline->lineoff = lnum - dp->df_lnum[idx]; + + simple_diffline_change.dc_start[idx] = change_start; + simple_diffline_change.dc_end[idx] = change_end; + simple_diffline_change.dc_start_lnum_off[idx] = off; + simple_diffline_change.dc_end_lnum_off[idx] = off; + return ret; + } + + // Use inline diff algorithm. + // The diff changes are usually cached so we check that first. + if (!dp->has_changes) { + diff_find_change_inline_diff(dp); + } + + garray_T *changes = &dp->df_changes; + + // Use linear search to find the first change for this line. We could + // optimize this to use binary search, but there should usually be a + // limited number of inline changes per diff block, and limited number of + // diff blocks shown on screen, so it is not necessary. + int num_changes = 0; + int change_idx = 0; + diffline->changes = NULL; + for (change_idx = 0; change_idx < changes->ga_len; change_idx++) { + diffline_change_T *change = + &((diffline_change_T *)dp->df_changes.ga_data)[change_idx]; + if (change->dc_end_lnum_off[idx] < off) { + continue; + } + if (change->dc_start_lnum_off[idx] > off) { + break; + } + if (diffline->changes == NULL) { + diffline->changes = change; + } + num_changes++; + } + diffline->num_changes = num_changes; + diffline->bufidx = idx; + diffline->lineoff = off; + + // Detect simple cases of added lines in the end within a diff block. This + // has to be the last change of this diff block, and all other buffers are + // considering this to be an addition past their last line. Other scenarios + // will be considered a changed line instead. + bool added = false; + if (num_changes == 1 && change_idx == dp->df_changes.ga_len) { + added = true; + for (int i = 0; i < DB_COUNT; i++) { + if (idx == i) { + continue; + } + if (curtab->tp_diffbuf[i] == NULL) { + continue; + } + diffline_change_T *change = + &((diffline_change_T *)dp->df_changes.ga_data)[dp->df_changes.ga_len - 1]; + if (change->dc_start_lnum_off[i] != INT_MAX) { + added = false; + break; + } + } + } + return added; +} + /// Check that line "lnum" is not close to a diff block, this line should /// be in a fold. /// @@ -3499,20 +4065,29 @@ void f_diff_filler(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) /// "diff_hlID()" function void f_diff_hlID(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { - linenr_T lnum = tv_get_lnum(argvars); static linenr_T prev_lnum = 0; static varnumber_T changedtick = 0; static int fnum = 0; + static int prev_diff_flags = 0; static int change_start = 0; static int change_end = 0; static hlf_T hlID = (hlf_T)0; + diffline_T diffline = { 0 }; + // Remember the results if using simple since it's recalculated per + // call. Otherwise just call diff_find_change() every time since + // internally the result is cached interally. + const bool cache_results = !(diff_flags & ALL_INLINE_DIFF); + + linenr_T lnum = tv_get_lnum(argvars); if (lnum < 0) { // ignore type error in {lnum} arg lnum = 0; } - if (lnum != prev_lnum + if (!cache_results + || lnum != prev_lnum || changedtick != buf_get_changedtick(curbuf) - || fnum != curbuf->b_fnum) { + || fnum != curbuf->b_fnum + || diff_flags != prev_diff_flags) { // New line, buffer, change: need to get the values. int linestatus = 0; int filler_lines = diff_check_with_linestatus(curwin, lnum, &linestatus); @@ -3520,10 +4095,14 @@ void f_diff_hlID(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) if (filler_lines == -1 || linestatus == -1) { change_start = MAXCOL; change_end = -1; - if (diff_find_change(curwin, lnum, &change_start, &change_end)) { + if (diff_find_change(curwin, lnum, &diffline)) { hlID = HLF_ADD; // added line } else { hlID = HLF_CHD; // changed line + if (diffline.num_changes > 0 && cache_results) { + change_start = diffline.changes[0].dc_start[diffline.bufidx]; + change_end = diffline.changes[0].dc_end[diffline.bufidx]; + } } } else { hlID = HLF_ADD; // added line @@ -3531,17 +4110,37 @@ void f_diff_hlID(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } else { hlID = (hlf_T)0; } - prev_lnum = lnum; - changedtick = buf_get_changedtick(curbuf); - fnum = curbuf->b_fnum; + + if (cache_results) { + prev_lnum = lnum; + changedtick = buf_get_changedtick(curbuf); + fnum = curbuf->b_fnum; + prev_diff_flags = diff_flags; + } } if (hlID == HLF_CHD || hlID == HLF_TXD) { int col = (int)tv_get_number(&argvars[1]) - 1; // Ignore type error in {col}. - if (col >= change_start && col <= change_end) { - hlID = HLF_TXD; // Changed text. + if (cache_results) { + if (col >= change_start && col < change_end) { + hlID = HLF_TXD; // Changed text. + } else { + hlID = HLF_CHD; // Changed line. + } } else { - hlID = HLF_CHD; // Changed line. + hlID = HLF_CHD; + for (int i = 0; i < diffline.num_changes; i++) { + bool added = diff_change_parse(&diffline, &diffline.changes[i], + &change_start, &change_end); + if (col >= change_start && col < change_end) { + hlID = added ? HLF_TXA : HLF_TXD; + break; + } + if (col < change_start) { + // the remaining changes are past this column and not relevant + break; + } + } } } rettv->vval.v_number = hlID; diff --git a/src/nvim/drawline.c b/src/nvim/drawline.c index 16e3a95121..43d9f67b5c 100644 --- a/src/nvim/drawline.c +++ b/src/nvim/drawline.c @@ -844,6 +844,18 @@ static void apply_cursorline_highlight(win_T *wp, winlinevars_T *wlv) } } +static void set_line_attr_for_diff(win_T *wp, winlinevars_T *wlv) +{ + wlv->line_attr = win_hl_attr(wp, (int)wlv->diff_hlf); + // Overlay CursorLine onto diff-mode highlight. + if (wlv->cul_attr) { + wlv->line_attr = 0 != wlv->line_attr_lowprio // Low-priority CursorLine + ? hl_combine_attr(hl_combine_attr(wlv->cul_attr, wlv->line_attr), + hl_get_underline()) + : hl_combine_attr(wlv->line_attr, wlv->cul_attr); + } +} + /// Checks if there is more inline virtual text that need to be drawn. static bool has_more_inline_virt(winlinevars_T *wlv, ptrdiff_t v) { @@ -1259,14 +1271,28 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, b int linestatus = 0; wlv.filler_lines = diff_check_with_linestatus(wp, lnum, &linestatus); + diffline_T line_changes = { 0 }; + int change_index = -1; if (wlv.filler_lines < 0 || linestatus < 0) { if (wlv.filler_lines == -1 || linestatus == -1) { - if (diff_find_change(wp, lnum, &change_start, &change_end)) { - wlv.diff_hlf = HLF_ADD; // added line - } else if (change_start == 0) { - wlv.diff_hlf = HLF_TXD; // changed text + if (diff_find_change(wp, lnum, &line_changes)) { + wlv.diff_hlf = HLF_ADD; // added line + } else if (line_changes.num_changes > 0) { + bool added = diff_change_parse(&line_changes, &line_changes.changes[0], + &change_start, &change_end); + if (change_start == 0) { + if (added) { + wlv.diff_hlf = HLF_TXA; // added text on changed line + } else { + wlv.diff_hlf = HLF_TXD; // changed text on changed line + } + } else { + wlv.diff_hlf = HLF_CHD; // unchanged text on changed line + } + change_index = 0; } else { - wlv.diff_hlf = HLF_CHD; // changed line + wlv.diff_hlf = HLF_CHD; // changed line + change_index = 0; } } else { wlv.diff_hlf = HLF_ADD; // added line @@ -1846,24 +1872,32 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, b } if (wlv.diff_hlf != (hlf_T)0) { + if (line_changes.num_changes > 0 + && change_index >= 0 + && change_index < line_changes.num_changes - 1) { + if (ptr - line + >= line_changes.changes[change_index + 1].dc_start[line_changes.bufidx]) { + change_index += 1; + } + } + bool added = false; + if (line_changes.num_changes > 0 && change_index >= 0 + && change_index < line_changes.num_changes) { + added = diff_change_parse(&line_changes, &line_changes.changes[change_index], + &change_start, &change_end); + } // When there is extra text (eg: virtual text) it gets the // diff highlighting for the line, but not for changed text. if (wlv.diff_hlf == HLF_CHD && ptr - line >= change_start && wlv.n_extra == 0) { - wlv.diff_hlf = HLF_TXD; // changed text - } - if (wlv.diff_hlf == HLF_TXD && ((ptr - line > change_end && wlv.n_extra == 0) - || (wlv.n_extra > 0 && wlv.extra_for_extmark))) { - wlv.diff_hlf = HLF_CHD; // changed line + wlv.diff_hlf = added ? HLF_TXA : HLF_TXD; // added/changed text } - wlv.line_attr = win_hl_attr(wp, (int)wlv.diff_hlf); - // Overlay CursorLine onto diff-mode highlight. - if (wlv.cul_attr) { - wlv.line_attr = 0 != wlv.line_attr_lowprio // Low-priority CursorLine - ? hl_combine_attr(hl_combine_attr(wlv.cul_attr, wlv.line_attr), - hl_get_underline()) - : hl_combine_attr(wlv.line_attr, wlv.cul_attr); + if ((wlv.diff_hlf == HLF_TXD || wlv.diff_hlf == HLF_TXA) + && ((ptr - line >= change_end && wlv.n_extra == 0) + || (wlv.n_extra > 0 && wlv.extra_for_extmark))) { + wlv.diff_hlf = HLF_CHD; // changed line } + set_line_attr_for_diff(wp, &wlv); } // Decide which of the highlight attributes to use. @@ -2727,8 +2761,9 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, b const int cuc_attr = win_hl_attr(wp, HLF_CUC); const int mc_attr = win_hl_attr(wp, HLF_MC); - if (wlv.diff_hlf == HLF_TXD) { + if (wlv.diff_hlf == HLF_TXD || wlv.diff_hlf == HLF_TXA) { wlv.diff_hlf = HLF_CHD; + set_line_attr_for_diff(wp, &wlv); } const int diff_attr = wlv.diff_hlf != 0 diff --git a/src/nvim/highlight.h b/src/nvim/highlight.h index a89d778474..03853c2ddb 100644 --- a/src/nvim/highlight.h +++ b/src/nvim/highlight.h @@ -45,6 +45,7 @@ EXTERN const char *hlf_names[] INIT( = { [HLF_CHD] = "DiffChange", [HLF_DED] = "DiffDelete", [HLF_TXD] = "DiffText", + [HLF_TXA] = "DiffTextAdd", [HLF_SC] = "SignColumn", [HLF_CONCEAL] = "Conceal", [HLF_SPB] = "SpellBad", diff --git a/src/nvim/highlight_defs.h b/src/nvim/highlight_defs.h index cbbc28311f..dba69fcf71 100644 --- a/src/nvim/highlight_defs.h +++ b/src/nvim/highlight_defs.h @@ -93,6 +93,7 @@ typedef enum { HLF_CHD, ///< Changed diff line HLF_DED, ///< Deleted diff line HLF_TXD, ///< Text Changed in diff line + HLF_TXA, ///< Text Added in changed diff line HLF_SC, ///< Sign column HLF_CONCEAL, ///< Concealed text HLF_SPB, ///< SpellBad diff --git a/src/nvim/highlight_group.c b/src/nvim/highlight_group.c index 901d2c84bc..2ecd3b9af7 100644 --- a/src/nvim/highlight_group.c +++ b/src/nvim/highlight_group.c @@ -159,6 +159,7 @@ static const char *highlight_init_both[] = { "default link CursorIM Cursor", "default link CursorLineFold FoldColumn", "default link CursorLineSign SignColumn", + "default link DiffTextAdd DiffText", "default link EndOfBuffer NonText", "default link FloatBorder NormalFloat", "default link FloatFooter FloatTitle", diff --git a/src/nvim/option_vars.h b/src/nvim/option_vars.h index 2e5698870f..0b5d0a45b4 100644 --- a/src/nvim/option_vars.h +++ b/src/nvim/option_vars.h @@ -13,11 +13,11 @@ // option_vars.h: definition of global variables for settable options #define HIGHLIGHT_INIT \ - "8:SpecialKey,~:EndOfBuffer,z:TermCursor,@:NonText,d:Directory,e:ErrorMsg," \ - "i:IncSearch,l:Search,y:CurSearch,m:MoreMsg,M:ModeMsg,n:LineNr,a:LineNrAbove,b:LineNrBelow," \ - "N:CursorLineNr,G:CursorLineSign,O:CursorLineFold,r:Question,s:StatusLine,S:StatusLineNC," \ - "c:VertSplit,t:Title,v:Visual,V:VisualNOS,w:WarningMsg,W:WildMenu,f:Folded,F:FoldColumn," \ - "A:DiffAdd,C:DiffChange,D:DiffDelete,T:DiffText,>:SignColumn,-:Conceal,B:SpellBad,P:SpellCap," \ + "8:SpecialKey,~:EndOfBuffer,z:TermCursor,@:NonText,d:Directory,e:ErrorMsg,i:IncSearch,l:Search," \ + "y:CurSearch,m:MoreMsg,M:ModeMsg,n:LineNr,a:LineNrAbove,b:LineNrBelow,N:CursorLineNr," \ + "G:CursorLineSign,O:CursorLineFold,r:Question,s:StatusLine,S:StatusLineNC,c:VertSplit,t:Title," \ + "v:Visual,V:VisualNOS,w:WarningMsg,W:WildMenu,f:Folded,F:FoldColumn,A:DiffAdd,C:DiffChange," \ + "D:DiffDelete,T:DiffText,E:DiffTextAdd,>:SignColumn,-:Conceal,B:SpellBad,P:SpellCap," \ "R:SpellRare,L:SpellLocal,+:Pmenu,=:PmenuSel,k:PmenuMatch,<:PmenuMatchSel,[:PmenuKind," \ "]:PmenuKindSel,{:PmenuExtra,}:PmenuExtraSel,x:PmenuSbar,X:PmenuThumb,*:TabLine,#:TabLineSel," \ "_:TabLineFill,!:CursorColumn,.:CursorLine,o:ColorColumn,q:QuickFixLine,z:StatusLineTerm," \ diff --git a/src/nvim/options.lua b/src/nvim/options.lua index f566582c0c..4a11078b80 100644 --- a/src/nvim/options.lua +++ b/src/nvim/options.lua @@ -2188,7 +2188,7 @@ local options = { { abbreviation = 'dip', cb = 'did_set_diffopt', - defaults = 'internal,filler,closeoff,linematch:40', + defaults = 'internal,filler,closeoff,inline:simple,linematch:40', -- Keep this in sync with diffopt_changed(). values = { 'filler', @@ -2207,6 +2207,7 @@ local options = { 'internal', 'indent-heuristic', { 'algorithm:', { 'myers', 'minimal', 'patience', 'histogram' } }, + { 'inline:', { 'none', 'simple', 'char', 'word' } }, 'linematch:', }, deny_duplicates = true, @@ -2272,6 +2273,21 @@ local options = { Use the indent heuristic for the internal diff library. + inline:{text} Highlight inline differences within a change. + See |view-diffs|. Supported values are: + + none Do not perform inline highlighting. + simple Highlight from first different + character to the last one in each + line. This is the default if nothing + is set. + char Use internal diff to perform a + character-wise diff and highlight the + difference. + word Use internal diff to perform a + |word|-wise diff and highlight the + difference. + internal Use the internal diff library. This is ignored when 'diffexpr' is set. *E960* When running out of memory when writing a diff --git a/src/nvim/optionstr.c b/src/nvim/optionstr.c index c6cc7af8cd..905656ccc9 100644 --- a/src/nvim/optionstr.c +++ b/src/nvim/optionstr.c @@ -1018,6 +1018,16 @@ int expand_set_diffopt(optexpand_T *args, int *numMatches, char ***matches) numMatches, matches); } + // Within "inline:", we have a subgroup of possible options. + const size_t inline_len = strlen("inline:"); + if (xp->xp_pattern - args->oe_set_arg >= (int)inline_len + && strncmp(xp->xp_pattern - inline_len, "inline:", inline_len) == 0) { + return expand_set_opt_string(args, + opt_dip_inline_values, + ARRAY_SIZE(opt_dip_inline_values) - 1, + numMatches, + matches); + } return FAIL; } diff --git a/test/functional/ui/cmdline_spec.lua b/test/functional/ui/cmdline_spec.lua index 99af9f9f73..59c8be3d5e 100644 --- a/test/functional/ui/cmdline_spec.lua +++ b/test/functional/ui/cmdline_spec.lua @@ -858,7 +858,7 @@ local function test_cmdline(linegrid) cmdline = { { content = { { '' } }, - hl_id = 242, + hl_id = 243, pos = 0, prompt = 'Prompt:', }, diff --git a/test/functional/ui/cursor_spec.lua b/test/functional/ui/cursor_spec.lua index ff304db54c..087ae79eb1 100644 --- a/test/functional/ui/cursor_spec.lua +++ b/test/functional/ui/cursor_spec.lua @@ -258,11 +258,11 @@ describe('ui/cursor', function() end end if m.hl_id then - m.hl_id = 65 + m.hl_id = 66 m.attr = { background = Screen.colors.DarkGray } end if m.id_lm then - m.id_lm = 72 + m.id_lm = 73 m.attr_lm = {} end end diff --git a/test/functional/ui/diff_spec.lua b/test/functional/ui/diff_spec.lua index e0d88771d3..223622eb6b 100644 --- a/test/functional/ui/diff_spec.lua +++ b/test/functional/ui/diff_spec.lua @@ -1555,7 +1555,6 @@ it('diff mode overlapped diff blocks will be merged', function() local screen = Screen.new(35, 20) command('set winwidth=10 diffopt=filler,internal') - command('args Xdifile1 Xdifile2 | vert all | windo diffthis') WriteDiffFiles('a\nb', 'x\nx') @@ -2252,3 +2251,536 @@ it('diff mode does not scroll with line("w0")', function() 9 | ]]) end) + +-- oldtest: Test_diff_inline() +it('diff mode inline highlighting', function() + write_file('Xdifile1', '') + write_file('Xdifile2', '') + finally(function() + os.remove('Xdifile1') + os.remove('Xdifile2') + end) + + local screen = Screen.new(37, 20) + screen:add_extra_attr_ids({ + [100] = { background = Screen.colors.Blue1 }, + [101] = { bold = true, background = Screen.colors.Red, foreground = Screen.colors.Blue1 }, + [102] = { background = Screen.colors.LightMagenta, foreground = Screen.colors.Blue1 }, + [103] = { bold = true, background = Screen.colors.Blue1, foreground = Screen.colors.Blue1 }, + [104] = { bold = true, background = Screen.colors.LightBlue, foreground = Screen.colors.Blue1 }, + }) + command('set winwidth=10') + command('args Xdifile1 Xdifile2 | vert all | windo diffthis | 1wincmd w') + + WriteDiffFiles('abcdef ghi jk n\nx\ny', 'aBcef gHi lm n\ny\nz') + command('set diffopt=internal,filler') + local s1 = [[ + {7: }{4:^a}{27:bcdef ghi jk}{4: n }│{7: }{4:a}{27:Bcef gHi lm}{4: n }| + {7: }{22:x }│{7: }{23:----------------}| + {7: }y │{7: }y | + {7: }{23:----------------}│{7: }{22:z }| + {1:~ }│{1:~ }|*14 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]] + screen:expect(s1) + command('set diffopt=internal,filler diffopt+=inline:none') + local s2 = [[ + {7: }{4:^abcdef ghi jk n }│{7: }{4:aBcef gHi lm n }| + {7: }{22:x }│{7: }{23:----------------}| + {7: }y │{7: }y | + {7: }{23:----------------}│{7: }{22:z }| + {1:~ }│{1:~ }|*14 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]] + screen:expect(s2) + + -- inline:simple is the same as default + command('set diffopt=internal,filler diffopt+=inline:simple') + screen:expect(s1) + + command('set diffopt=internal,filler diffopt+=inline:char') + local s3 = [[ + {7: }{4:^a}{27:b}{4:c}{27:d}{4:ef g}{27:h}{4:i }{27:jk}{4: n }│{7: }{4:a}{27:B}{4:cef g}{27:H}{4:i }{27:lm}{4: n }| + {7: }{22:x }│{7: }{23:----------------}| + {7: }y │{7: }y | + {7: }{23:----------------}│{7: }{22:z }| + {1:~ }│{1:~ }|*14 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]] + screen:expect(s3) + + command('set diffopt=internal,filler diffopt+=inline:word') + screen:expect([[ + {7: }{27:^abcdef}{4: }{27:ghi}{4: }{27:jk}{4: n }│{7: }{27:aBcef}{4: }{27:gHi}{4: }{27:lm}{4: n }| + {7: }{22:x }│{7: }{23:----------------}| + {7: }y │{7: }y | + {7: }{23:----------------}│{7: }{22:z }| + {1:~ }│{1:~ }|*14 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + + -- multiple inline values will the last one + command('set diffopt=internal,filler diffopt+=inline:none,inline:char,inline:simple') + screen:expect(s1) + command('set diffopt=internal,filler diffopt+=inline:simple,inline:word,inline:none') + screen:expect(s2) + command('set diffopt=internal,filler diffopt+=inline:simple,inline:word,inline:char') + screen:expect(s3) + + -- DiffTextAdd highlight + command('hi DiffTextAdd guibg=blue') + command('set diffopt=internal,filler diffopt+=inline:char') + screen:expect([[ + {7: }{4:^a}{27:b}{4:c}{100:d}{4:ef g}{27:h}{4:i }{27:jk}{4: n }│{7: }{4:a}{27:B}{4:cef g}{27:H}{4:i }{27:lm}{4: n }| + {7: }{22:x }│{7: }{23:----------------}| + {7: }y │{7: }y | + {7: }{23:----------------}│{7: }{22:z }| + {1:~ }│{1:~ }|*14 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + + -- Live update in insert mode + feed('isometext') + screen:expect([[ + {7: }{27:sometext^abcd}{4:ef g}│{7: }{27:aBc}{4:ef g}{27:H}{4:i }{27:lm}{4: n }| + {7: }{22:x }│{7: }{23:----------------}| + {7: }y │{7: }y | + {7: }{23:----------------}│{7: }{22:z }| + {1:~ }│{1:~ }|*14 + {3:Xdifile1 [+] }{2:Xdifile2 }| + {5:-- INSERT --} | + ]]) + feed('<Esc>') + command('silent! undo') + + -- icase simple scenarios + command('set diffopt=internal,filler diffopt+=inline:simple,icase') + screen:expect([[ + {7: }{4:^abc}{27:def ghi jk}{4: n }│{7: }{4:aBc}{27:ef gHi lm}{4: n }| + {7: }{22:x }│{7: }{23:----------------}| + {7: }y │{7: }y | + {7: }{23:----------------}│{7: }{22:z }| + {1:~ }│{1:~ }|*14 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + command('set diffopt=internal,filler diffopt+=inline:char,icase') + screen:expect([[ + {7: }{4:^abc}{100:d}{4:ef ghi }{27:jk}{4: n }│{7: }{4:aBcef gHi }{27:lm}{4: n }| + {7: }{22:x }│{7: }{23:----------------}| + {7: }y │{7: }y | + {7: }{23:----------------}│{7: }{22:z }| + {1:~ }│{1:~ }|*14 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + command('set diffopt=internal,filler diffopt+=inline:word,icase') + screen:expect([[ + {7: }{27:^abcdef}{4: ghi }{27:jk}{4: n }│{7: }{27:aBcef}{4: gHi }{27:lm}{4: n }| + {7: }{22:x }│{7: }{23:----------------}| + {7: }y │{7: }y | + {7: }{23:----------------}│{7: }{22:z }| + {1:~ }│{1:~ }|*14 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + + screen:try_resize(45, 20) + command('wincmd =') + -- diff algorithms should affect highlight + WriteDiffFiles('apples and oranges', 'oranges and apples') + command('set diffopt=internal,filler diffopt+=inline:char') + screen:expect([[ + {7: }{27:^appl}{4:es and }{27:orang}{4:es }│{7: }{27:orang}{4:es and }{27:appl}{4:es }| + {1:~ }│{1:~ }|*17 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + command('set diffopt=internal,filler diffopt+=inline:char,algorithm:patience') + screen:expect([[ + {7: }{100:^apples and }{4:oranges }│{7: }{4:oranges}{100: and apples}{4: }| + {1:~ }│{1:~ }|*17 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + + screen:try_resize(65, 20) + command('wincmd =') + -- icase: composing chars and Unicode fold case edge cases + WriteDiffFiles( + '1 - sigma in 6σ and Ὀδυσσεύς\n1 - angstrom in åå\n1 - composing: ii⃗I⃗', + '2 - Sigma in 6Σ and ὈΔΥΣΣΕΎΣ\n2 - Angstrom in ÅÅ\n2 - Composing: i⃗I⃗I⃗' + ) + command('set diffopt=internal,filler diffopt+=inline:char') + screen:expect([[ + {7: }{27:^1}{4: - }{27:s}{4:igma in 6}{27:σ}{4: and Ὀ}{27:δυσσεύς}{4: }│{7: }{27:2}{4: - }{27:S}{4:igma in 6}{27:Σ}{4: and Ὀ}{27:ΔΥΣΣΕΎΣ}{4: }| + {7: }{27:1}{4: - }{27:a}{4:ngstrom in }{27:åå}{4: }│{7: }{27:2}{4: - }{27:A}{4:ngstrom in }{27:ÅÅ}{4: }| + {7: }{27:1}{4: - }{27:c}{4:omposing: }{100:i}{4:i⃗I⃗ }│{7: }{27:2}{4: - }{27:C}{4:omposing: i⃗I⃗}{100:I⃗}{4: }| + {1:~ }│{1:~ }|*15 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + command('set diffopt=internal,filler diffopt+=inline:char,icase') + screen:expect([[ + {7: }{27:^1}{4: - sigma in 6σ and Ὀδυσσεύς }│{7: }{27:2}{4: - Sigma in 6Σ and ὈΔΥΣΣΕΎΣ }| + {7: }{27:1}{4: - angstrom in åå }│{7: }{27:2}{4: - Angstrom in ÅÅ }| + {7: }{27:1}{4: - composing: }{27:i}{4:i⃗I⃗ }│{7: }{27:2}{4: - Composing: }{27:i⃗}{4:I⃗I⃗ }| + {1:~ }│{1:~ }|*15 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + + screen:try_resize(35, 20) + command('wincmd =') + -- wide chars + WriteDiffFiles('abc😅xde一\nf🚀g', 'abcy😢de\n二f🚀g') + command('set diffopt=internal,filler diffopt+=inline:char,icase') + screen:expect([[ + {7: }{4:^abc}{27:😅x}{4:de}{100:一}{4: }│{7: }{4:abc}{27:y😢}{4:de }| + {7: }{4:f🚀g }│{7: }{100:二}{4:f🚀g }| + {1:~ }│{1:~ }|*16 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + + -- NUL char + WriteDiffFiles('1\00034\0005\0006', '1234\0005\n6') + command('set diffopt=internal,filler diffopt+=inline:char') + screen:expect([[ + {7: }{4:^1}{101:^@}{4:34}{102:^@}{4:5}{101:^@}{4:6 }│{7: }{4:1}{27:2}{4:34}{102:^@}{4:5 }| + {7: }{23:---------------}│{7: }{4:6 }| + {1:~ }│{1:~ }|*16 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + + -- word diff: always use first buffer's iskeyword and ignore others' for consistency + WriteDiffFiles('foo+bar test', 'foo+baz test') + command('set diffopt=internal,filler diffopt+=inline:word') + local sw1 = [[ + {7: }{4:^foo+}{27:bar}{4: test }│{7: }{4:foo+}{27:baz}{4: test }| + {1:~ }│{1:~ }|*17 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]] + screen:expect(sw1) + + command('set iskeyword+=+ | diffupdate') + screen:expect([[ + {7: }{27:^foo+bar}{4: test }│{7: }{27:foo+baz}{4: test }| + {1:~ }│{1:~ }|*17 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + + command('set iskeyword& | wincmd w') + command('set iskeyword+=+ | wincmd w | diffupdate') + -- Use the previous screen as 2nd buffer's iskeyword does not matter + screen:expect(sw1) + + command('windo set iskeyword& | 1wincmd w') + + screen:try_resize(69, 20) + command('wincmd =') + -- char diff: should slide highlight to whitespace boundary if possible for + -- better readability (by using forced indent-heuristics). A wrong result + -- would be if the highlight is "Bar, prefix". It should be "prefixBar, " + -- instead. + WriteDiffFiles('prefixFoo, prefixEnd', 'prefixFoo, prefixBar, prefixEnd') + command('set diffopt=internal,filler diffopt+=inline:char') + screen:expect([[ + {7: }{4:^prefixFoo, prefixEnd }│{7: }{4:prefixFoo, }{100:prefixBar, }{4:prefixEnd }| + {1:~ }│{1:~ }|*17 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + + screen:try_resize(39, 20) + command('wincmd =') + -- char diff: small gaps between inline diff blocks will be merged during refine step + -- - first segment: test that we iteratively merge small gaps after we merged + -- adjacent blocks, but only with limited number (set to 4) of iterations. + -- - second and third segments: show that we need a large enough adjacent block to + -- trigger a merge. + -- - fourth segment: small gaps are not merged when adjacent large block is + -- on a different line. + WriteDiffFiles( + 'abcdefghijklmno\nanchor1\n' + .. 'abcdefghijklmno\nanchor2\n' + .. 'abcdefghijklmno\nanchor3\n' + .. 'test\nmultiline', + 'a?c?e?g?i?k???o\nanchor1\n' + .. 'a??de?????klmno\nanchor2\n' + .. 'a??de??????lmno\nanchor3\n' + .. 't?s?\n??????i?e' + ) + command('set diffopt=internal,filler diffopt+=inline:char') + screen:expect([[ + {7: }{4:^a}{27:b}{4:c}{27:defghijklmn}{4:o }│{7: }{4:a}{27:?}{4:c}{27:?e?g?i?k???}{4:o }| + {7: }anchor1 │{7: }anchor1 | + {7: }{4:a}{27:bc}{4:de}{27:fghij}{4:klmno }│{7: }{4:a}{27:??}{4:de}{27:?????}{4:klmno }| + {7: }anchor2 │{7: }anchor2 | + {7: }{4:a}{27:bcdefghijk}{4:lmno }│{7: }{4:a}{27:??de??????}{4:lmno }| + {7: }anchor3 │{7: }anchor3 | + {7: }{4:t}{27:e}{4:s}{27:t}{4: }│{7: }{4:t}{27:?}{4:s}{27:?}{4: }| + {7: }{27:multilin}{4:e }│{7: }{27:??????i?}{4:e }| + {1:~ }│{1:~ }|*10 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + + screen:try_resize(49, 20) + command('wincmd =') + -- Test multi-line blocks and whitespace + WriteDiffFiles( + 'this is \nsometest text foo\nbaz abc def \none\nword another word\nadditional line', + 'this is some test\ntexts\nfoo bar abX Yef \noneword another word' + ) + command('set diffopt=internal,filler diffopt+=inline:char,iwhite') + screen:expect([[ + {7: }{4:^this is }│{7: }{4:this is some}{100: }{4:test }| + {7: }{4:sometest text foo }│{7: }{4:text}{100:s}{4: }| + {7: }{4:ba}{27:z}{4: ab}{27:c}{4: }{27:d}{4:ef }│{7: }{4:foo ba}{27:r}{4: ab}{27:X}{4: }{27:Y}{4:ef }| + {7: }{4:one }│{7: }{4:oneword another word }| + {7: }{4:word another word }│{7: }{23:----------------------}| + {7: }{22:additional line }│{7: }{23:----------------------}| + {1:~ }│{1:~ }|*12 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + command('set diffopt=internal,filler diffopt+=inline:word,iwhite') + screen:expect([[ + {7: }{4:^this is }│{7: }{4:this is }{27:some}{4: }{27:test}{4: }| + {7: }{27:sometest}{4: }{27:text}{4: }{27:foo}{4: }│{7: }{27:texts}{4: }| + {7: }{27:baz}{4: }{27:abc}{4: }{27:def}{4: }│{7: }{27:foo}{4: }{27:bar}{4: }{27:abX}{4: }{27:Yef}{4: }| + {7: }{27:one}{4: }│{7: }{27:oneword}{4: another word }| + {7: }{27:word}{4: another word }│{7: }{23:----------------------}| + {7: }{22:additional line }│{7: }{23:----------------------}| + {1:~ }│{1:~ }|*12 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + command('set diffopt=internal,filler diffopt+=inline:char,iwhiteeol') + screen:expect([[ + {7: }{4:^this }{100: }{4:is }│{7: }{4:this is some}{100: }{4:test }| + {7: }{4:sometest text foo }│{7: }{4:text}{100:s}{4: }| + {7: }{4:ba}{27:z}{4: ab}{27:c}{4: }{27:d}{4:ef }│{7: }{4:foo ba}{27:r}{4: ab}{27:X}{4: }{27:Y}{4:ef }| + {7: }{4:one }│{7: }{4:oneword another word }| + {7: }{4:word another word }│{7: }{23:----------------------}| + {7: }{22:additional line }│{7: }{23:----------------------}| + {1:~ }│{1:~ }|*12 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + command('set diffopt=internal,filler diffopt+=inline:word,iwhiteeol') + screen:expect([[ + {7: }{4:^this }{100: }{4:is }│{7: }{4:this is }{27:some}{4: }{27:test}{4: }| + {7: }{27:sometest}{4: }{27:text}{4: foo }│{7: }{27:texts}{4: }| + {7: }{27:baz}{4: }{27:abc}{4: }{27:def}{4: }│{7: }{4:foo }{27:bar}{4: }{27:abX}{4: }{27:Yef}{4: }| + {7: }{27:one}{4: }│{7: }{27:oneword}{4: another word }| + {7: }{27:word}{4: another word }│{7: }{23:----------------------}| + {7: }{22:additional line }│{7: }{23:----------------------}| + {1:~ }│{1:~ }|*12 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + command('set diffopt=internal,filler diffopt+=inline:char,iwhiteall') + screen:expect([[ + {7: }{4:^this is }│{7: }{4:this is some test }| + {7: }{4:sometest text foo }│{7: }{4:text}{100:s}{4: }| + {7: }{4:ba}{27:z}{4: ab}{27:c d}{4:ef }│{7: }{4:foo ba}{27:r}{4: ab}{27:X Y}{4:ef }| + {7: }{4:one }│{7: }{4:oneword another word }| + {7: }{4:word another word }│{7: }{23:----------------------}| + {7: }{22:additional line }│{7: }{23:----------------------}| + {1:~ }│{1:~ }|*12 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + command('set diffopt=internal,filler diffopt+=inline:word,iwhiteall') + screen:expect([[ + {7: }{4:^this is }│{7: }{4:this is }{27:some test}{4: }| + {7: }{27:sometest text}{4: foo }│{7: }{27:texts}{4: }| + {7: }{27:baz abc def }{4: }│{7: }{4:foo }{27:bar abX Yef }{4: }| + {7: }{27:one}{4: }│{7: }{27:oneword}{4: another word }| + {7: }{27:word}{4: another word }│{7: }{23:----------------------}| + {7: }{22:additional line }│{7: }{23:----------------------}| + {1:~ }│{1:~ }|*12 + {3:Xdifile1 }{2:Xdifile2 }| + | + ]]) + + -- newline should be highlighted too when 'list' is set + command('windo set list listchars=eol:$') + command('set diffopt=internal,filler diffopt+=inline:char') + screen:expect([[ + {7: }{4:this }{100: }{4:is }{100: }{103:$}{4: }│{7: }{4:^this is some}{100: }{4:test}{101:$}{4: }| + {7: }{4:sometest}{27: }{4:text}{27: }{4:foo}{101:$}{4: }│{7: }{4:text}{27:s}{101:$}{4: }| + {7: }{4:ba}{27:z}{4: ab}{27:c}{4: }{27:d}{4:ef }{11:$}{4: }│{7: }{4:foo}{27: }{4:ba}{27:r}{4: ab}{27:X}{4: }{27:Y}{4:ef }{100: }{11:$}{4: }| + {7: }{4:one}{103:$}{4: }│{7: }{4:oneword another word}{11:$}{4: }| + {7: }{4:word another word}{11:$}{4: }│{7: }{23:----------------------}| + {7: }{22:additional line}{104:$}{22: }│{7: }{23:----------------------}| + {1:~ }│{1:~ }|*12 + {2:Xdifile1 }{3:Xdifile2 }| + | + ]]) + command('set diffopt=internal,filler diffopt+=inline:char,iwhite') + screen:expect([[ + {7: }{4:this is }{11:$}{4: }│{7: }{4:^this is some}{100: }{4:test}{11:$}{4: }| + {7: }{4:sometest text foo}{11:$}{4: }│{7: }{4:text}{100:s}{11:$}{4: }| + {7: }{4:ba}{27:z}{4: ab}{27:c}{4: }{27:d}{4:ef }{11:$}{4: }│{7: }{4:foo ba}{27:r}{4: ab}{27:X}{4: }{27:Y}{4:ef }{11:$}{4: }| + {7: }{4:one}{103:$}{4: }│{7: }{4:oneword another word}{11:$}{4: }| + {7: }{4:word another word}{11:$}{4: }│{7: }{23:----------------------}| + {7: }{22:additional line}{104:$}{22: }│{7: }{23:----------------------}| + {1:~ }│{1:~ }|*12 + {2:Xdifile1 }{3:Xdifile2 }| + | + ]]) + command('set diffopt=internal,filler diffopt+=inline:char,iwhiteeol') + screen:expect([[ + {7: }{4:this }{100: }{4:is }{11:$}{4: }│{7: }{4:^this is some}{100: }{4:test}{11:$}{4: }| + {7: }{4:sometest text foo}{11:$}{4: }│{7: }{4:text}{100:s}{11:$}{4: }| + {7: }{4:ba}{27:z}{4: ab}{27:c}{4: }{27:d}{4:ef }{11:$}{4: }│{7: }{4:foo ba}{27:r}{4: ab}{27:X}{4: }{27:Y}{4:ef }{11:$}{4: }| + {7: }{4:one}{103:$}{4: }│{7: }{4:oneword another word}{11:$}{4: }| + {7: }{4:word another word}{11:$}{4: }│{7: }{23:----------------------}| + {7: }{22:additional line}{104:$}{22: }│{7: }{23:----------------------}| + {1:~ }│{1:~ }|*12 + {2:Xdifile1 }{3:Xdifile2 }| + | + ]]) + command('set diffopt=internal,filler diffopt+=inline:char,iwhiteall') + screen:expect([[ + {7: }{4:this is }{11:$}{4: }│{7: }{4:^this is some test}{11:$}{4: }| + {7: }{4:sometest text foo}{11:$}{4: }│{7: }{4:text}{100:s}{11:$}{4: }| + {7: }{4:ba}{27:z}{4: ab}{27:c d}{4:ef }{11:$}{4: }│{7: }{4:foo ba}{27:r}{4: ab}{27:X Y}{4:ef }{11:$}{4: }| + {7: }{4:one}{11:$}{4: }│{7: }{4:oneword another word}{11:$}{4: }| + {7: }{4:word another word}{11:$}{4: }│{7: }{23:----------------------}| + {7: }{22:additional line}{104:$}{22: }│{7: }{23:----------------------}| + {1:~ }│{1:~ }|*12 + {2:Xdifile1 }{3:Xdifile2 }| + | + ]]) + command('windo set nolist') +end) + +-- oldtest: Test_diff_inline_multibuffer() +it('diff mode inline highlighting with 3 buffers', function() + write_file('Xdifile1', '') + write_file('Xdifile2', '') + write_file('Xdifile3', '') + finally(function() + os.remove('Xdifile1') + os.remove('Xdifile2') + os.remove('Xdifile3') + end) + + local screen = Screen.new(75, 20) + screen:add_extra_attr_ids({ + [100] = { background = Screen.colors.Blue1 }, + }) + command('args Xdifile1 Xdifile2 Xdifile3 | vert all | windo diffthis | 1wincmd w') + command('wincmd =') + command('hi DiffTextAdd guibg=Blue') + + WriteDiffFiles3( + 'That is buffer1.\nanchor\nSome random text\nanchor', + 'This is buffer2.\nanchor\nSome text\nanchor\nbuffer2/3', + 'This is buffer3. Last.\nanchor\nSome more\ntext here.\nanchor\nonly in buffer2/3\nnot in buffer1' + ) + command('set diffopt=internal,filler diffopt+=inline:char') + local s1 = [[ + {7: }{4:^Th}{27:at}{4: is buffer}{27:1}{4:. }│{7: }{4:Th}{27:is}{4: is buffer}{27:2}{4:. }│{7: }{4:Th}{27:is}{4: is buffer}{27:3. Last}{4:.}| + {7: }anchor │{7: }anchor │{7: }anchor | + {7: }{4:Some }{27:random }{4:text }│{7: }{4:Some text }│{7: }{4:Some }{27:more}{4: }| + {7: }{23:-----------------------}│{7: }{23:----------------------}│{7: }{4:text}{100: here.}{4: }| + {7: }anchor │{7: }anchor │{7: }anchor | + {7: }{23:-----------------------}│{7: }{4:buffer2/3 }│{7: }{100:only in }{4:buffer2/3 }| + {7: }{23:-----------------------}│{7: }{23:----------------------}│{7: }{22:not in buffer1 }| + {1:~ }│{1:~ }│{1:~ }|*11 + {3:Xdifile1 }{2:Xdifile2 Xdifile3 }| + | + ]] + screen:expect(s1) + + -- Close one of the buffers and make sure it updates correctly + command('diffoff') + screen:expect([[ + ^That is buffer1. │{7: }{4:This is buffer}{27:2}{4:. }│{7: }{4:This is buffer}{27:3. Last}{4:.}| + anchor │{7: }anchor │{7: }anchor | + Some random text │{7: }{4:Some text }│{7: }{4:Some }{100:more}{4: }| + anchor │{7: }{23:----------------------}│{7: }{4:text}{100: here.}{4: }| + {1:~ }│{7: }anchor │{7: }anchor | + {1:~ }│{7: }{4:buffer2/3 }│{7: }{100:only in }{4:buffer2/3 }| + {1:~ }│{7: }{23:----------------------}│{7: }{22:not in buffer1 }| + {1:~ }│{1:~ }│{1:~ }|*11 + {3:Xdifile1 }{2:Xdifile2 Xdifile3 }| + | + ]]) + + -- Update text in the non-diff buffer and nothing should be changed + feed('isometext') + screen:expect([[ + sometext^That is buffer1. │{7: }{4:This is buffer}{27:2}{4:. }│{7: }{4:This is buffer}{27:3. Last}{4:.}| + anchor │{7: }anchor │{7: }anchor | + Some random text │{7: }{4:Some text }│{7: }{4:Some }{100:more}{4: }| + anchor │{7: }{23:----------------------}│{7: }{4:text}{100: here.}{4: }| + {1:~ }│{7: }anchor │{7: }anchor | + {1:~ }│{7: }{4:buffer2/3 }│{7: }{100:only in }{4:buffer2/3 }| + {1:~ }│{7: }{23:----------------------}│{7: }{22:not in buffer1 }| + {1:~ }│{1:~ }│{1:~ }|*11 + {3:Xdifile1 [+] }{2:Xdifile2 Xdifile3 }| + {5:-- INSERT --} | + ]]) + feed('<Esc>') + command('silent! undo') + + command('diffthis') + screen:expect(s1) + + -- Test that removing first buffer from diff will in turn use the next + -- earliest buffer's iskeyword during word diff. + WriteDiffFiles3('This+is=a-setence', 'This+is=another-setence', 'That+is=a-setence') + command('set iskeyword+=+ | 2wincmd w | set iskeyword+=- | 1wincmd w') + command('set diffopt=internal,filler diffopt+=inline:word') + local s4 = [[ + {7: }{27:^This+is}{4:=}{27:a}{4:-setence }│{7: }{27:This+is}{4:=}{27:another}{4:-setenc}│{7: }{27:That+is}{4:=}{27:a}{4:-setence }| + {1:~ }│{1:~ }│{1:~ }|*17 + {3:Xdifile1 }{2:Xdifile2 Xdifile3 }| + | + ]] + screen:expect(s4) + command('diffoff') + screen:expect([[ + ^This+is=a-setence │{7: }{27:This}{4:+is=}{27:another-setenc}│{7: }{27:That}{4:+is=}{27:a-setence}{4: }| + {1:~ }│{1:~ }│{1:~ }|*17 + {3:Xdifile1 }{2:Xdifile2 Xdifile3 }| + | + ]]) + command('diffthis') + screen:expect(s4) + + -- Test multi-buffer char diff refinement, and that removing a buffer from + -- diff will update the others properly. + WriteDiffFiles3('abcdefghijkYmYYY', 'aXXdXXghijklmnop', 'abcdefghijkYmYop') + command('set diffopt=internal,filler diffopt+=inline:char') + local s6 = [[ + {7: }{4:^a}{27:bcdef}{4:ghijk}{27:YmYYY}{4: }│{7: }{4:a}{27:XXdXX}{4:ghijk}{27:lmnop}{4: }│{7: }{4:a}{27:bcdef}{4:ghijk}{27:YmYop}{4: }| + {1:~ }│{1:~ }│{1:~ }|*17 + {3:Xdifile1 }{2:Xdifile2 Xdifile3 }| + | + ]] + screen:expect(s6) + command('diffoff') + screen:expect([[ + ^abcdefghijkYmYYY │{7: }{4:a}{27:XXdXX}{4:ghijk}{27:l}{4:m}{27:n}{4:op }│{7: }{4:a}{27:bcdef}{4:ghijk}{27:Y}{4:m}{27:Y}{4:op }| + {1:~ }│{1:~ }│{1:~ }|*17 + {3:Xdifile1 }{2:Xdifile2 Xdifile3 }| + | + ]]) + command('diffthis') + screen:expect(s6) +end) diff --git a/test/functional/ui/messages_spec.lua b/test/functional/ui/messages_spec.lua index 9b01150411..94faafa9be 100644 --- a/test/functional/ui/messages_spec.lua +++ b/test/functional/ui/messages_spec.lua @@ -229,11 +229,11 @@ describe('ui/ext_messages', function() { content = { { '\n@character ' }, - { 'xxx', 26, 155 }, + { 'xxx', 26, 156 }, { ' ' }, { 'links to', 18, 5 }, { ' Character\n@character.special ' }, - { 'xxx', 16, 156 }, + { 'xxx', 16, 157 }, { ' ' }, { 'links to', 18, 5 }, { ' SpecialChar' }, @@ -300,7 +300,7 @@ describe('ui/ext_messages', function() cmdline = { { abort = false } }, messages = { { - content = { { 'Error', 9, 6 }, { 'Message', 16, 99 } }, + content = { { 'Error', 9, 6 }, { 'Message', 16, 100 } }, history = true, kind = 'echoerr', }, @@ -940,7 +940,7 @@ describe('ui/ext_messages', function() ^ | {1:~ }|*4 ]], - ruler = { { '0,0-1 All', 9, 61 } }, + ruler = { { '0,0-1 All', 9, 62 } }, }) command('hi clear MsgArea') feed('i') diff --git a/test/old/testdir/gen_opt_test.vim b/test/old/testdir/gen_opt_test.vim index 2127945624..fb0af3de0a 100644 --- a/test/old/testdir/gen_opt_test.vim +++ b/test/old/testdir/gen_opt_test.vim @@ -214,10 +214,12 @@ let test_values = { \ 'closeoff', 'hiddenoff', 'foldcolumn:0', 'foldcolumn:12', \ 'followwrap', 'internal', 'indent-heuristic', 'algorithm:myers', \ 'icase,iwhite', 'algorithm:minimal', 'algorithm:patience', - \ 'algorithm:histogram', 'linematch:5'], + \ 'algorithm:histogram', 'inline:none', 'inline:simple', + \ 'inline:char', 'inline:word', 'inline:char,inline:word', 'linematch:5'], \ ['xxx', 'foldcolumn:', 'foldcolumn:x', 'foldcolumn:xxx', \ 'linematch:', 'linematch:x', 'linematch:xxx', 'algorithm:', - \ 'algorithm:xxx', 'context:', 'context:x', 'context:xxx']], + \ 'algorithm:xxx', 'context:', 'context:x', 'context:xxx', + \ 'inline:xxx']], \ 'display': [['', 'lastline', 'truncate', 'uhex', 'lastline,uhex'], \ ['xxx']], \ 'eadirection': [['both', 'ver', 'hor'], ['xxx', 'ver,hor']], diff --git a/test/old/testdir/setup.vim b/test/old/testdir/setup.vim index 87287a57ff..c077eb021a 100644 --- a/test/old/testdir/setup.vim +++ b/test/old/testdir/setup.vim @@ -3,7 +3,7 @@ if exists('s:did_load') set commentstring=/*\ %s\ */ set complete=.,w,b,u,t,i set define=^\\s*#\\s*define - set diffopt=internal,filler,closeoff + set diffopt=internal,filler,closeoff,inline:simple set directory^=. set display= set fillchars=vert:\|,foldsep:\|,fold:- diff --git a/test/old/testdir/test_diffmode.vim b/test/old/testdir/test_diffmode.vim index 0d20085360..b5bfef1f1d 100644 --- a/test/old/testdir/test_diffmode.vim +++ b/test/old/testdir/test_diffmode.vim @@ -430,13 +430,13 @@ endfunc func Common_icase_test() edit one - call setline(1, ['One', 'Two', 'Three', 'Four', 'Fi#ve']) + call setline(1, ['One', 'Two', 'Three', 'Four', 'Fi#vϵ', 'Si⃗x', 'Se⃗ve⃗n']) redraw let normattr = screenattr(1, 1) diffthis botright vert new two - call setline(1, ['one', 'TWO', 'Three ', 'Four', 'fI=VE']) + call setline(1, ['one', 'TWO', 'Three ', 'Four', 'fI=VΕ', 'SI⃗x', 'SEvE⃗n']) diffthis redraw @@ -444,10 +444,13 @@ func Common_icase_test() call assert_equal(normattr, screenattr(2, 1)) call assert_notequal(normattr, screenattr(3, 1)) call assert_equal(normattr, screenattr(4, 1)) + call assert_equal(normattr, screenattr(6, 2)) + call assert_notequal(normattr, screenattr(7, 2)) let dtextattr = screenattr(5, 3) call assert_notequal(dtextattr, screenattr(5, 1)) call assert_notequal(dtextattr, screenattr(5, 5)) + call assert_notequal(dtextattr, screenattr(7, 4)) diffoff! %bwipe! @@ -779,10 +782,10 @@ endfunc func Test_diff_hlID() new - call setline(1, [1, 2, 3]) + call setline(1, [1, 2, 3, 'Yz', 'a dxxg',]) diffthis vnew - call setline(1, ['1x', 2, 'x', 3]) + call setline(1, ['1x', 2, 'x', 3, 'yx', 'abc defg']) diffthis redraw @@ -793,6 +796,26 @@ func Test_diff_hlID() call diff_hlID(2, 1)->synIDattr("name")->assert_equal("") call diff_hlID(3, 1)->synIDattr("name")->assert_equal("DiffAdd") eval 4->diff_hlID(1)->synIDattr("name")->assert_equal("") + call diff_hlID(5, 1)->synIDattr("name")->assert_equal("DiffText") + call diff_hlID(5, 2)->synIDattr("name")->assert_equal("DiffText") + + set diffopt+=icase " test that caching is invalidated by diffopt change + call diff_hlID(5, 1)->synIDattr("name")->assert_equal("DiffChange") + set diffopt-=icase + call diff_hlID(5, 1)->synIDattr("name")->assert_equal("DiffText") + + call diff_hlID(6, 1)->synIDattr("name")->assert_equal("DiffChange") + call diff_hlID(6, 2)->synIDattr("name")->assert_equal("DiffText") + call diff_hlID(6, 4)->synIDattr("name")->assert_equal("DiffText") + call diff_hlID(6, 7)->synIDattr("name")->assert_equal("DiffText") + call diff_hlID(6, 8)->synIDattr("name")->assert_equal("DiffChange") + set diffopt+=inline:char + call diff_hlID(6, 1)->synIDattr("name")->assert_equal("DiffChange") + call diff_hlID(6, 2)->synIDattr("name")->assert_equal("DiffTextAdd") + call diff_hlID(6, 4)->synIDattr("name")->assert_equal("DiffChange") + call diff_hlID(6, 7)->synIDattr("name")->assert_equal("DiffText") + call diff_hlID(6, 8)->synIDattr("name")->assert_equal("DiffChange") + set diffopt-=inline:char wincmd w call assert_equal(synIDattr(diff_hlID(1, 1), "name"), "DiffChange") @@ -2099,6 +2122,178 @@ func Test_diff_topline_noscroll() call StopVimInTerminal(buf) endfunc +" Test inline highlighting which shows what's different within each diff block +func Test_diff_inline() + CheckScreendump + + call WriteDiffFiles(0, [], []) + let buf = RunVimInTerminal('-d Xdifile1 Xdifile2', {}) + call term_sendkeys(buf, ":set autoread\<CR>\<c-w>w:set autoread\<CR>\<c-w>w") + + call WriteDiffFiles(buf, ["abcdef ghi jk n", "x", "y"], ["aBcef gHi lm n", "y", "z"]) + call VerifyInternal(buf, "Test_diff_inline_01", "") + call VerifyInternal(buf, "Test_diff_inline_02", " diffopt+=inline:none") + + " inline:simple is the same as default + call VerifyInternal(buf, "Test_diff_inline_01", " diffopt+=inline:simple") + + call VerifyInternal(buf, "Test_diff_inline_03", " diffopt+=inline:char") + call VerifyInternal(buf, "Test_diff_inline_04", " diffopt+=inline:word") + + " multiple inline values will the last one + call VerifyInternal(buf, "Test_diff_inline_01", " diffopt+=inline:none,inline:char,inline:simple") + call VerifyInternal(buf, "Test_diff_inline_02", " diffopt+=inline:simple,inline:word,inline:none") + call VerifyInternal(buf, "Test_diff_inline_03", " diffopt+=inline:simple,inline:word,inline:char") + + " DiffTextAdd highlight + call term_sendkeys(buf, ":hi DiffTextAdd ctermbg=blue\<CR>") + call VerifyInternal(buf, "Test_diff_inline_05", " diffopt+=inline:char") + + " Live update in insert mode + call term_sendkeys(buf, "\<Esc>isometext") + call VerifyScreenDump(buf, "Test_diff_inline_06", {}) + call term_sendkeys(buf, "\<Esc>u") + + " icase simple scenarios + call VerifyInternal(buf, "Test_diff_inline_07", " diffopt+=inline:simple,icase") + call VerifyInternal(buf, "Test_diff_inline_08", " diffopt+=inline:char,icase") + call VerifyInternal(buf, "Test_diff_inline_09", " diffopt+=inline:word,icase") + + " diff algorithms should affect highlight + call WriteDiffFiles(buf, ["apples and oranges"], ["oranges and apples"]) + call VerifyInternal(buf, "Test_diff_inline_10", " diffopt+=inline:char") + call VerifyInternal(buf, "Test_diff_inline_11", " diffopt+=inline:char,algorithm:patience") + + " icase: composing chars and Unicode fold case edge cases + call WriteDiffFiles(buf, + \ ["1 - sigma in 6σ and Ὀδυσσεύς", "1 - angstrom in åå", "1 - composing: ii⃗I⃗"], + \ ["2 - Sigma in 6Σ and ὈΔΥΣΣΕΎΣ", "2 - Angstrom in ÅÅ", "2 - Composing: i⃗I⃗I⃗"]) + call VerifyInternal(buf, "Test_diff_inline_12", " diffopt+=inline:char") + call VerifyInternal(buf, "Test_diff_inline_13", " diffopt+=inline:char,icase") + + " wide chars + call WriteDiffFiles(buf, ["abc😅xde一", "f🚀g"], ["abcy😢de", "二f🚀g"]) + call VerifyInternal(buf, "Test_diff_inline_14", " diffopt+=inline:char,icase") + + " NUL char (\n below is internally substituted as NUL) + call WriteDiffFiles(buf, ["1\n34\n5\n6"], ["1234\n5", "6"]) + call VerifyInternal(buf, "Test_diff_inline_15", " diffopt+=inline:char") + + " word diff: always use first buffer's iskeyword and ignore others' for consistency + call WriteDiffFiles(buf, ["foo+bar test"], ["foo+baz test"]) + call VerifyInternal(buf, "Test_diff_inline_word_01", " diffopt+=inline:word") + + call term_sendkeys(buf, ":set iskeyword+=+\<CR>:diffupdate\<CR>") + call VerifyInternal(buf, "Test_diff_inline_word_02", " diffopt+=inline:word") + + call term_sendkeys(buf, ":set iskeyword&\<CR>:wincmd w\<CR>") + call term_sendkeys(buf, ":set iskeyword+=+\<CR>:wincmd w\<CR>:diffupdate\<CR>") + " Use the previous screen dump as 2nd buffer's iskeyword does not matter + call VerifyInternal(buf, "Test_diff_inline_word_01", " diffopt+=inline:word") + + call term_sendkeys(buf, ":windo set iskeyword&\<CR>:1wincmd w\<CR>") + + " char diff: should slide highlight to whitespace boundary if possible for + " better readability (by using forced indent-heuristics). A wrong result + " would be if the highlight is "Bar, prefix". It should be "prefixBar, " + " instead. + call WriteDiffFiles(buf, ["prefixFoo, prefixEnd"], ["prefixFoo, prefixBar, prefixEnd"]) + call VerifyInternal(buf, "Test_diff_inline_char_01", " diffopt+=inline:char") + + " char diff: small gaps between inline diff blocks will be merged during refine step + " - first segment: test that we iteratively merge small gaps after we merged + " adjacent blocks, but only with limited number (set to 4) of iterations. + " - second and third segments: show that we need a large enough adjacent block to + " trigger a merge. + " - fourth segment: small gaps are not merged when adjacent large block is + " on a different line. + call WriteDiffFiles(buf, + \ ["abcdefghijklmno", "anchor1", + \ "abcdefghijklmno", "anchor2", + \ "abcdefghijklmno", "anchor3", + \ "test", "multiline"], + \ ["a?c?e?g?i?k???o", "anchor1", + \ "a??de?????klmno", "anchor2", + \ "a??de??????lmno", "anchor3", + \ "t?s?", "??????i?e"]) + call VerifyInternal(buf, "Test_diff_inline_char_02", " diffopt+=inline:char") + + " Test multi-line blocks and whitespace + call WriteDiffFiles(buf, + \ ["this is ", "sometest text foo", "baz abc def ", "one", "word another word", "additional line"], + \ ["this is some test", "texts", "foo bar abX Yef ", "oneword another word"]) + call VerifyInternal(buf, "Test_diff_inline_multiline_01", " diffopt+=inline:char,iwhite") + call VerifyInternal(buf, "Test_diff_inline_multiline_02", " diffopt+=inline:word,iwhite") + call VerifyInternal(buf, "Test_diff_inline_multiline_03", " diffopt+=inline:char,iwhiteeol") + call VerifyInternal(buf, "Test_diff_inline_multiline_04", " diffopt+=inline:word,iwhiteeol") + call VerifyInternal(buf, "Test_diff_inline_multiline_05", " diffopt+=inline:char,iwhiteall") + call VerifyInternal(buf, "Test_diff_inline_multiline_06", " diffopt+=inline:word,iwhiteall") + + " newline should be highlighted too when 'list' is set + call term_sendkeys(buf, ":windo set list\<CR>") + call VerifyInternal(buf, "Test_diff_inline_multiline_07", " diffopt+=inline:char") + call VerifyInternal(buf, "Test_diff_inline_multiline_08", " diffopt+=inline:char,iwhite") + call VerifyInternal(buf, "Test_diff_inline_multiline_09", " diffopt+=inline:char,iwhiteeol") + call VerifyInternal(buf, "Test_diff_inline_multiline_10", " diffopt+=inline:char,iwhiteall") + call term_sendkeys(buf, ":windo set nolist\<CR>") + + call StopVimInTerminal(buf) +endfunc + +func Test_diff_inline_multibuffer() + CheckScreendump + + call WriteDiffFiles3(0, [], [], []) + let buf = RunVimInTerminal('-d Xdifile1 Xdifile2 Xdifile3', {}) + call term_sendkeys(buf, ":windo set autoread\<CR>:1wincmd w\<CR>") + call term_sendkeys(buf, ":hi DiffTextAdd ctermbg=blue\<CR>") + + call WriteDiffFiles3(buf, + \ ["That is buffer1.", "anchor", "Some random text", "anchor"], + \ ["This is buffer2.", "anchor", "Some text", "anchor", "buffer2/3"], + \ ["This is buffer3. Last.", "anchor", "Some more", "text here.", "anchor", "only in buffer2/3", "not in buffer1"]) + call VerifyInternal(buf, "Test_diff_inline_multibuffer_01", " diffopt+=inline:char") + + " Close one of the buffer and make sure it updates correctly + call term_sendkeys(buf, ":diffoff\<CR>") + call VerifyInternal(buf, "Test_diff_inline_multibuffer_02", " diffopt+=inline:char") + + " Update text in the non-diff buffer and nothing should be changed + call term_sendkeys(buf, "\<Esc>isometext") + call VerifyScreenDump(buf, "Test_diff_inline_multibuffer_03", {}) + call term_sendkeys(buf, "\<Esc>u") + + call term_sendkeys(buf, ":diffthis\<CR>") + call VerifyInternal(buf, "Test_diff_inline_multibuffer_01", " diffopt+=inline:char") + + " Test that removing first buffer from diff will in turn use the next + " earliest buffer's iskeyword during word diff. + call WriteDiffFiles3(buf, + \ ["This+is=a-setence"], + \ ["This+is=another-setence"], + \ ["That+is=a-setence"]) + call term_sendkeys(buf, ":set iskeyword+=+\<CR>:2wincmd w\<CR>:set iskeyword+=-\<CR>:1wincmd w\<CR>") + call VerifyInternal(buf, "Test_diff_inline_multibuffer_04", " diffopt+=inline:word") + call term_sendkeys(buf, ":diffoff\<CR>") + call VerifyInternal(buf, "Test_diff_inline_multibuffer_05", " diffopt+=inline:word") + call term_sendkeys(buf, ":diffthis\<CR>") + call VerifyInternal(buf, "Test_diff_inline_multibuffer_04", " diffopt+=inline:word") + + " Test multi-buffer char diff refinement, and that removing a buffer from + " diff will update the others properly. + call WriteDiffFiles3(buf, + \ ["abcdefghijkYmYYY"], + \ ["aXXdXXghijklmnop"], + \ ["abcdefghijkYmYop"]) + call VerifyInternal(buf, "Test_diff_inline_multibuffer_06", " diffopt+=inline:char") + call term_sendkeys(buf, ":diffoff\<CR>") + call VerifyInternal(buf, "Test_diff_inline_multibuffer_07", " diffopt+=inline:char") + call term_sendkeys(buf, ":diffthis\<CR>") + call VerifyInternal(buf, "Test_diff_inline_multibuffer_06", " diffopt+=inline:char") + + call StopVimInTerminal(buf) +endfunc + func Test_diffget_diffput_linematch() CheckScreendump call delete('.Xdifile1.swp') diff --git a/test/old/testdir/test_options.vim b/test/old/testdir/test_options.vim index 9d8122cd8b..f3dcb3bbb7 100644 --- a/test/old/testdir/test_options.vim +++ b/test/old/testdir/test_options.vim @@ -629,10 +629,11 @@ func Test_set_completion_string_values() " call assert_equal([], getcompletion('set completepopup=bogusname:', 'cmdline')) " set previewpopup& completepopup& - " diffopt: special handling of algorithm:<alg_list> + " diffopt: special handling of algorithm:<alg_list> and inline:<inline_type> call assert_equal('filler', getcompletion('set diffopt+=', 'cmdline')[0]) call assert_equal([], getcompletion('set diffopt+=iblank,foldcolumn:', 'cmdline')) call assert_equal('patience', getcompletion('set diffopt+=iblank,algorithm:pat*', 'cmdline')[0]) + call assert_equal('char', getcompletion('set diffopt+=iwhite,inline:ch*', 'cmdline')[0]) " highlight: special parsing, including auto-completing highlight groups " after ':' @@ -722,7 +723,7 @@ func Test_set_completion_string_values() call assert_equal([], getcompletion('set diffopt-=', 'cmdline')) " Test all possible values call assert_equal(['filler', 'context:', 'iblank', 'icase', 'iwhite', 'iwhiteall', 'iwhiteeol', 'horizontal', - \ 'vertical', 'closeoff', 'hiddenoff', 'foldcolumn:', 'followwrap', 'internal', 'indent-heuristic', 'algorithm:', 'linematch:'], + \ 'vertical', 'closeoff', 'hiddenoff', 'foldcolumn:', 'followwrap', 'internal', 'indent-heuristic', 'algorithm:', 'inline:', 'linematch:'], \ getcompletion('set diffopt=', 'cmdline')) set diffopt& |