Files
neovim/src/nvim/highlight_defs.h
zeertzjq 2331c52aff 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

9943d4790e

Co-authored-by: Yee Cheng Chin <ychin.git@gmail.com>
2025-03-28 14:45:01 +08:00

172 lines
5.6 KiB
C

#pragma once
#include <stdbool.h>
#include <stdint.h>
#include "nvim/api/private/defs.h"
typedef int32_t RgbValue;
/// Highlighting attribute bits.
///
/// sign bit should not be used here, as it identifies invalid highlight
typedef enum {
HL_INVERSE = 0x01,
HL_BOLD = 0x02,
HL_ITALIC = 0x04,
// The next three bits are all underline styles
HL_UNDERLINE_MASK = 0x38,
HL_UNDERLINE = 0x08,
HL_UNDERCURL = 0x10,
HL_UNDERDOUBLE = 0x18,
HL_UNDERDOTTED = 0x20,
HL_UNDERDASHED = 0x28,
// 0x30 and 0x38 spare for underline styles
HL_STANDOUT = 0x0040,
HL_STRIKETHROUGH = 0x0080,
HL_ALTFONT = 0x0100,
// 0x0200 spare
HL_NOCOMBINE = 0x0400,
HL_BG_INDEXED = 0x0800,
HL_FG_INDEXED = 0x1000,
HL_DEFAULT = 0x2000,
HL_GLOBAL = 0x4000,
} HlAttrFlags;
/// Stores a complete highlighting entry, including colors and attributes
/// for both TUI and GUI.
typedef struct {
int16_t rgb_ae_attr, cterm_ae_attr; ///< HlAttrFlags
RgbValue rgb_fg_color, rgb_bg_color, rgb_sp_color;
int16_t cterm_fg_color, cterm_bg_color;
int32_t hl_blend;
int32_t url;
} HlAttrs;
#define HLATTRS_INIT (HlAttrs) { \
.rgb_ae_attr = 0, \
.cterm_ae_attr = 0, \
.rgb_fg_color = -1, \
.rgb_bg_color = -1, \
.rgb_sp_color = -1, \
.cterm_fg_color = 0, \
.cterm_bg_color = 0, \
.hl_blend = -1, \
.url = -1, \
}
/// Values for index in highlight_attr[].
/// When making changes, also update hlf_names in highlight.h!
typedef enum {
HLF_NONE = 0, ///< no UI highlight active
HLF_8, ///< Meta & special keys listed with ":map", text that is
///< displayed different from what it is
HLF_EOB, ///< after the last line in the buffer
HLF_TERM, ///< terminal cursor focused
HLF_AT, ///< @ characters at end of screen, characters that don't really exist in the text
HLF_D, ///< directories in CTRL-D listing
HLF_E, ///< error messages
HLF_I, ///< incremental search
HLF_L, ///< last search string
HLF_LC, ///< current search match
HLF_M, ///< "--More--" message
HLF_CM, ///< Mode (e.g., "-- INSERT --")
HLF_N, ///< line number for ":number" and ":#" commands
HLF_LNA, ///< LineNrAbove
HLF_LNB, ///< LineNrBelow
HLF_CLN, ///< current line number when 'cursorline' is set
HLF_CLS, ///< current line sign column
HLF_CLF, ///< current line fold
HLF_R, ///< return to continue message and yes/no questions
HLF_S, ///< status lines
HLF_SNC, ///< status lines of not-current windows
HLF_C, ///< window split separators
HLF_VSP, ///< VertSplit
HLF_T, ///< Titles for output from ":set all", ":autocmd" etc.
HLF_V, ///< Visual mode
HLF_VNC, ///< Visual mode, autoselecting and not clipboard owner
HLF_W, ///< warning messages
HLF_WM, ///< Wildmenu highlight
HLF_FL, ///< Folded line
HLF_FC, ///< Fold column
HLF_ADD, ///< Added diff line
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
HLF_SPC, ///< SpellCap
HLF_SPR, ///< SpellRare
HLF_SPL, ///< SpellLocal
HLF_PNI, ///< popup menu normal item
HLF_PSI, ///< popup menu selected item
HLF_PMNI, ///< popup menu matched text in normal item
HLF_PMSI, ///< popup menu matched text in selected item
HLF_PNK, ///< popup menu normal item "kind"
HLF_PSK, ///< popup menu selected item "kind"
HLF_PNX, ///< popup menu normal item "menu" (extra text)
HLF_PSX, ///< popup menu selected item "menu" (extra text)
HLF_PSB, ///< popup menu scrollbar
HLF_PST, ///< popup menu scrollbar thumb
HLF_TP, ///< tabpage line
HLF_TPS, ///< tabpage line selected
HLF_TPF, ///< tabpage line filler
HLF_CUC, ///< 'cursorcolumn'
HLF_CUL, ///< 'cursorline'
HLF_MC, ///< 'colorcolumn'
HLF_QFL, ///< selected quickfix line
HLF_0, ///< Whitespace
HLF_INACTIVE, ///< NormalNC: Normal text in non-current windows
HLF_MSGSEP, ///< message separator line
HLF_NFLOAT, ///< Floating window
HLF_MSG, ///< Message area
HLF_BORDER, ///< Floating window border
HLF_WBR, ///< Window bars
HLF_WBRNC, ///< Window bars of not-current windows
HLF_CU, ///< Cursor
HLF_BTITLE, ///< Float Border Title
HLF_BFOOTER, ///< Float Border Footer
HLF_TS, ///< status line for terminal window
HLF_TSNC, ///< status line for non-current terminal window
HLF_COUNT, ///< MUST be the last one
} hlf_T;
typedef enum {
kHlUnknown,
kHlUI,
kHlSyntax,
kHlTerminal,
kHlCombine,
kHlBlend,
kHlBlendThrough,
kHlInvalid,
} HlKind;
typedef struct {
HlAttrs attr;
HlKind kind;
int id1;
int id2;
int winid;
} HlEntry;
typedef struct {
int ns_id;
int syn_id;
} ColorKey;
#define ColorKey(n, s) (ColorKey) { .ns_id = (int)(n), .syn_id = (s) }
typedef struct {
int attr_id;
int link_id;
int version;
bool is_default;
bool link_global;
} ColorItem;
#define COLOR_ITEM_INITIALIZER { .attr_id = -1, .link_id = -1, .version = -1, \
.is_default = false, .link_global = false }
enum { HLATTRS_DICT_SIZE = 16, };