diff --git a/packages/client/ui-primitives/README.i18n.yaml b/packages/client/ui-primitives/README.i18n.yaml index 0e8af7a613..57651951e1 100644 --- a/packages/client/ui-primitives/README.i18n.yaml +++ b/packages/client/ui-primitives/README.i18n.yaml @@ -18,8 +18,8 @@ en: ead6be55c7c9d7cf zh: 125ad36db7328551 /deepseek-ai-dsh-client-ui-primitives/use-this-package/component-catalog: - en: 77383e8771784a60 - zh: 25c4c59b1cbc7f8b + en: ae85dcbb25303332 + zh: 471224ebad67df91 /deepseek-ai-dsh-client-ui-primitives/use-this-package/controls-and-icons: en: 2fc65b908307b52e zh: e5c1181ecd2da6e5 @@ -54,8 +54,8 @@ en: ca75c51c89c2c9b0 zh: de54f3467ef3b9be /deepseek-ai-dsh-client-ui-primitives/known-limitations-and-deferred-work: - en: 4b0759677946a32a - zh: 0e562bb5d43670e8 + en: 96ab15bc5b281c46 + zh: b1e073e60e348b0e /deepseek-ai-dsh-client-ui-primitives/known-limitations-and-deferred-work/dev-note: en: e67f633dc6fe7773 zh: 3bb0c20c93f16f98 diff --git a/packages/client/ui-primitives/README.md b/packages/client/ui-primitives/README.md index 046b5d9aee..aa4fa2d978 100644 --- a/packages/client/ui-primitives/README.md +++ b/packages/client/ui-primitives/README.md @@ -72,7 +72,7 @@ Check this table before writing a control in a feature package. A plugin cannot | `SettingsForm`, `SettingsValueField`, `SettingsSecretField` | The frame and the controls of a plugin's settings page: the frame takes its copy as `labels`, saves only on its button, and discards on unmount; a value field shows staged text with the overridden badge and reset; a secret field starts blank, requests no saved-password autofill, and reports only whether a value is configured. | | `SettingsFormModel`, `settingsNumberField`, `settingsTextField` | The staged-edit model behind such a page over a settings scope: drafts are staged and written on save, a field is overridden by its presence in the user layer, and a save that did not land keeps its drafts. | | `JsonTree`, `JsonBlock` | Read-only JSON inspection. | -| `MarkdownText`, `MarkdownDelegateProvider`, `CodeBlock` | Untrusted GFM with TeX math, owner-delegated HTTP(S) navigation, and highlighted code. `CodeBlock` accepts opt-in `lineNumbers`; copied source excludes the gutter, and `contentRef` exposes its stable source wrapper to an owner that uses it as a scrollport. Set `showHeader={false}` when the owner supplies its own language and copy toolbar. | +| `MarkdownText`, `MarkdownDelegateProvider`, `CodeBlock` | Untrusted GFM with TeX math, owner-delegated HTTP(S) navigation, and highlighted code. `CodeBlock` accepts opt-in `lineNumbers`; copied source excludes the gutter, and `contentRef` exposes its stable source wrapper to an owner that uses it as a scrollport. Set `showHeader={false}` when the owner supplies its own language and copy toolbar. `diff` and `patch` fences classify rows by their marker column, color added and removed rows like the Sidebar file diff, and tokenize each file's removed and added sides with the language its header names, so source diffs keep syntax colors; the marker column stays in the copied source. | | `DetailDocument`, `detailDocumentState`, `useDetailDocumentLabels` | Section without a heading of its own for a long Markdown description, such as a plugin's detail document: three skeleton lines while loading, the document, the owner's failure message with a retry, or nothing when there is no document. A document taller than `min(560px, 60vh)` scrolls inside a keyboard-focusable region that the owner's title names. Raw HTML in the document is omitted. Relative links stay text and only images the owner's `images` resolver accepts, or absolute HTTP(S) images, load. `detailDocumentState` maps an owner's read to the shown state, and `useDetailDocumentLabels` builds reference-stable labels from translated strings. | | `useClipboardCopy` | Copies the current text with one-second success feedback, prevents overlapping or success-window repeats, and cancels its reset timer on unmount; late write completion cannot update an unmounted control. | | `UserMessageBubble` | User-message background, radius, content inset, typography, and plain-text wrapping. Accepts native div props; callers own row alignment, width, content, and actions. | @@ -235,6 +235,7 @@ These limits define how the atoms behave at the edges; they are current package - **Streaming defers cross-boundary reference resolution** — a reference-style link or footnote whose definition sits on the other side of the incremental freeze boundary renders as literal text while the reply streams; the settled full parse at finalize resolves it. - **A long highlighted fence retains its complete token DOM** — streaming avoids re-parsing, re-tokenizing, and reconciling the completed prefix, but it does not discard old colors or virtualize token spans. Final DOM cardinality therefore still follows the fence's token count; nested/container fences and a pathological single long line remain on the general tail path. - **Math copies as TeX only within one Markdown document** — a selection that reaches outside its `MarkdownText` root, such as from a user message into a reply, keeps the browser's native copy, which joins KaTeX's hidden MathML text with the visible glyphs. Inside one document, the plain text of non-math content comes from `innerText`, so code-block language labels and copy-button text are included, as in native copy. +- **Diff fences infer colors per file and per side** — `diff` and `patch` fences parse hunk line budgets so later file headers stay chrome, tokenize each file's removed and added lines separately with the language its `---`/`+++` header names, and leave the body plain when that path is unknown. A streaming diff re-tokenizes on every chunk; the marker column stays in copied source. - **Overlong lines stay uncolored** — a source line of 1,000 or more UTF-16 units renders as one plain run in every highlighted surface, because TextMate scanning cost grows superlinearly with line length and a single generated line can otherwise block the page for seconds. The grammar state skips that line, so a comment or template literal that opens or closes on it mis-colors the following lines. Highlighting still runs synchronously during render, so a file with thousands of ordinary lines costs proportionally more. - **Bare URLs end before non-ASCII punctuation** — the [tokenizer patch](../../../patches/micromark-extension-gfm-autolink-literal@2.1.0.patch) and the [transform patch](../../../patches/mdast-util-gfm-autolink-literal@2.0.1.patch) end a GFM autolink literal at non-ASCII punctuation, symbols, or whitespace, so a URL with an unencoded character such as `(` or `・` in its path links only up to it; write such a URL as `<…>` or `[…](…)`. A URL followed directly by CJK letters still links them, because paths such as `/wiki/中文` are valid. Both patches are keyed to exact versions: an upgrade of either package must re-apply or retire its patch, otherwise the boundary rule silently disappears. - **The Go struct-field rule is patched locally** — when the Go grammar loads, its repeated one-line struct-field rule is replaced with the rule [worlpaker/go-syntax#25](https://github.com/worlpaker/go-syntax/pull/25) merged, because the vendored rule backtracks exponentially on lines such as a brace-bearing comment after `struct {`. If a dependency upgrade removes that rule, loading the grammar throws and Go code renders as plain text; delete the patch once `@shikijs/langs` ships the upstream rule. diff --git a/packages/client/ui-primitives/README.zh.md b/packages/client/ui-primitives/README.zh.md index bd860cd8c1..1eeebf6dac 100644 --- a/packages/client/ui-primitives/README.zh.md +++ b/packages/client/ui-primitives/README.zh.md @@ -72,7 +72,7 @@ kind: "package-library" | `SettingsForm`、`SettingsValueField`、`SettingsSecretField` | 插件设置页的框架与控件:框架以 `labels` 接收文案,只在按钮点击时保存,卸载即丢弃;值字段显示暂存文本以及已覆盖标签和重置;密文字段每次为空,请求浏览器不要自动填入已保存的密码,只报告是否已配置。 | | `SettingsFormModel`、`settingsNumberField`、`settingsTextField` | 这类页面背后基于设置 scope 的暂存编辑模型:草稿先暂存、保存时写入,字段是否被覆盖看用户层是否含有它,未落地的保存保留草稿。 | | `JsonTree`、`JsonBlock` | 只读 JSON 查看。 | -| `MarkdownText`、`MarkdownDelegateProvider`、`CodeBlock` | 不可信 GFM 与 TeX 数学、owner 委托的 HTTP(S) 导航,以及高亮代码。`CodeBlock` 可通过 `lineNumbers` 开启行号;复制的源码不含行号栏,`contentRef` 则向需要把稳定源码包装节点用作滚动区的 owner 提供该节点。调用方提供自己的语言与复制工具栏时,设置 `showHeader={false}`。 | +| `MarkdownText`、`MarkdownDelegateProvider`、`CodeBlock` | 不可信 GFM 与 TeX 数学、owner 委托的 HTTP(S) 导航,以及高亮代码。`CodeBlock` 可通过 `lineNumbers` 开启行号;复制的源码不含行号栏,`contentRef` 则向需要把稳定源码包装节点用作滚动区的 owner 提供该节点。调用方提供自己的语言与复制工具栏时,设置 `showHeader={false}`。`diff` 与 `patch` 围栏按标记列划分行,按侧边栏文件 diff 的样式给新增、删除行上色,并按文件头写明的语言分别对该文件的删除侧与新增侧做语法高亮,因此源码 diff 保留语法颜色;标记列仍保留在复制的源码里。 | | `DetailDocument`、`detailDocumentState`、`useDetailDocumentLabels` | 用于较长 Markdown 描述(例如插件的详细介绍文档)的区块,本身不带标题:加载期间显示三行骨架屏,随后显示文档、owner 提供的失败消息和重试,或在没有文档时不显示任何内容。文档高度超过 `min(560px, 60vh)` 时,在可通过键盘聚焦、由 owner 的标题命名的区域内滚动。文档中的原始 HTML 会被省略。相对链接保持为文本,只加载 owner 的 `images` 解析器接受的图片或绝对 HTTP(S) 地址的图片。`detailDocumentState` 把 owner 的读取结果映射为显示状态,`useDetailDocumentLabels` 根据翻译后的字符串构建引用稳定的标签。 | | `useClipboardCopy` | 复制当前文本并显示一秒成功反馈,阻止写入期间及成功反馈期间的重复操作,卸载时清除重置计时器;迟到的写入结果不会更新已卸载控件。 | | `UserMessageBubble` | 用户消息的背景、圆角、内容内边距、字体与纯文本换行规则。接收原生 div props;行对齐、宽度、内容和操作由调用方拥有。 | @@ -235,6 +235,7 @@ kind: "package-library" - **流式期间跨边界引用解析被推迟**:定义落在增量冻结边界另一侧的引用式链接或脚注,在回复流式输出期间渲染为字面文本;定稿时的全量解析会将其解析。 - **长高亮 fence 会保留完整 token DOM**:流式路径避免重新解析、重新 tokenize 和 reconcile 已完成前缀,但不会丢弃旧颜色或虚拟化 token span。因此最终 DOM 数量仍随 fence 的 token 数增长;嵌套/容器内 fence 与病态的单个超长行仍走通用尾部路径。 - **公式仅在单个 Markdown 文档内复制为 TeX**:超出其 `MarkdownText` 根节点的选区(例如从用户消息延伸到回复)沿用浏览器原生复制,会将 KaTeX 隐藏的 MathML 文本与可见字形拼接在一起。在单个文档内,非公式内容的纯文本取自 `innerText`,因此与原生复制一样,会包含代码块语言标签和复制按钮文本。 +- **Diff 围栏按文件、按侧推断颜色**:`diff` 与 `patch` 围栏解析 hunk 行数预算,使后续文件头仍按文件头处理;每个文件的删除行与新增行分别用其 `---`/`+++` 头写明的语言 tokenize,路径不可识别时正文保持纯文本。流式 diff 每来一个 chunk 都会重新 tokenize;标记列仍保留在复制的源码中。 - **超长行不着色**:所有高亮界面中,长度达到 1,000 个 UTF-16 单元的源码行渲染为一段纯文本,因为 TextMate 扫描成本随行长超线性增长,单个生成行否则会阻塞页面数秒。语法状态会跳过该行,因此在该行开始或结束的注释、模板字符串会使后续行着色错误。高亮仍在渲染期间同步执行,因此数千个普通行的文件仍按比例增加开销。 - **裸 URL 在非 ASCII 标点前结束**:[tokenizer 补丁](../../../patches/micromark-extension-gfm-autolink-literal@2.1.0.patch)和[转换补丁](../../../patches/mdast-util-gfm-autolink-literal@2.0.1.patch)让 GFM 自动链接在非 ASCII 标点、符号或空白处结束,因此路径中含未编码 `(`、`・` 等字符的 URL 只链接到该字符之前;这类 URL 请写成 `<…>` 或 `[…](…)`。URL 后直接跟中日韩文字时仍会并入链接,因为 `/wiki/中文` 这类路径是合法的。两个补丁都绑定精确版本:升级任一依赖时必须重新应用或撤销对应补丁,否则该边界规则会悄然失效。 - **Go 结构体字段规则在本地打了补丁**:Go 语法加载时,重复匹配单行结构体字段的规则会替换为 [worlpaker/go-syntax#25](https://github.com/worlpaker/go-syntax/pull/25) 合入的规则,因为内置规则在 `struct {` 之后含花括号注释等行上会指数级回溯。若依赖升级后该规则消失,加载语法会抛错,Go 代码以纯文本显示;`@shikijs/langs` 发布上游规则后删除此补丁。 diff --git a/packages/client/ui-primitives/src/markdown/CodeBlock.module.css b/packages/client/ui-primitives/src/markdown/CodeBlock.module.css index b59bef283d..387186109c 100644 --- a/packages/client/ui-primitives/src/markdown/CodeBlock.module.css +++ b/packages/client/ui-primitives/src/markdown/CodeBlock.module.css @@ -143,3 +143,67 @@ content: counter(source-line); user-select: none; } + +/* Diff fences paint full-bleed rows with the Sidebar file diff's row colors; + the row's own inline padding keeps the marker clear of the inset bar. */ +.diffFence :where(pre) { + padding: 8px 0 16px; +} + +.diffLine { + display: inline-block; + width: 100%; +} + +/* Numbered gutters already make `.line` a full-width block; keep the padding + box intact instead of stacking a second 100% width on it. */ +.numbered .diffLine { + display: block; + width: auto; +} + +.diffFence .diffLine { + padding: 0 16px; + box-sizing: border-box; +} + +/* A non-wrapping fence scrolls horizontally: rows cover the scrollable width, + not just the visible box, so the fill and left bar survive scrolling. */ +.diffFence[data-code-wrap='false'] .diffLine { + width: max-content; + min-width: 100%; +} + +.diffAdd { + background: var(--dsw-alias-file-diff-added-bg); + box-shadow: inset 3px 0 0 var(--dsw-alias-file-diff-added-marker); +} + +.diffDel { + background: var(--dsw-alias-file-diff-deleted-bg); + box-shadow: inset 3px 0 0 var(--dsw-alias-file-diff-deleted-marker); +} + +.diffMarker { + margin-inline-end: 1ch; +} + +.diffAdd .diffMarker { + color: var(--dsw-alias-file-diff-added-marker); +} + +.diffDel .diffMarker { + color: var(--dsw-alias-file-diff-deleted-marker); +} + +.diffContext { + color: var(--dsw-alias-label-secondary); +} + +.diffHunk { + color: var(--dsw-alias-label-secondary); +} + +.diffMeta { + color: var(--dsw-alias-label-tertiary); +} diff --git a/packages/client/ui-primitives/src/markdown/CodeBlock.tsx b/packages/client/ui-primitives/src/markdown/CodeBlock.tsx index fa573578e1..040189b6ea 100644 --- a/packages/client/ui-primitives/src/markdown/CodeBlock.tsx +++ b/packages/client/ui-primitives/src/markdown/CodeBlock.tsx @@ -3,6 +3,10 @@ import type { CSSProperties, ReactNode, Ref } from 'react' import clsx from 'clsx' import { writeClipboard } from '../clipboard.ts' import { CodeToolbar, type CodeToolbarLabels } from '../CodeToolbar.tsx' +import { + diffFenceTokens, isDiffContent, isDiffFenceLanguage, parseDiffFence, +} from './diff-fence.ts' +import type { DiffRow } from './diff-fence.ts' import { StreamingHighlightSession, grammarLoadCount, highlightToHtml, subscribeGrammarLoaded, } from './highlight.ts' @@ -13,7 +17,11 @@ import css from './CodeBlock.module.css' export interface CodeBlockProps { /** The source text, rendered verbatim (trailing newline trimmed for display). */ code: string - /** Grammar hint (markdown fence info string or a fixed caller id); unknown = plain. */ + /** + * Grammar hint (markdown fence info string or a fixed caller id); unknown = plain. + * A `diff` or `patch` fence renders row colors and tokenizes each file's body + * with the language its header names. + */ lang?: string | undefined /** * The code is still growing (a streaming markdown fence): highlight through @@ -57,6 +65,35 @@ const SHIKI_PRE_PROPS = { /** Completed-line group size; React reconciles groups while the DOM remains line-for-line identical. */ const STREAMING_LINE_GROUP_SIZE = 32 +/** Line class per row kind; context rows keep the block's default ink. */ +const DIFF_ROW_CLASS: Record = { + add: css.diffAdd, + del: css.diffDel, + hunk: css.diffHunk, + meta: css.diffMeta, + context: css.diffContext, +} + +/** One diff row: optional marker column, then the body with its token colors. */ +function renderDiffRow(row: DiffRow, index: number, tokens: readonly HighlightSpan[] | undefined): ReactNode { + const kindClass = DIFF_ROW_CLASS[row.kind] + return ( + + {index > 0 && '\n'} + + {isDiffContent(row) + ? + {row.marker} + {tokens === undefined + ? row.body + : tokens.map((span, spanIndex) => {span.text})} + + : row.raw} + + + ) +} + function renderLine(line: readonly HighlightSpan[], index: number): ReactNode { return ( @@ -72,13 +109,24 @@ export function CodeBlock({ code, lang, streaming, className, contentRef, lineNumbers = false, showHeader = true, copyLabel, copiedLabel, toolbarLabels, wrap, }: CodeBlockProps) { const trimmed = code.endsWith('\n') ? code.slice(0, -1) : code + const diff = useMemo(() => (isDiffFenceLanguage(lang) ? parseDiffFence(trimmed) : undefined), + [trimmed, lang]) const sourceLines = lineNumbers ? trimmed.split('\n') : undefined const rootRef = useRef(null) - const highlighting = useViewportHighlighting(rootRef, lang) + // Diff rows join the same viewport gate as grammar-highlighted fences, so an + // off-screen fence paints its row colors without tokenizing the body. + const highlighting = useViewportHighlighting(rootRef, diff === undefined ? lang : 'diff') // Re-render when a lazy grammar finishes loading, so a fence that showed plain // text while its language's grammar imported picks up highlighting. The // snapshot value is opaque; only its change across renders drives the memo. const loaded = useSyncExternalStore(subscribeGrammarLoaded, grammarLoadCount, grammarLoadCount) + // Each file's old and new sides are tokenized separately; a growing fence + // re-tokenizes per chunk because that grouping has no incremental path, and + // diffs in a fence are short enough to accept it. + const diffTokens = useMemo( + () => (diff === undefined || !highlighting ? undefined : diffFenceTokens(diff)), + [diff, highlighting, loaded], + ) // Streaming state lives in refs mutated inside the memo (the MarkdownText // streaming-cache pattern): the session's caches carry across chunks only // because the owner keys this instance stably while the fence grows. @@ -95,7 +143,8 @@ export function CodeBlock({ } | null>(null) const settledRef = useRef(false) const streamedBody = useMemo(() => { - if (!highlighting) { + // A diff fence draws its own rows; the session's single-language stream is unused. + if (!highlighting || diff !== undefined) { sessionRef.current = null lineCacheRef.current = null settledRef.current = false @@ -146,12 +195,12 @@ export function CodeBlock({ code: trimmed, lang, generation: frame.generation, frame, groups, pending, nextLine, body, } return body - }, [streaming, highlighting, trimmed, lang, loaded]) + }, [streaming, highlighting, diff, trimmed, lang, loaded]) const html = useMemo( - () => (highlighting && streaming !== true && streamedBody === undefined + () => (diff === undefined && highlighting && streaming !== true && streamedBody === undefined ? highlightToHtml(trimmed, lang) : undefined), - [streaming, highlighting, streamedBody, trimmed, lang, loaded], + [diff, streaming, highlighting, streamedBody, trimmed, lang, loaded], ) const [copied, setCopied] = useState(false) const [localWrapped, setWrapped] = useState(true) @@ -172,20 +221,24 @@ export function CodeBlock({ // shiki's HTML output is a static span tree it generated from `code` (no // user HTML passes through), the sanctioned innerHTML consumption path per // shiki's own docs. - const body = streamedBody !== undefined - ? streamedBody - : html === undefined - ? ( -
{sourceLines === undefined ? trimmed : sourceLines.map((line, index) => (
-          {index > 0 && '\n'}{line}
-        ))}
- ) - : ( -
- ) + const body = diff !== undefined + ? ( +
{diff.rows.map((row, index) => renderDiffRow(row, index, diffTokens?.get(index)))}
+ ) + : streamedBody !== undefined + ? streamedBody + : html === undefined + ? ( +
{sourceLines === undefined ? trimmed : sourceLines.map((line, index) => (
+            {index > 0 && '\n'}{line}
+          ))}
+ ) + : ( +
+ ) return ( -
+} + +/** File-level chrome a unified diff carries above and between hunks. */ +const DIFF_META_PREFIXES = [ + 'diff --git ', 'index ', 'old mode ', 'new mode ', 'new file mode ', 'deleted file mode ', + 'similarity index ', 'dissimilarity index ', 'rename from ', 'rename to ', 'copy from ', 'copy to ', + 'Binary files ', 'GIT binary patch', '\\ No newline', +] + +/** Hunk header with both line counts; a missing count means one line. */ +const HUNK_HEADER = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/u + +/** Lines the hunk header says remain on each side. */ +interface HunkRemainder { + /** Unconsumed old-side lines. */ + old: number + /** Unconsumed new-side lines. */ + new: number +} + +/** + * Parse a hunk header's line budget. + * @param raw - the `@@ -a,b +c,d @@` line. + * @returns the remaining line counts, or `undefined` for a malformed header. + */ +function hunkRemainder(raw: string): HunkRemainder | undefined { + const match = HUNK_HEADER.exec(raw) + if (match === null) return undefined + return { + old: match[2] === undefined ? 1 : Number(match[2]), + new: match[4] === undefined ? 1 : Number(match[4]), + } +} + +/** Whether a raw line is file-level chrome rather than hunk content. */ +function isDiffMeta(raw: string): boolean { + return DIFF_META_PREFIXES.some(prefix => raw.startsWith(prefix)) +} + +/** + * Resolve the inner language a `---`/`+++` header names. + * @param raw - the header line. + * @returns the language id, or `undefined` for `/dev/null` or an unknown path. + */ +function headerLanguage(raw: string): string | undefined { + const path = raw.slice(4).replace(/\t.*$/u, '').trim() + const unquoted = path.startsWith('"') && path.endsWith('"') + ? path.slice(1, -1).replace(/\\(["\\])/gu, '$1') + : path + if (unquoted === '' || unquoted === '/dev/null') return undefined + return languageForPath(unquoted.replace(/^[ab]\//u, '')) +} + +/** + * Split a unified diff into rows without losing a byte (copy reads the DOM). + * + * The hunk header's line budget ends a hunk, so a following `---` / `+++` file + * header is chrome again — both for a concatenated `git diff` and for `diff -u` + * output without `diff --git` lines. A malformed header keeps the row until the + * next `diff --git`, matching how authored fences are rendered. + * @param code - the fence body, display-trimmed. + * @returns the rows and each file section's language. + */ +export function parseDiffFence(code: string): DiffFence { + const rows: DiffRow[] = [] + const languages = new Map() + let inHunk = false + let remainder: HunkRemainder | undefined + let file = -1 + + const push = (row: Omit): void => { + rows.push({ ...row, file }) + } + + for (const raw of code.split('\n')) { + if (raw.startsWith('diff --git ')) { + inHunk = false + remainder = undefined + push({ kind: 'meta', marker: '', body: '', raw }) + continue + } + if (raw.startsWith('@@')) { + inHunk = true + remainder = hunkRemainder(raw) + push({ kind: 'hunk', marker: '', body: '', raw }) + continue + } + // A file header carries a marker column but names the file, not a line. + if (!inHunk && (raw.startsWith('--- ') || raw.startsWith('+++ '))) { + if (raw.startsWith('--- ')) file += 1 + const language = headerLanguage(raw) + if (language !== undefined) languages.set(file, language) + push({ kind: 'meta', marker: '', body: '', raw }) + continue + } + if (isDiffMeta(raw)) { + push({ kind: 'meta', marker: '', body: '', raw }) + continue + } + if (raw.startsWith('+')) { + if (remainder !== undefined) remainder.new -= 1 + push({ kind: 'add', marker: '+', body: raw.slice(1), raw }) + } else if (raw.startsWith('-')) { + if (remainder !== undefined) remainder.old -= 1 + push({ kind: 'del', marker: '-', body: raw.slice(1), raw }) + } else if (raw.startsWith(' ')) { + if (remainder !== undefined) { + remainder.old -= 1 + remainder.new -= 1 + } + push({ kind: 'context', marker: ' ', body: raw.slice(1), raw }) + } else { + // Authored diffs may omit the context marker column; the text is the body. + push({ kind: 'context', marker: '', body: raw, raw }) + } + if (inHunk && remainder !== undefined && remainder.old <= 0 && remainder.new <= 0) inHunk = false + } + return { rows, languages } +} + +/** + * Whether a row carries code the body highlighter tokenizes. + * @param row - the parsed row. + * @returns whether the row has tokenizable body text. + */ +export function isDiffContent(row: DiffRow): boolean { + return row.kind === 'add' || row.kind === 'del' || row.kind === 'context' +} + +/** + * Tokenize each file's old side (context and deletions) and new side (context + * and additions) separately, so an unterminated comment or string in a removed + * line cannot color the added lines and one file's grammar never applies to the + * next. Context rows keep the old-side runs. + * @param diff - parsed fence rows and per-file languages. + * @returns token runs per content-row index, or `undefined` when no body has a + * loaded grammar and the caller should render plain text. + */ +export function diffFenceTokens(diff: DiffFence): Map | undefined { + const groups = new Map>() + for (const [index, row] of diff.rows.entries()) { + if (!isDiffContent(row)) continue + const sides: Array<'old' | 'new'> = row.kind === 'del' + ? ['old'] + : row.kind === 'add' ? ['new'] : ['old', 'new'] + for (const side of sides) { + const key = `${row.file}:${side}` + const entry = { row: index, body: row.body, kind: row.kind } + const list = groups.get(key) + if (list === undefined) groups.set(key, [entry]) + else list.push(entry) + } + } + if (groups.size === 0) return undefined + + const tokens = new Map() + let highlighted = false + for (const [key, entries] of groups) { + const separator = key.lastIndexOf(':') + const language = diff.languages.get(Number(key.slice(0, separator))) + const side = key.slice(separator + 1) + const lines = highlightLines(entries.map(entry => entry.body).join('\n'), language) + if (lines === undefined) continue + highlighted = true + entries.forEach((entry, lineIndex) => { + // A group ending in an empty body line loses shiki's terminal empty row; + // that row stays plain while its siblings keep their tokens. + const line = lines[lineIndex] ?? [] + // Context runs come from the old side; added rows only exist on the new side. + if (side === 'old' || entry.kind === 'add') tokens.set(entry.row, line) + }) + } + return highlighted ? tokens : undefined +} diff --git a/packages/client/ui-primitives/tests/diff-fence.client.spec.tsx b/packages/client/ui-primitives/tests/diff-fence.client.spec.tsx new file mode 100644 index 0000000000..c9d3ea5131 --- /dev/null +++ b/packages/client/ui-primitives/tests/diff-fence.client.spec.tsx @@ -0,0 +1,209 @@ +// @vitest-environment jsdom + +import { afterEach, describe, expect, it } from 'vitest' +import { cleanup, render } from '@testing-library/react' +import type { ComponentProps } from 'react' +import { CodeBlock as LocalizedCodeBlock } from '../src/markdown/CodeBlock.tsx' +import { diffBlockLabels, markdownLabels } from './labels.client.ts' + +function CodeBlock(props: Omit, 'copyLabel' | 'copiedLabel'>) { + return +} + +afterEach(cleanup) + +/** The fence's rendered row elements, in source order. */ +function rows(container: HTMLElement): HTMLElement[] { + return [...container.querySelectorAll('pre code > span.line')] as HTMLElement[] +} + +const TS_DIFF = [ + 'diff --git a/src/app.ts b/src/app.ts', + 'index 1111111..2222222 100644', + '--- a/src/app.ts', + '+++ b/src/app.ts', + '@@ -1,3 +1,4 @@', + ' const value: number = 1', + '-const removed = 1', + '+const added = 2', + 'no marker here', +].join('\n') + +describe('diff fences', () => { + it('classifies added, removed, context, hunk, and file-header rows', () => { + const { container } = render() + const lines = rows(container) + expect(lines).toHaveLength(9) + expect(lines[0]!.className).toContain('_diffMeta_') + expect(lines[1]!.className).toContain('_diffMeta_') + expect(lines[2]!.className).toContain('_diffMeta_') + expect(lines[3]!.className).toContain('_diffMeta_') + expect(lines[4]!.className).toContain('_diffHunk_') + expect(lines[5]!.className).not.toContain('_diffAdd_') + expect(lines[5]!.className).not.toContain('_diffDel_') + expect(lines[6]!.className).toContain('_diffDel_') + expect(lines[7]!.className).toContain('_diffAdd_') + expect(lines[8]!.className).not.toContain('_diffAdd_') + expect(lines[8]!.textContent).toBe('no marker here') + }) + + it('keeps the source byte-identical so copy still reproduces the fence', () => { + const { container } = render() + expect(container.querySelector('pre')!.textContent).toBe(TS_DIFF) + }) + + it('highlights the body with the language the diff header names', () => { + const { container } = render() + expect(container.querySelectorAll('pre code span[style*="--shiki-"]').length).toBeGreaterThan(0) + }) + + it('falls back to plain body text when the diff names no known language', () => { + const code = ['+++ b/notes.unknownext', '@@ -1 +1 @@', '+plain text'].join('\n') + const { container } = render() + expect(rows(container)[2]!.className).toContain('_diffAdd_') + expect(container.querySelectorAll('pre code span[style*="--shiki-"]').length).toBe(0) + }) + + it('treats header-shaped lines after a hunk as changed content', () => { + const code = ['--- a/x.ts', '+++ b/x.ts', '@@ -1 +1,2 @@', '--- removed header-like line', '+++ added header-like line'].join('\n') + const lines = rows(render().container) + expect(lines[0]!.className).toContain('_diffMeta_') + expect(lines[1]!.className).toContain('_diffMeta_') + expect(lines[3]!.className).toContain('_diffDel_') + expect(lines[4]!.className).toContain('_diffAdd_') + }) + + it('resolves the language from the old path when the new side is /dev/null', () => { + const code = ['--- a/src/app.ts', '+++ /dev/null', '@@ -1 +0,0 @@', '-const removed = 1'].join('\n') + const { container } = render() + expect(container.querySelectorAll('pre code span[style*="--shiki-"]').length).toBeGreaterThan(0) + }) + + it('skips /dev/null on the old side of a creation', () => { + const code = ['--- /dev/null', '+++ b/src/new.ts', '@@ -0,0 +1 @@', '+export const created = true'].join('\n') + const { container } = render() + expect(container.querySelectorAll('pre code span[style*="--shiki-"]').length).toBeGreaterThan(0) + }) + + it('renders patch fences the same way', () => { + const { container } = render() + const lines = rows(container) + expect(lines[1]!.className).toContain('_diffDel_') + expect(lines[2]!.className).toContain('_diffAdd_') + }) + + it('renders file-level chrome rows as meta', () => { + const code = [ + 'similarity index 90%', + 'rename from a/x.ts', + 'rename to b/x.ts', + 'Binary files a/x and b/x differ', + '\\ No newline at end of file', + ].join('\n') + const lines = rows(render().container) + expect(lines).toHaveLength(5) + for (const line of lines) expect(line.className).toContain('_diffMeta_') + }) + + + it('keeps the second file header of a git diff as chrome', () => { + const code = [ + 'diff --git a/a.ts b/a.ts', + '--- a/a.ts', + '+++ b/a.ts', + '@@ -1 +1 @@', + '-old', + '+new', + 'diff --git a/b.css b/b.css', + 'index 333..444 100644', + '--- a/b.css', + '+++ b/b.css', + '@@ -1 +1 @@', + '-old', + '+new', + ].join('\n') + const lines = rows(render().container) + expect(lines).toHaveLength(13) + for (const index of [7, 8, 9]) expect(lines[index]!.className).toContain('_diffMeta_') + expect(lines[11]!.className).toContain('_diffDel_') + expect(lines[12]!.className).toContain('_diffAdd_') + }) + + it('ends a hunk by its line budget so concatenated diff -u headers stay chrome', () => { + const code = [ + '--- a/a.ts', + '+++ b/a.ts', + '@@ -1 +1 @@', + '-old', + '+new', + '--- a/b.ts', + '+++ b/b.ts', + '@@ -1 +1 @@', + '-x', + '+y', + ].join('\n') + const lines = rows(render().container) + expect(lines[5]!.className).toContain('_diffMeta_') + expect(lines[6]!.className).toContain('_diffMeta_') + }) + + it('accepts uppercase diff and patch info strings', () => { + const lines = rows(render().container) + expect(lines[1]!.className).toContain('_diffDel_') + expect(lines[2]!.className).toContain('_diffAdd_') + }) + + it('resolves quoted header paths', () => { + const code = ['--- "a/src/app.ts"', '+++ "b/src/app.ts"', '@@ -1 +1 @@', '-const old = 1', '+const next = 2'].join('\n') + const { container } = render() + expect(container.querySelectorAll('pre code span[style*="--shiki-"]').length).toBeGreaterThan(0) + }) + + it('treats every mode, copy, and dissimilarity line as chrome', () => { + const code = ['old mode 100644', 'new mode 100755', 'copy from a/x.ts', 'copy to b/x.ts', 'dissimilarity index 20%'].join('\n') + const lines = rows(render().container) + expect(lines).toHaveLength(5) + for (const line of lines) expect(line.className).toContain('_diffMeta_') + }) + + it('tokenizes the removed and added sides independently', () => { + const code = ['--- a/src/app.ts', '+++ b/src/app.ts', '@@ -1 +1 @@', '-/*', '+const fixed = 1'].join('\n') + const lines = rows(render().container) + const added = [...lines[4]!.querySelectorAll('span')] + const keyword = added.find(span => span.textContent === 'const') + expect(keyword?.style.color).toBe('var(--shiki-token-keyword)') + const removed = [...lines[3]!.querySelectorAll('span')] + const comment = removed.find(span => span.textContent.trim() === '/*') + expect(comment?.style.color).toBe('var(--shiki-token-comment)') + }) + + it('keeps rows when the owner turns wrapping off', () => { + const { container } = render() + expect(container.querySelector('[data-code-wrap="false"]')).toBeTruthy() + expect(rows(container)).toHaveLength(3) + }) + + + it('renders content under a malformed hunk header', () => { + const code = ['@@ not a hunk header @@', '+add', '-del', ' ctx'].join('\n') + const lines = rows(render().container) + expect(lines[0]!.className).toContain('_diffHunk_') + expect(lines[1]!.className).toContain('_diffAdd_') + expect(lines[2]!.className).toContain('_diffDel_') + expect(lines[3]!.className).toContain('_diffContext_') + }) + + it('keeps a group whose last removed line is empty', () => { + const code = ['--- a/src/app.ts', '+++ b/src/app.ts', '@@ -1,2 +1,2 @@', '-a', '-', '+b', '+c'].join('\n') + const lines = rows(render().container) + expect(lines).toHaveLength(7) + expect(lines[4]!.textContent).toBe('-') + expect(lines[5]!.textContent).toBe('+b') + }) + + it('keeps the numbered gutter usable on a diff fence', () => { + const { container } = render() + expect(container.querySelector('[data-line-numbers="true"]')).toBeTruthy() + expect(rows(container)).toHaveLength(9) + }) +})