From f3c5514c19bff2a6aebe605e17a67e2c70514496 Mon Sep 17 00:00:00 2001 From: Max Syabro Date: Thu, 18 Jun 2026 14:29:49 +0700 Subject: [PATCH] FIX: make word emphasis readable on light themes Use lighter word-emphasis backgrounds when the configured Shiki theme is light, keeping the existing dark-theme colors unchanged. Add a regression test for the light-theme highlight colors. --- src/diff/index.spec.ts | 9 +++++++++ src/diff/word/line-emphasis.ts | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/src/diff/index.spec.ts b/src/diff/index.spec.ts index 9c342a4..9a831c9 100644 --- a/src/diff/index.spec.ts +++ b/src/diff/index.spec.ts @@ -131,6 +131,15 @@ test("word emphasis pairs the most similar lines inside change blocks", () => { assert.match(rendered[0] ?? "", /\x1b\[48;2;148;62;70m\x1b\[1mline/); }); +test("word emphasis uses readable backgrounds with light syntax themes", () => { + setCodePreviewSettings({ ...codePreviewSettings, shikiTheme: "github-light" }); + const diff = "-1 const value = oldValue;\n+1 const value = newValue;"; + const rendered = renderSyntaxHighlightedDiff(diff, undefined, testTheme(), 2); + assert.match(rendered, /\x1b\[48;2;216;182;182m\x1b\[1mold/); + assert.match(rendered, /\x1b\[48;2;194;209;194m\x1b\[1mnew/); + assert.doesNotMatch(rendered, /\x1b\[48;2;(?:148;62;70|64;132;82)m/); +}); + test("word emphasis marks low-overlap one-to-one changed pairs instead of skipping them", () => { const diff = "-1 out.push(pair.removed, pair.added);\n+1 block.push(next);"; const rendered = renderSyntaxHighlightedDiff(diff, undefined, testTheme(), 2).split("\n"); diff --git a/src/diff/word/line-emphasis.ts b/src/diff/word/line-emphasis.ts index 8c6c31e..83529b5 100644 --- a/src/diff/word/line-emphasis.ts +++ b/src/diff/word/line-emphasis.ts @@ -1,9 +1,13 @@ +import { bundledThemesInfo } from "shiki"; +import { codePreviewSettings } from "../../settings/index"; import type { DiffWordEmphasis } from "../../settings/types"; import { injectVisibleRanges } from "../../shared/terminal-text"; import type { ParsedDiffLine } from "../parse"; import { analyzeChangedLineBlock } from "./change-block"; import { shouldEmphasizeChangedPair } from "./emphasis"; +const shikiThemeTypes = new Map(bundledThemesInfo.map((theme) => [theme.id, theme.type])); + export function changedLineEmphasis( block: ParsedDiffLine[], wordEmphasis: DiffWordEmphasis, @@ -50,5 +54,9 @@ function findCodeStart(line: string): number { } function wordEmphasis(kind: "add" | "remove"): string { + if (shikiThemeTypes.get(codePreviewSettings.shikiTheme) === "light") { + const bg = kind === "add" ? "\x1b[48;2;194;209;194m" : "\x1b[48;2;216;182;182m"; + return `${bg}\x1b[1m`; + } return kind === "add" ? "\x1b[48;2;64;132;82m\x1b[1m" : "\x1b[48;2;148;62;70m\x1b[1m"; }