Skip to content

Commit d70b971

Browse files
zeertzjqychin
andcommitted
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:". 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 vim/vim@9943d47 Co-authored-by: Yee Cheng Chin
1 parent ae98d0a commit d70b971

22 files changed

+1042
-112
lines changed

runtime/colors/vim.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ hi('WildMenu', { fg = 'Black', bg = 'Yellow', ctermfg = 'Black', cterm
4747
hi('VertSplit', { link = 'Normal' })
4848
hi('WinSeparator', { link = 'VertSplit' })
4949
hi('WinBarNC', { link = 'WinBar' })
50+
hi('DiffTextAdd', { link = 'DiffText' })
5051
hi('EndOfBuffer', { link = 'NonText' })
5152
hi('LineNrAbove', { link = 'LineNr' })
5253
hi('LineNrBelow', { link = 'LineNr' })

runtime/doc/diff.txt

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,29 @@ The diffs are highlighted with these groups:
214214
|hl-DiffAdd| DiffAdd Added (inserted) lines. These lines exist in
215215
this buffer but not in another.
216216
|hl-DiffChange| DiffChange Changed lines.
217-
|hl-DiffText| DiffText Changed text inside a Changed line. Vim
218-
finds the first character that is different,
219-
and the last character that is different
220-
(searching from the end of the line). The
221-
text in between is highlighted. This means
222-
that parts in the middle that are still the
223-
same are highlighted anyway. The 'diffopt'
224-
flags "iwhite" and "icase" are used here.
217+
|hl-DiffText| DiffText Changed text inside a Changed line. Exact
218+
behavior depends on the `inline:` setting in
219+
'diffopt'.
220+
With `inline:` set to "simple", Vim finds the
221+
first character that is different, and the
222+
last character that is different (searching
223+
from the end of the line). The text in
224+
between is highlighted. This means that parts
225+
in the middle that are still the same are
226+
highlighted anyway. The 'diffopt' flags
227+
"iwhite" and "icase" are used here.
228+
With `inline:` set to "char" or "word", Vim
229+
uses the internal diff library to perform a
230+
detailed diff between the changed blocks and
231+
highlight the exact difference between the
232+
two. Will respect any 'diffopt' flag that
233+
affects internal diff.
234+
Not used when `inline:` set to "none".
235+
|hl-DiffTextAdd| DiffTextAdd Added text inside a Changed line. Similar to
236+
DiffText, but used when there is no
237+
corresponding text in other buffers. Will not
238+
be used when `inline:` is set to "simple" or
239+
"none".
225240
|hl-DiffDelete| DiffDelete Deleted lines. Also called filler lines,
226241
because they don't really exist in this
227242
buffer.

runtime/doc/news.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ EVENTS
6666

6767
HIGHLIGHTS
6868

69-
todo
69+
|hl-DiffTextAdd| highlights added text within a changed line.
7070

7171
LSP
7272

@@ -78,7 +78,7 @@ LUA
7878

7979
OPTIONS
8080

81-
todo
81+
'diffopt' "inline" configures diff highlighting for changes within a line.
8282

8383
PLUGINS
8484

runtime/doc/options.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,7 @@ A jump table for the options with a short description can be found at |Q_op|.
20562056
security reasons.
20572057

20582058
*'diffopt'* *'dip'*
2059-
'diffopt' 'dip' string (default "internal,filler,closeoff,linematch:40")
2059+
'diffopt' 'dip' string (default "internal,filler,closeoff,inline:simple,linematch:40")
20602060
global
20612061
Option settings for diff mode. It can consist of the following items.
20622062
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|.
21192119
Use the indent heuristic for the internal
21202120
diff library.
21212121

2122+
inline:{text} Highlight inline differences within a change.
2123+
See |view-diffs|. Supported values are:
2124+
2125+
none Do not perform inline highlighting.
2126+
simple Highlight from first different
2127+
character to the last one in each
2128+
line. This is the default if nothing
2129+
is set.
2130+
char Use internal diff to perform a
2131+
character-wise diff and highlight the
2132+
difference.
2133+
word Use internal diff to perform a
2134+
|word|-wise diff and highlight the
2135+
difference.
2136+
21222137
internal Use the internal diff library. This is
21232138
ignored when 'diffexpr' is set. *E960*
21242139
When running out of memory when writing a

runtime/doc/syntax.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5177,8 +5177,11 @@ DiffChange Diff mode: Changed line. |diff.txt|
51775177
DiffDelete Diff mode: Deleted line. |diff.txt|
51785178
*hl-DiffText*
51795179
DiffText Diff mode: Changed text within a changed line. |diff.txt|
5180+
*hl-DiffTextAdd*
5181+
DiffTextAdd Diff mode: Added text within a changed line. Linked to
5182+
|hl-DiffText| by default. |diff.txt|
51805183
*hl-EndOfBuffer*
5181-
EndOfBuffer Filler lines (~) after the end of the buffer.
5184+
EndOfBuffer Filler lines (~) after the last line in the buffer.
51825185
By default, this is highlighted like |hl-NonText|.
51835186
*hl-TermCursor*
51845187
TermCursor Cursor in a focused terminal.

runtime/doc/vim_diff.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Defaults *defaults* *nvim-defaults*
4848
- 'complete' excludes "i"
4949
- 'completeopt' defaults to "menu,popup"
5050
- 'define' defaults to "". The C ftplugin sets it to "^\\s*#\\s*define"
51-
- 'diffopt' defaults to "internal,filler,closeoff,linematch:40"
51+
- 'diffopt' defaults to "internal,filler,closeoff,inline:simple,linematch:40"
5252
- 'directory' defaults to ~/.local/state/nvim/swap// (|xdg|), auto-created
5353
- 'display' defaults to "lastline"
5454
- 'encoding' is UTF-8 (cf. 'fileencoding' for file-content encoding)

runtime/lua/vim/_meta/options.lua

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runtime/syntax/vim.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ syn case ignore
6060
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
6161

6262
" Default highlighting groups {{{2
63-
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
63+
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
6464
syn match vimHLGroup contained "\<Conceal\>"
6565
syn keyword vimOnlyHLGroup contained Menu Scrollbar ToolbarButton ToolbarLine Tooltip VisualNOS
6666
syn keyword nvimHLGroup contained FloatBorder FloatFooter FloatTitle MsgSeparator NormalFloat NormalNC Substitute TermCursor VisualNC Whitespace WinBar WinBarNC WinSeparator

src/nvim/buffer_defs.h

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -743,25 +743,53 @@ struct file_buffer {
743743
// Stuff for diff mode.
744744
#define DB_COUNT 8 // up to four buffers can be diff'ed
745745

746-
// Each diffblock defines where a block of lines starts in each of the buffers
747-
// and how many lines it occupies in that buffer. When the lines are missing
748-
// in the buffer the df_count[] is zero. This is all counted in
749-
// buffer lines.
750-
// There is always at least one unchanged line in between the diffs.
751-
// Otherwise it would have been included in the diff above or below it.
752-
// df_lnum[] + df_count[] is the lnum below the change. When in one buffer
753-
// lines have been inserted, in the other buffer df_lnum[] is the line below
754-
// the insertion and df_count[] is zero. When appending lines at the end of
755-
// the buffer, df_lnum[] is one beyond the end!
756-
// This is using a linked list, because the number of differences is expected
757-
// to be reasonable small. The list is sorted on lnum.
746+
/// Each diffblock defines where a block of lines starts in each of the buffers
747+
/// and how many lines it occupies in that buffer. When the lines are missing
748+
/// in the buffer the df_count[] is zero. This is all counted in
749+
/// buffer lines.
750+
/// There is always at least one unchanged line in between the diffs (unless
751+
/// linematch is used). Otherwise it would have been included in the diff above
752+
/// or below it.
753+
/// df_lnum[] + df_count[] is the lnum below the change. When in one buffer
754+
/// lines have been inserted, in the other buffer df_lnum[] is the line below
755+
/// the insertion and df_count[] is zero. When appending lines at the end of
756+
/// the buffer, df_lnum[] is one beyond the end!
757+
/// This is using a linked list, because the number of differences is expected
758+
/// to be reasonable small. The list is sorted on lnum.
759+
/// Each diffblock also contains a cached list of inline diff of changes within
760+
/// the block, used for highlighting.
758761
typedef struct diffblock_S diff_T;
759762
struct diffblock_S {
760763
diff_T *df_next;
761764
linenr_T df_lnum[DB_COUNT]; // line number in buffer
762765
linenr_T df_count[DB_COUNT]; // nr of inserted/changed lines
763766
bool is_linematched; // has the linematch algorithm ran on this diff hunk to divide it into
764767
// smaller diff hunks?
768+
769+
bool has_changes; ///< has cached list of inline changes
770+
garray_T df_changes; ///< list of inline changes (diffline_change_T)
771+
};
772+
773+
/// Each entry stores a single inline change within a diff block. Line numbers
774+
/// are recorded as relative offsets, and columns are byte offsets, not
775+
/// character counts.
776+
/// Ranges are [start,end), with the end being exclusive.
777+
typedef struct diffline_change_S diffline_change_T;
778+
struct diffline_change_S {
779+
colnr_T dc_start[DB_COUNT]; ///< byte offset of start of range in the line
780+
colnr_T dc_end[DB_COUNT]; ///< 1 paste byte offset of end of range in line
781+
int dc_start_lnum_off[DB_COUNT]; ///< starting line offset
782+
int dc_end_lnum_off[DB_COUNT]; ///< end line offset
783+
};
784+
785+
/// Describes a single line's list of inline changes. Use diff_change_parse() to
786+
/// parse this.
787+
typedef struct diffline_S diffline_T;
788+
struct diffline_S {
789+
diffline_change_T *changes;
790+
int num_changes;
791+
int bufidx;
792+
int lineoff;
765793
};
766794

767795
#define SNAP_HELP_IDX 0

src/nvim/change.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ static void changed_common(buf_T *buf, linenr_T lnum, colnr_T col, linenr_T lnum
240240
FOR_ALL_WINDOWS_IN_TAB(win, curtab) {
241241
if (win->w_buffer == buf && win->w_p_diff && diff_internal()) {
242242
curtab->tp_diff_update = true;
243+
diff_update_line(lnum);
243244
}
244245
}
245246

0 commit comments

Comments
 (0)