70160
Ce
cel-tui / files
cel-tui / packages / components / src / syntax-highlight.test.ts
19.7 KB
1import { describe, expect, test } from "bun:test"; 2import { 3 CellBuffer, 4 defaultTheme, 5 emitBuffer, 6 measureContentHeight, 7 type Theme, 8} from "@cel-tui/core"; 9import type { ContainerNode, Node, TextNode } from "@cel-tui/types"; 10import { 11 createSyntaxHighlight, 12 SyntaxHighlight, 13 type SyntaxHighlightNativeTheme, 14 type SyntaxHighlightProps, 15} from "./syntax-highlight.js"; 16 17function asContainer(node: Node): ContainerNode { 18 expect(node.type === "vstack" || node.type === "hstack").toBe(true); 19 return node as ContainerNode; 20} 21 22function asText(node: Node): TextNode { 23 expect(node.type).toBe("text"); 24 return node as TextNode; 25} 26 27function item<T>(items: readonly T[], index: number): T { 28 const value = items[index]; 29 expect(value).toBeDefined(); 30 if (value === undefined) { 31 throw new Error(`Missing item at index ${index}`); 32 } 33 return value; 34} 35 36function lineText(line: Node): string { 37 const container = asContainer(line); 38 return container.children.map((child) => asText(child).content).join(""); 39} 40 41function firstText(line: Node): TextNode { 42 const container = asContainer(line); 43 return asText(item(container.children, 0)); 44} 45 46function textNodes(node: ContainerNode): TextNode[] { 47 return node.children.flatMap((line) => { 48 const container = asContainer(line); 49 return container.children.map((child) => asText(child)); 50 }); 51} 52 53function findText(node: ContainerNode, content: string): TextNode { 54 const text = textNodes(node).find((token) => token.content === content); 55 if (!text) { 56 throw new Error(`Missing token: ${content}`); 57 } 58 return text; 59} 60 61function findTextContaining(node: ContainerNode, content: string): TextNode { 62 const text = textNodes(node).find((token) => token.content.includes(content)); 63 if (!text) { 64 throw new Error(`Missing token containing: ${content}`); 65 } 66 return text; 67} 68 69function lineTokens(line: Node) { 70 const container = asContainer(line); 71 return container.children.map((child) => { 72 const text = asText(child); 73 return { 74 content: text.content, 75 fgColor: text.props.fgColor, 76 bgColor: text.props.bgColor, 77 bold: text.props.bold, 78 italic: text.props.italic, 79 underline: text.props.underline, 80 }; 81 }); 82} 83 84function hasStyledText(node: ContainerNode): boolean { 85 return node.children.some((line) => { 86 const container = asContainer(line); 87 return container.children.some((child) => { 88 const text = asText(child); 89 return ( 90 text.props.fgColor !== undefined || 91 text.props.bgColor !== undefined || 92 text.props.bold === true || 93 text.props.italic === true || 94 text.props.underline === true 95 ); 96 }); 97 }); 98} 99 100function render( 101 content: string, 102 language: string, 103 props?: SyntaxHighlightProps, 104): ContainerNode { 105 return SyntaxHighlight(content, language, props); 106} 107 108describe("SyntaxHighlight", () => { 109 test("renders plain text for unknown languages", () => { 110 const node = render("plain text", "definitely-not-real"); 111 112 expect(node.type).toBe("vstack"); 113 expect(node.children).toHaveLength(1); 114 expect(lineText(item(node.children, 0))).toBe("plain text"); 115 expect(hasStyledText(node)).toBe(false); 116 }); 117 118 test("highlights javascript synchronously through clew", () => { 119 const node = render("const value = 42", "javascript"); 120 121 expect(node.type).toBe("vstack"); 122 expect(node.children).toHaveLength(1); 123 124 const firstLine = asContainer(item(node.children, 0)); 125 const firstToken = asText(item(firstLine.children, 0)); 126 expect(firstLine.props.flexWrap).toBe("wrap"); 127 expect(firstToken.content).toBe("const"); 128 expect(firstToken.props.fgColor).toBeDefined(); 129 }); 130 131 test("highlights python decorators, types, builtins, and strings", () => { 132 const node = render( 133 '@dataclass\nclass Job:\n def render(self):\n print("hi")', 134 "python", 135 ); 136 137 expect(findText(node, "@dataclass").props.fgColor).toBe("color06"); 138 expect(findText(node, "class").props.fgColor).toBe("color05"); 139 expect(findText(node, "Job").props.fgColor).toBe("color06"); 140 expect(findText(node, "render").props.fgColor).toBe("color04"); 141 expect(findText(node, "print").props.fgColor).toBe("color06"); 142 expect(findText(node, '"hi"').props.fgColor).toBe("color02"); 143 }); 144 145 test("highlights richer TypeScript scopes through clew", () => { 146 const node = render( 147 [ 148 "@sealed", 149 "interface Job {", 150 " status: Phase;", 151 "}", 152 'type Phase = "idle";', 153 "const render = (job: Job) => console.log(job.status, config.port);", 154 "const config = { port: 3000 };", 155 ].join("\n"), 156 "typescript", 157 { 158 theme: { 159 name: "syntax-highlight-typescript-semantics", 160 type: "dark", 161 fg: "#e5e5e5", 162 bg: "#000000", 163 tokenColors: [ 164 { settings: { foreground: "#e5e5e5" } }, 165 { scope: "meta", settings: { foreground: "#e5e510" } }, 166 { scope: "type", settings: { foreground: "#2472c8" } }, 167 { scope: "property", settings: { foreground: "#0dbc79" } }, 168 { scope: "function", settings: { foreground: "#cd3131" } }, 169 { scope: "builtin", settings: { foreground: "#bc3fbc" } }, 170 ], 171 }, 172 }, 173 ); 174 175 expect(findText(node, "sealed").props.fgColor).toBe("color03"); 176 expect(findText(node, "Job").props.fgColor).toBe("color04"); 177 expect(findText(node, "status").props.fgColor).toBe("color02"); 178 expect(findText(node, "render").props.fgColor).toBe("color01"); 179 expect(findText(node, "console").props.fgColor).toBe("color05"); 180 }); 181 182 test("highlights bash keywords and builtins", () => { 183 const node = render('if true; then echo "$HOME"; fi', "bash"); 184 185 expect(findText(node, "if").props.fgColor).toBeDefined(); 186 expect(findText(node, "then").props.fgColor).toBeDefined(); 187 expect(findText(node, "fi").props.fgColor).toBeDefined(); 188 expect(findText(node, "echo").props.fgColor).toBeDefined(); 189 expect(findText(node, "$HOME").props.fgColor).toBeUndefined(); 190 }); 191 192 test("highlights json properties and literals", () => { 193 const node = render('{"name":"cel","ok":true,"count":2}', "json"); 194 195 expect(findText(node, '"name"').props.fgColor).toBe("color06"); 196 expect(findText(node, '"cel"').props.fgColor).toBe("color02"); 197 expect(findText(node, "true").props.fgColor).toBe("color05"); 198 expect(findText(node, "2").props.fgColor).toBe("color03"); 199 }); 200 201 test("highlights markdown headings, lists, code, and links", () => { 202 const node = render( 203 "# Title\n- item with `code`\n> quote [link](https://example.com)", 204 "markdown", 205 ); 206 207 expect(findText(node, "Title").props.bold).toBe(true); 208 expect(findText(node, "Title").props.fgColor).toBe("color06"); 209 expect(findText(node, "-").props.fgColor).toBe("color03"); 210 expect(findText(node, "`code`").props.fgColor).toBe("color02"); 211 expect(findText(node, "[link](https://example.com)").props.underline).toBe( 212 true, 213 ); 214 }); 215 216 test("wraps plain fallback content by default", () => { 217 const node = render( 218 "alpha beta gamma delta epsilon", 219 "definitely-not-real", 220 ); 221 222 expect(measureContentHeight(node, { width: 10 })).toBeGreaterThan(1); 223 }); 224 225 test("wraps highlighted content by default", () => { 226 const node = render( 227 "const alpha beta gamma delta epsilon = 42", 228 "javascript", 229 ); 230 231 expect(measureContentHeight(node, { width: 12 })).toBeGreaterThan(1); 232 }); 233 234 test("expands tabs before laying out wrapped highlight lines", () => { 235 const node = render("const\tvalue = 42", "javascript"); 236 237 expect(lineText(item(node.children, 0))).toBe("const value = 42"); 238 expect(measureContentHeight(node, { width: 10 })).toBeGreaterThan(1); 239 }); 240 241 test("default ansi16 theme uses terminal defaults for base identifiers", () => { 242 const node = render("let value = 42", "javascript"); 243 244 expect(findText(node, "let").props.fgColor).toBe("color05"); 245 expect(findTextContaining(node, "value").props.fgColor).toBeUndefined(); 246 expect(findText(node, "42").props.fgColor).toBe("color03"); 247 }); 248 249 test("default ansi16 theme styles bash heredocs as strings", () => { 250 const node = render("cat <<EOF\nhello\nEOF", "bash"); 251 252 expect(findText(node, "cat").props.fgColor).toBe("color04"); 253 expect(findText(node, "hello").props.fgColor).toBe("color02"); 254 expect(findText(node, "EOF").props.fgColor).toBe("color02"); 255 }); 256 257 test("custom theme object matches direct clew scopes for typescript", () => { 258 const node = render('type Phase = "idle"', "typescript", { 259 theme: { 260 name: "syntax-highlight-test-theme", 261 type: "dark", 262 fg: "#e5e5e5", 263 bg: "#000000", 264 tokenColors: [ 265 { settings: { foreground: "#e5e5e5" } }, 266 { 267 scope: ["keyword", "string"], 268 settings: { foreground: "#cd3131" }, 269 }, 270 ], 271 }, 272 }); 273 274 expect(findText(node, "type").props.fgColor).toBe("color01"); 275 expect(findText(node, '"idle"').props.fgColor).toBe("color01"); 276 expect(findText(node, "Phase").props.fgColor).toBe("color06"); 277 }); 278 279 test("custom theme object matches direct clew scopes for bash", () => { 280 const node = render('name="hi"\necho $(pwd) "$name"', "bash", { 281 theme: { 282 name: "syntax-highlight-bash-scope-theme", 283 type: "dark", 284 fg: "#e5e5e5", 285 bg: "#000000", 286 tokenColors: [ 287 { settings: { foreground: "#e5e5e5" } }, 288 { 289 scope: ["command", "builtin"], 290 settings: { foreground: "#0dbc79" }, 291 }, 292 { scope: "variable", settings: { foreground: "#cd3131" } }, 293 { 294 scope: "meta.substitution.command", 295 settings: { foreground: "#bc3fbc" }, 296 }, 297 ], 298 }, 299 }); 300 301 expect(findText(node, "name").props.fgColor).toBe("color01"); 302 expect(findText(node, "echo").props.fgColor).toBe("color02"); 303 expect(findText(node, "$(pwd)").props.fgColor).toBe("color05"); 304 expect(findText(node, "$name").props.fgColor).toBe("color01"); 305 }); 306 307 test("native themes preserve palette slots without hex quantization", () => { 308 const theme: SyntaxHighlightNativeTheme = { 309 baseStyle: { fgColor: "color13", bgColor: "color00" }, 310 scopeStyles: { 311 keyword: { fgColor: "color11" }, 312 number: { fgColor: "color10" }, 313 }, 314 }; 315 const node = render("const value = 42", "typescript", { theme }); 316 317 expect(findText(node, "const").props.fgColor).toBe("color11"); 318 expect(findTextContaining(node, "value").props.fgColor).toBe("color13"); 319 expect(findText(node, "42").props.fgColor).toBe("color10"); 320 expect(findText(node, "const").props.bgColor).toBe("color00"); 321 }); 322 323 test("native palette slots follow runtime theme replacement without re-highlighting", () => { 324 const node = render("const value = 1", "typescript", { 325 theme: { scopeStyles: { keyword: { fgColor: "color12" } } }, 326 }); 327 const keyword = findText(node, "const"); 328 const buffer = new CellBuffer(1, 1); 329 buffer.set(0, 0, { 330 char: "K", 331 fgColor: keyword.props.fgColor ?? null, 332 bgColor: null, 333 bold: false, 334 italic: false, 335 underline: false, 336 }); 337 const firstTheme: Theme = { ...defaultTheme, color12: "#112233" }; 338 const secondTheme: Theme = { ...defaultTheme, color12: "#aabbcc" }; 339 340 expect(emitBuffer(buffer, firstTheme)).toContain("\x1b[38;2;17;34;51m"); 341 expect(emitBuffer(buffer, secondTheme)).toContain("\x1b[38;2;170;187;204m"); 342 expect(keyword.props.fgColor).toBe("color12"); 343 }); 344 345 test("native scope styles preserve explicit false values", () => { 346 const node = render("// plain comment", "typescript", { 347 theme: { 348 scopeStyles: { 349 comment: { bold: false, italic: false, underline: false }, 350 }, 351 }, 352 }); 353 const comment = findText(node, "plain"); 354 355 expect(comment.props.bold).toBe(false); 356 expect(comment.props.italic).toBe(false); 357 expect(comment.props.underline).toBe(false); 358 expect(comment.props.fgColor).toBe("color08"); 359 }); 360 361 test("native fields take precedence over compatible TextMate fields", () => { 362 const node = render("const value = 1", "typescript", { 363 theme: { 364 fg: "#cd3131", 365 tokenColors: [ 366 { 367 scope: "keyword", 368 settings: { 369 foreground: "#0dbc79", 370 fontStyle: "bold italic underline", 371 }, 372 }, 373 ], 374 baseStyle: { fgColor: "color14" }, 375 scopeStyles: { 376 keyword: { 377 fgColor: "color12", 378 bold: false, 379 italic: false, 380 underline: false, 381 }, 382 }, 383 }, 384 }); 385 const keyword = findText(node, "const"); 386 387 expect(findTextContaining(node, "value").props.fgColor).toBe("color14"); 388 expect(keyword.props.fgColor).toBe("color12"); 389 expect(keyword.props.bold).toBe(false); 390 expect(keyword.props.italic).toBe(false); 391 expect(keyword.props.underline).toBe(false); 392 }); 393 394 test("keeps TextMate-only theme registrations compatible", () => { 395 const node = render('const value = "text"', "typescript", { 396 theme: { 397 fg: "#e5e5e5", 398 tokenColors: [ 399 { 400 scope: "keyword", 401 settings: { foreground: "#cd3131", fontStyle: "bold" }, 402 }, 403 { scope: "string", settings: { foreground: "#0dbc79" } }, 404 ], 405 }, 406 }); 407 408 expect(findText(node, "const").props.fgColor).toBe("color01"); 409 expect(findText(node, "const").props.bold).toBe(true); 410 expect(findText(node, '"text"').props.fgColor).toBe("color02"); 411 expect(findTextContaining(node, "value").props.fgColor).toBe("color07"); 412 }); 413 414 test("named theme presets are accepted", () => { 415 const node = render("const value = 42", "javascript", { 416 theme: "dark-plus", 417 }); 418 419 expect(findText(node, "const").props.fgColor).toBeDefined(); 420 }); 421 422 test("keeps one rendered child per source line", () => { 423 render("const first = 1", "javascript"); 424 const node = render("const first = 1\nconst second = 2", "javascript"); 425 426 expect(node.children).toHaveLength(2); 427 expect(lineText(item(node.children, 0))).toBe("const first = 1"); 428 expect(lineText(item(node.children, 1))).toBe("const second = 2"); 429 }); 430 431 test("updates append-only content without duplicating previous text", () => { 432 const highlight = createSyntaxHighlight(); 433 highlight('const message = "hel', "javascript"); 434 const node = highlight('const message = "hello"', "javascript"); 435 436 expect(node.children).toHaveLength(1); 437 expect(lineText(item(node.children, 0))).toBe('const message = "hello"'); 438 }); 439 440 test("produces the same final highlight regardless of append chunk boundaries", () => { 441 const firstChunk = "t"; 442 const secondChunk = "ype Foo<T extends string | number> = { value: T };\n"; 443 const finalContent = `${firstChunk}${secondChunk}const x: Foo<string> = { value: "a" };\n`; 444 const streamedTheme = { 445 name: "syntax-highlight-streamed-boundary-regression", 446 tokenColors: [], 447 }; 448 const directTheme = { 449 name: "syntax-highlight-direct-boundary-regression", 450 tokenColors: [], 451 }; 452 453 const highlight = createSyntaxHighlight(); 454 highlight(firstChunk, "typescript", { theme: streamedTheme }); 455 highlight(`${firstChunk}${secondChunk}`, "typescript", { 456 theme: streamedTheme, 457 }); 458 const streamed = highlight(finalContent, "typescript", { 459 theme: streamedTheme, 460 }); 461 const direct = render(finalContent, "typescript", { theme: directTheme }); 462 463 expect(lineTokens(item(streamed.children, 0))).toEqual( 464 lineTokens(item(direct.children, 0)), 465 ); 466 expect(lineTokens(item(streamed.children, 1))).toEqual( 467 lineTokens(item(direct.children, 1)), 468 ); 469 }); 470 471 test("preserves highlighting across appended bash heredoc lines", () => { 472 const highlight = createSyntaxHighlight(); 473 highlight("cat <<EOF\nhello", "bash"); 474 const node = highlight("cat <<EOF\nhello\nEOF", "bash"); 475 476 expect(node.children).toHaveLength(3); 477 expect(lineText(item(node.children, 0))).toBe("cat <<EOF"); 478 expect(lineText(item(node.children, 1))).toBe("hello"); 479 expect(lineText(item(node.children, 2))).toBe("EOF"); 480 expect(findText(node, "hello").props.fgColor).toBe("color02"); 481 expect(findText(node, "EOF").props.fgColor).toBe("color02"); 482 }); 483 484 test("falls back to full rehighlight on non-append edits", () => { 485 const highlight = createSyntaxHighlight(); 486 highlight("const value = 42", "javascript"); 487 const node = highlight("function value() {}", "javascript"); 488 489 expect(node.children).toHaveLength(1); 490 expect(lineText(item(node.children, 0))).toBe("function value() {}"); 491 expect(firstText(item(node.children, 0)).content).toBe("function"); 492 expect(firstText(item(node.children, 0)).props.fgColor).toBeDefined(); 493 }); 494 495 test("does not infer direct-call identity from content prefixes", () => { 496 const prefix = render("const message", "javascript"); 497 498 render('const message = "hello"', "javascript"); 499 500 expect(render("const message", "javascript")).toBe(prefix); 501 }); 502 503 test("keeps more than four direct-call snippets hot", () => { 504 const snippets = Array.from( 505 { length: 5 }, 506 (_, index) => `const message${index} = ${index}`, 507 ); 508 const firstFrame = snippets.map((source) => render(source, "typescript")); 509 const secondFrame = snippets.map((source) => render(source, "typescript")); 510 511 expect(secondFrame).toEqual(firstFrame); 512 for (let index = 0; index < snippets.length; index++) { 513 expect(secondFrame[index]).toBe(firstFrame[index]); 514 } 515 }); 516 517 test("bounds the direct-render cache", () => { 518 const firstSource = "const cacheBound0 = 0"; 519 const first = render(firstSource, "typescript"); 520 521 for (let index = 1; index <= 128; index++) { 522 render(`const cacheBound${index} = ${index}`, "typescript"); 523 } 524 525 expect(render(firstSource, "typescript")).not.toBe(first); 526 }); 527 528 test("isolates append streams in callable instances", () => { 529 const first = createSyntaxHighlight(); 530 const second = createSyntaxHighlight(); 531 532 const firstInitial = first('const message = "hel', "typescript"); 533 const secondInitial = second('const message = "hel', "typescript"); 534 535 const firstResult = first('const message = "hello"', "typescript"); 536 const secondResult = second('const message = "help"', "typescript"); 537 538 expect(secondInitial).not.toBe(firstInitial); 539 expect(lineText(item(firstResult.children, 0))).toBe( 540 'const message = "hello"', 541 ); 542 expect(lineText(item(secondResult.children, 0))).toBe( 543 'const message = "help"', 544 ); 545 }); 546 547 test("reuses an instance node until its content changes", () => { 548 const highlight = createSyntaxHighlight(); 549 const first = highlight("const value = 1", "typescript"); 550 551 expect(highlight("const value = 1", "typescript")).toBe(first); 552 expect(highlight("const value = 12", "typescript")).not.toBe(first); 553 }); 554 555 test("dispose releases a callable instance's parser state", () => { 556 const highlight = createSyntaxHighlight(); 557 const beforeDispose = highlight("const value = 1", "typescript"); 558 559 highlight.dispose(); 560 561 expect(highlight("const value = 1", "typescript")).not.toBe(beforeDispose); 562 }); 563 564 test("reuses resolved custom themes created from equivalent registrations", () => { 565 const firstTheme = { 566 name: "cached-theme", 567 tokenColors: [{ scope: "keyword", settings: { foreground: "#cd3131" } }], 568 } as const; 569 const secondTheme = { 570 name: "cached-theme", 571 tokenColors: [{ scope: "keyword", settings: { foreground: "#cd3131" } }], 572 } as const; 573 574 const first = render("const value = 1", "typescript", { 575 theme: firstTheme, 576 }); 577 const second = render("const value = 1", "typescript", { 578 theme: secondTheme, 579 }); 580 581 expect(second).toBe(first); 582 }); 583});