Skip to content

Commit 9943d47

Browse files
ychinchrisbra
authored andcommitted
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 #16768. closes: #16881 Signed-off-by: Yee Cheng Chin <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 06774a2 commit 9943d47

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1855
-113
lines changed

runtime/doc/diff.txt

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*diff.txt* For Vim version 9.1. Last change: 2024 Feb 01
1+
*diff.txt* For Vim version 9.1. Last change: 2024 Mar 26
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -226,14 +226,29 @@ The diffs are highlighted with these groups:
226226
|hl-DiffAdd| DiffAdd Added (inserted) lines. These lines exist in
227227
this buffer but not in another.
228228
|hl-DiffChange| DiffChange Changed lines.
229-
|hl-DiffText| DiffText Changed text inside a Changed line. Vim
230-
finds the first character that is different,
231-
and the last character that is different
232-
(searching from the end of the line). The
233-
text in between is highlighted. This means
234-
that parts in the middle that are still the
235-
same are highlighted anyway. The 'diffopt'
236-
flags "iwhite" and "icase" are used here.
229+
|hl-DiffText| DiffText Changed text inside a Changed line. Exact
230+
behavior depends on the `inline:` setting in
231+
'diffopt'.
232+
With `inline:` set to "simple", Vim finds the
233+
first character that is different, and the
234+
last character that is different (searching
235+
from the end of the line). The text in
236+
between is highlighted. This means that parts
237+
in the middle that are still the same are
238+
highlighted anyway. The 'diffopt' flags
239+
"iwhite" and "icase" are used here.
240+
With `inline:` set to "char" or "word", Vim
241+
uses the internal diff library to perform a
242+
detailed diff between the changed blocks and
243+
highlight the exact difference between the
244+
two. Will respect any 'diffopt' flag that
245+
affects internal diff.
246+
Not used when `inline:` set to "none".
247+
|hl-DiffTextAdd| DiffTextAdd Added text inside a Changed line. Similar to
248+
DiffText, but used when there is no
249+
corresponding text in other buffers. Will not
250+
be used when `inline:` is set to "simple" or
251+
"none".
237252
|hl-DiffDelete| DiffDelete Deleted lines. Also called filler lines,
238253
because they don't really exist in this
239254
buffer.

runtime/doc/options.txt

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*options.txt* For Vim version 9.1. Last change: 2025 Mar 14
1+
*options.txt* For Vim version 9.1. Last change: 2025 Mar 26
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -2910,7 +2910,8 @@ A jump table for the options with a short description can be found at |Q_op|.
29102910
security reasons.
29112911

29122912
*'dip'* *'diffopt'*
2913-
'diffopt' 'dip' string (default "internal,filler,closeoff")
2913+
'diffopt' 'dip' string (default
2914+
"internal,filler,closeoff,inline:simple")
29142915
global
29152916
{not available when compiled without the |+diff|
29162917
feature}
@@ -2975,6 +2976,21 @@ A jump table for the options with a short description can be found at |Q_op|.
29752976
Use the indent heuristic for the internal
29762977
diff library.
29772978

2979+
inline:{text} Highlight inline differences within a change.
2980+
See |view-diffs|. Supported values are:
2981+
2982+
none Do not perform inline highlighting.
2983+
simple Highlight from first different
2984+
character to the last one in each
2985+
line. This is the default if nothing
2986+
is set.
2987+
char Use internal diff to perform a
2988+
character-wise diff and highlight the
2989+
difference.
2990+
word Use internal diff to perform a
2991+
|word|-wise diff and highlight the
2992+
difference.
2993+
29782994
internal Use the internal diff library. This is
29792995
ignored when 'diffexpr' is set. *E960*
29802996
When running out of memory when writing a
@@ -4392,10 +4408,10 @@ A jump table for the options with a short description can be found at |Q_op|.
43924408
v:Visual,V:VisualNOS,w:WarningMsg,
43934409
W:WildMenu,f:Folded,F:FoldColumn,
43944410
A:DiffAdd,C:DiffChange,D:DiffDelete,
4395-
T:DiffText,>:SignColumn,-:Conceal,
4396-
B:SpellBad,P:SpellCap,R:SpellRare,
4397-
L:SpellLocal,+:Pmenu,=:PmenuSel,
4398-
k:PmenuMatch,<:PmenuMatchSel,
4411+
T:DiffText,E:DiffTextAdd,>:SignColumn,
4412+
-:Conceal,B:SpellBad,P:SpellCap,
4413+
R:SpellRare, L:SpellLocal,+:Pmenu,
4414+
=:PmenuSel, k:PmenuMatch,<:PmenuMatchSel,
43994415
[:PmenuKind,]:PmenuKindSel,
44004416
{:PmenuExtra,}:PmenuExtraSel,
44014417
x:PmenuSbar,X:PmenuThumb,*:TabLine,
@@ -4447,7 +4463,8 @@ A jump table for the options with a short description can be found at |Q_op|.
44474463
|hl-DiffAdd| A added line in diff mode
44484464
|hl-DiffChange| C changed line in diff mode
44494465
|hl-DiffDelete| D deleted line in diff mode
4450-
|hl-DiffText| T inserted text in diff mode
4466+
|hl-DiffText| T changed text in diff mode
4467+
|hl-DiffTextAdd| E inserted text in diff mode
44514468
|hl-SignColumn| > column used for |signs|
44524469
|hl-Conceal| - the placeholders used for concealed characters
44534470
(see 'conceallevel')

runtime/doc/syntax.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*syntax.txt* For Vim version 9.1. Last change: 2025 Mar 21
1+
*syntax.txt* For Vim version 9.1. Last change: 2025 Mar 26
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -5832,6 +5832,9 @@ DiffChange Diff mode: Changed line. |diff.txt|
58325832
DiffDelete Diff mode: Deleted line. |diff.txt|
58335833
*hl-DiffText*
58345834
DiffText Diff mode: Changed text within a changed line. |diff.txt|
5835+
*hl-DiffTextAdd*
5836+
DiffTextAdd Diff mode: Added text within a changed line. Linked to
5837+
|hl-DiffText| by default. |diff.txt|
58355838
*hl-EndOfBuffer*
58365839
EndOfBuffer Filler lines (~) after the last line in the buffer.
58375840
By default, this is highlighted like |hl-NonText|.

runtime/doc/tags

+1
Original file line numberDiff line numberDiff line change
@@ -8205,6 +8205,7 @@ hl-DiffAdd syntax.txt /*hl-DiffAdd*
82058205
hl-DiffChange syntax.txt /*hl-DiffChange*
82068206
hl-DiffDelete syntax.txt /*hl-DiffDelete*
82078207
hl-DiffText syntax.txt /*hl-DiffText*
8208+
hl-DiffTextAdd syntax.txt /*hl-DiffTextAdd*
82088209
hl-Directory syntax.txt /*hl-Directory*
82098210
hl-EndOfBuffer syntax.txt /*hl-EndOfBuffer*
82108211
hl-ErrorMsg syntax.txt /*hl-ErrorMsg*

runtime/doc/version9.txt

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*version9.txt* For Vim version 9.1. Last change: 2025 Mar 23
1+
*version9.txt* For Vim version 9.1. Last change: 2025 Mar 26
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -41553,6 +41553,16 @@ Enum support for Vim9 script |:enum|
4155341553

4155441554
Support for protected _new() method
4155541555

41556+
Diff mode ~
41557+
---------
41558+
Include the "linematch" algorithm for the 'diffopt' setting. This aligns
41559+
changes between buffers on similar lines improving the diff highlighting in
41560+
Vim
41561+
41562+
Improve the diff highlighting for changes within a line. Configurable using
41563+
the "inline" sub option value for the 'diffopt' setting, with "inline:simple"
41564+
being added to the default "diffopt" value (but this does not change how diff
41565+
mode works).
4155641566
*new-other-9.2*
4155741567
Other new features ~
4155841568
------------------
@@ -41570,10 +41580,6 @@ Support highlighting the matched text and the completion kind for insert-mode
4157041580
completion and command-line completion in |ins-completion-menu|, see
4157141581
|complete-items|
4157241582

41573-
Include the "linematch" algorithm for the 'diffopt' setting. This aligns
41574-
changes between buffers on similar lines improving the diff highlighting in
41575-
Vim
41576-
4157741583
Support for the |Tuple| data type in Vim script and Vim9 script.
4157841584

4157941585
*changed-9.2*
@@ -41590,7 +41596,6 @@ Default values: ~
4159041596
- the default value of the 'keyprotocol' option has been updated and support
4159141597
for the ghostty terminal emulator (using kitty protocol) has been added
4159241598

41593-
4159441599
Completion: ~
4159541600
- allow to complete directories from 'cdpath' for |:cd| and similar commands,
4159641601
add the "cd_in_path" completion type for e.g. |:command-complete| and
@@ -41702,7 +41707,8 @@ Autocommands: ~
4170241707

4170341708
Highlighting: ~
4170441709

41705-
|hl-ComplMatchIns| matched text of the currently inserted completion.
41710+
|hl-ComplMatchIns| matched text of the currently inserted completion
41711+
|hl-DiffTextAdd| added text within a changed line
4170641712
|hl-MsgArea| highlighting of the Command-line and messages area
4170741713
|hl-PmenuMatch| Popup menu: highlighting of matched text
4170841714
|hl-PmenuMatchSel| Popup menu: highlighting of matched text in selected

runtime/syntax/vim.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ syn keyword vimGroup contained Comment Constant String Character Number Boolean
123123

124124
" Default highlighting groups {{{2
125125
" GEN_SYN_VIM: vimHLGroup, START_STR='syn keyword vimHLGroup contained', END_STR=''
126-
syn keyword vimHLGroup contained ErrorMsg IncSearch ModeMsg NonText StatusLine StatusLineNC EndOfBuffer VertSplit VisualNOS DiffText PmenuSbar TabLineSel TabLineFill Cursor lCursor QuickFixLine CursorLineSign CursorLineFold CurSearch PmenuKind PmenuKindSel PmenuMatch PmenuMatchSel PmenuExtra PmenuExtraSel PopupSelected MessageWindow PopupNotification 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 ToolbarLine ToolbarButton Menu Tooltip Scrollbar CursorIM LineNrAbove LineNrBelow
126+
syn keyword vimHLGroup contained ErrorMsg IncSearch ModeMsg NonText StatusLine StatusLineNC EndOfBuffer VertSplit VisualNOS DiffText DiffTextAdd PmenuSbar TabLineSel TabLineFill Cursor lCursor QuickFixLine CursorLineSign CursorLineFold CurSearch PmenuKind PmenuKindSel PmenuMatch PmenuMatchSel PmenuExtra PmenuExtraSel PopupSelected MessageWindow PopupNotification 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 ToolbarLine ToolbarButton Menu Tooltip Scrollbar CursorIM LineNrAbove LineNrBelow
127127
syn match vimHLGroup contained "\<Conceal\>"
128128
syn case match
129129

src/change.c

+3
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,10 @@ changed_common(
476476
#endif
477477
#ifdef FEAT_DIFF
478478
if (curwin->w_p_diff && diff_internal())
479+
{
479480
curtab->tp_diff_update = TRUE;
481+
diff_update_line(lnum);
482+
}
480483
#endif
481484

482485
// set the '. mark

0 commit comments

Comments
 (0)