Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tui-code-highlight-no-red.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Remove red coloring from syntax highlighting in code previews and markdown code blocks.
4 changes: 3 additions & 1 deletion apps/kimi-code/src/tui/components/media/code-highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { extname } from 'node:path';

import { highlight, supportsLanguage } from 'cli-highlight';

import { codeHighlightTheme } from '#/tui/theme/highlight-theme';

const EXT_LANG_MAP: Record<string, string> = {
ts: 'typescript',
tsx: 'typescript',
Expand Down Expand Up @@ -45,7 +47,7 @@ export function highlightLines(code: string, lang: string | undefined): string[]
const normalizedLang = lang?.trim().toLowerCase();
if (!normalizedLang || !supportsLanguage(normalizedLang)) return code.split('\n');
try {
return highlight(code, { language: normalizedLang, ignoreIllegals: true }).split('\n');
return highlight(code, { language: normalizedLang, ignoreIllegals: true, theme: codeHighlightTheme }).split('\n');
} catch {
return code.split('\n');
}
Expand Down
17 changes: 17 additions & 0 deletions apps/kimi-code/src/tui/theme/highlight-theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Shared cli-highlight theme for code previews (Write/Edit tool calls,
* approval panels) and markdown code blocks.
*
* cli-highlight's DEFAULT_THEME paints `string`, `regexp` and `deletion`
* tokens red; reset exactly those tokens to `plain` so highlighted code
* contains no red at all. Tokens not listed here fall back to DEFAULT_THEME.
*/

import { plain } from 'cli-highlight';
import type { Theme } from 'cli-highlight';

export const codeHighlightTheme: Theme = {
string: plain,
regexp: plain,
deletion: plain,
};
3 changes: 2 additions & 1 deletion apps/kimi-code/src/tui/theme/pi-tui-theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import chalk from 'chalk';
import { highlight, supportsLanguage } from 'cli-highlight';

import { currentTheme } from './theme';
import { codeHighlightTheme } from './highlight-theme';

// pi-tui's renderer emits literal "### " / "#### " / ... markers for h3-h6
// headings (h1/h2 are rendered without the `#` prefix). The prefix arrives
Expand Down Expand Up @@ -51,7 +52,7 @@ export function createMarkdownTheme(options?: { transient?: boolean }): Markdown
const language =
normalizedLang !== undefined && supportsLanguage(normalizedLang) ? normalizedLang : 'text';
try {
const highlighted = highlight(code, { language, ignoreIllegals: true });
const highlighted = highlight(code, { language, ignoreIllegals: true, theme: codeHighlightTheme });
return highlighted.split('\n');
} catch {
return code.split('\n');
Expand Down
34 changes: 34 additions & 0 deletions apps/kimi-code/test/tui/components/media/code-highlight.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { createRequire } from 'node:module';
import { dirname } from 'node:path';

import { describe, expect, it } from 'vitest';

import { highlightLines, langFromPath } from '#/tui/components/media/code-highlight';
import { codeHighlightTheme } from '#/tui/theme/highlight-theme';

import { captureProcessWrite } from '../../../helpers/process';

const ESC = String.fromCodePoint(27);

describe('code-highlight', () => {
it('maps known file extensions to supported highlight languages', () => {
expect(langFromPath('src/foo.ts')).toBe('typescript');
Expand All @@ -23,4 +29,32 @@ describe('code-highlight', () => {
stderr.restore();
}
});

it('resets red tokens to plain styling', () => {
for (const token of ['string', 'regexp', 'deletion'] as const) {
expect(codeHighlightTheme[token]?.('code')).toBe('code');
}
});

it('emits no red SGR for strings, regexps and diff deletions', () => {
// cli-highlight styles through its own chalk v4 instance; force colors on
// so the assertions below observe real SGR sequences.
const req = createRequire(import.meta.url);
const chalkV4 = req(
req.resolve('chalk', { paths: [dirname(req.resolve('cli-highlight'))] }),
) as { level: number };
const prevLevel = chalkV4.level;
chalkV4.level = 1;
try {
const js = highlightLines("const s = 'str';\nconst r = /re+/g;", 'javascript').join('\n');
expect(js).not.toContain(`${ESC}[31m`);
expect(js).toContain(`${ESC}[34m`); // keywords stay highlighted

const diff = highlightLines('+ added\n- removed', 'diff').join('\n');
expect(diff).not.toContain(`${ESC}[31m`);
expect(diff).toContain(`${ESC}[32m`); // additions stay green
} finally {
chalkV4.level = prevLevel;
}
});
});
Loading