|
| 1 | +import { afterAll, expect, test } from "bun:test" |
| 2 | +import { addDefaultParsers, CodeRenderable, TreeSitterClient, type Renderable } from "@opentui/core" |
| 3 | +import { testRender } from "@opentui/solid" |
| 4 | +import { ConfigProvider } from "../src/config" |
| 5 | +import { ThemeProvider, useTheme, useThemes } from "../src/context/theme" |
| 6 | +import parsers from "../src/parsers-config" |
| 7 | +import { emptyThemeSource, tmpdir } from "./fixture/fixture" |
| 8 | +import { TestTuiContexts } from "./fixture/tui-environment" |
| 9 | +import { createTuiResolvedConfig } from "./fixture/tui-runtime" |
| 10 | + |
| 11 | +addDefaultParsers(parsers.parsers) |
| 12 | +const data = await tmpdir() |
| 13 | +const client = new TreeSitterClient({ dataPath: data.path }) |
| 14 | + |
| 15 | +afterAll(async () => { |
| 16 | + await client.destroy() |
| 17 | + await data[Symbol.asyncDispose]() |
| 18 | +}) |
| 19 | + |
| 20 | +const snippet = [ |
| 21 | + "$compiled = parent::getCompiledPath($path);", |
| 22 | + "", |
| 23 | + "if ($this->echoFormat === self::DEFAULT_ECHO_FORMAT) {", |
| 24 | + " return $compiled;", |
| 25 | + "}", |
| 26 | +].join("\n") |
| 27 | + |
| 28 | +const content = [ |
| 29 | + "Without the tag:", |
| 30 | + "", |
| 31 | + "```php", |
| 32 | + snippet, |
| 33 | + "```", |
| 34 | + "", |
| 35 | + "With the tag:", |
| 36 | + "", |
| 37 | + "```php", |
| 38 | + "<?php", |
| 39 | + snippet, |
| 40 | + "```", |
| 41 | +].join("\n") |
| 42 | + |
| 43 | +test("highlights PHP fenced blocks both without and with an opening <?php tag", async () => { |
| 44 | + const output = await testRender( |
| 45 | + () => ( |
| 46 | + <TestTuiContexts> |
| 47 | + <ConfigProvider config={createTuiResolvedConfig({ animations: false })}> |
| 48 | + <ThemeProvider mode="dark" source={emptyThemeSource}> |
| 49 | + <Body /> |
| 50 | + </ThemeProvider> |
| 51 | + </ConfigProvider> |
| 52 | + </TestTuiContexts> |
| 53 | + ), |
| 54 | + { width: 80, height: 24, remote: true }, |
| 55 | + ) |
| 56 | + |
| 57 | + try { |
| 58 | + await output.renderOnce() |
| 59 | + await Promise.all(codeBlocks(output.renderer.root).map((node) => node.highlightingDone)) |
| 60 | + await output.renderOnce() |
| 61 | + |
| 62 | + const lines = output.captureSpans().lines |
| 63 | + const prose = lines.find((line) => line.spans.some((span) => span.text.includes("Without the tag:"))) |
| 64 | + const openTag = lines.find((line) => line.spans.some((span) => span.text.includes("<?php"))) |
| 65 | + expect(prose).toBeDefined() |
| 66 | + expect(openTag).toBeDefined() |
| 67 | + |
| 68 | + const proseColor = prose!.spans.find((span) => span.text.includes("Without the tag:"))!.fg.toInts() |
| 69 | + const tagColor = openTag!.spans.find((span) => span.text.includes("<?php"))!.fg.toInts() |
| 70 | + expect(tagColor).not.toEqual(proseColor) |
| 71 | + |
| 72 | + for (const needle of [ |
| 73 | + "$compiled = parent::getCompiledPath($path);", |
| 74 | + "if ($this->echoFormat === self::DEFAULT_ECHO_FORMAT) {", |
| 75 | + "return $compiled;", |
| 76 | + ]) { |
| 77 | + const matches = lines.filter((line) => |
| 78 | + line.spans |
| 79 | + .map((span) => span.text) |
| 80 | + .join("") |
| 81 | + .includes(needle), |
| 82 | + ) |
| 83 | + expect(matches).toHaveLength(2) |
| 84 | + |
| 85 | + const bare = lineSignature(matches[0]!.spans) |
| 86 | + const tagged = lineSignature(matches[1]!.spans) |
| 87 | + expect(new Set(tagged.map((span) => span.fg.join(","))).size).toBeGreaterThan(1) |
| 88 | + expect(bare).toEqual(tagged) |
| 89 | + } |
| 90 | + } finally { |
| 91 | + output.renderer.destroy() |
| 92 | + await output.renderer.closed |
| 93 | + } |
| 94 | +}) |
| 95 | + |
| 96 | +function Body() { |
| 97 | + const theme = useTheme() |
| 98 | + const themes = useThemes() |
| 99 | + return ( |
| 100 | + <markdown |
| 101 | + syntaxStyle={themes.currentSyntax()} |
| 102 | + content={content} |
| 103 | + streaming={false} |
| 104 | + internalBlockMode="top-level" |
| 105 | + conceal |
| 106 | + fg={theme.markdown.text} |
| 107 | + bg={theme.background.base} |
| 108 | + treeSitterClient={client} |
| 109 | + /> |
| 110 | + ) |
| 111 | +} |
| 112 | + |
| 113 | +function codeBlocks(node: Renderable): CodeRenderable[] { |
| 114 | + return [...(node instanceof CodeRenderable ? [node] : []), ...node.getChildren().flatMap(codeBlocks)] |
| 115 | +} |
| 116 | + |
| 117 | +function lineSignature(spans: ReadonlyArray<{ text: string; fg: { toInts(): number[] } }>) { |
| 118 | + return spans.flatMap((span) => { |
| 119 | + const text = span.text.trim() |
| 120 | + return text.length > 0 ? [{ text, fg: span.fg.toInts() }] : [] |
| 121 | + }) |
| 122 | +} |