GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(tui): highlight PHP code blocks without opening tag (#60359) · jonBoone/opencode@c24760c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/tui
    • src
      • parsers-config.ts
    • test
      • php-highlighting.test.tsx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/tui/src/parsers-config.ts‎

Copy file name to clipboardExpand all lines: packages/tui/src/parsers-config.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export default {
123123
},
124124
{
125125
filetype: "php",
126-
wasm: "https://github.com/tree-sitter/tree-sitter-php/releases/download/v0.24.2/tree-sitter-php.wasm",
126+
wasm: "https://github.com/tree-sitter/tree-sitter-php/releases/download/v0.24.2/tree-sitter-php_only.wasm",
127127
queries: {
128128
highlights: [
129129
// NOTE: This nvim-treesitter query is currently broken, because the parser is not compatible with the query apparently.
Collapse file

‎packages/tui/test/php-highlighting.test.tsx‎

Copy file name to clipboard
+122Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.