Skip to content

fix(highlights): preserve character group attributes - #447

Merged
esmuellert merged 3 commits into
esmuellert:mainfrom
richardgill:fix-char-highlight-attributes
Jul 25, 2026
Merged

fix(highlights): preserve character group attributes#447
esmuellert merged 3 commits into
esmuellert:mainfrom
richardgill:fix-char-highlight-attributes

Conversation

@richardgill

@richardgill richardgill commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Summary

Preserve the full highlight definition when char_insert or char_delete references a highlight group, instead of copying only its effective background.

Motivation: I wanted to style char insert text white like github diffs. And this was preventing me from doing that.

Example

vim.api.nvim_set_hl(0, "MyCharInsert", {
  fg = "#ffffff",
  bg = "#005f00",
  bold = true,
  nocombine = true,
})

require("codediff").setup({
  highlights = {
    char_insert = "MyCharInsert",
  },
})

Given this change:

before: local status = "pending"
after:  local status = "complete"

                       ^^^^^^^^  character-level insertion

Before this fix:

CodeDiffCharInsert:
  bg=#005f00

Only the background was copied. The configured foreground, bold, and nocombine attributes were lost.

After this fix:

CodeDiffCharInsert:
  fg=#ffffff bg=#005f00 bold nocombine

Resulting rendering:

after:  local status = "complete"
                       ^^^^^^^^
                       white, bold text on #005f00 from MyCharInsert hl

What changed

  • Preserve foreground, special color, GUI/cterm styles, and nocombine from configured character highlight groups.
  • Materialize reversed GUI and cterm colors before removing the reverse flags.
  • Use fallback colors only when the configured group has no background.
  • Keep line highlights background-only so syntax highlighting remains visible.
  • Keep direct colors and automatically derived character highlights background-only.
  • Re-resolve character group attributes after ColorScheme changes.

Behavior change

Named character highlight groups now define the complete appearance of changed characters. Users who want a background override without replacing syntax foreground or styles can continue to use a direct color or a custom background-only highlight group. Default and automatically derived character highlights are unchanged.

Test coverage

Added coverage for preserved attributes, reverse colors, line and direct-color behavior, derived highlights, and ColorScheme updates.

Fixes #446

@richardgill
richardgill marked this pull request as ready for review July 17, 2026 08:03
@richardgill
richardgill marked this pull request as draft July 17, 2026 10:53
@richardgill
richardgill marked this pull request as ready for review July 18, 2026 10:09
@richardgill richardgill added the Size/M Medium - Write logic, add tests (one evening) label Jul 22, 2026
@richardgill
richardgill requested a review from esmuellert as a code owner July 23, 2026 11:59
@esmuellert
esmuellert merged commit 7b19fa8 into esmuellert:main Jul 25, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Size/M Medium - Write logic, add tests (one evening)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Character highlight groups lose foreground and style attributes

2 participants