Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/diff/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
8 changes: 8 additions & 0 deletions src/diff/word/line-emphasis.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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";
}