pi-syntax-highlight 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/DEVELOPER.md +73 -0
  3. package/README.md +65 -2
  4. package/assets/icon.png +0 -0
  5. package/assets/icon.svg +36 -0
  6. package/extensions/index.ts +22 -0
  7. package/package.json +49 -4
  8. package/src/active-theme.ts +26 -0
  9. package/src/detect-language.ts +294 -0
  10. package/src/emphasis.ts +160 -0
  11. package/src/grep-parse.ts +42 -0
  12. package/src/highlight.ts +109 -0
  13. package/src/markdown-fence-lang.ts +180 -0
  14. package/src/markdown-style.ts +134 -0
  15. package/src/markdown-theme.ts +53 -0
  16. package/src/render-bash.ts +180 -0
  17. package/src/render-edit.ts +207 -0
  18. package/src/render-find.ts +64 -0
  19. package/src/render-generic.ts +86 -0
  20. package/src/render-grep.ts +142 -0
  21. package/src/render-ls.ts +66 -0
  22. package/src/render-path.ts +54 -0
  23. package/src/render-read.ts +277 -0
  24. package/src/render-utils.ts +219 -0
  25. package/src/render-write.ts +81 -0
  26. package/src/resolve-renderers.ts +406 -0
  27. package/src/style-path.ts +48 -0
  28. package/src/tokens.ts +114 -0
  29. package/test/unit/active-theme.test.ts +38 -0
  30. package/test/unit/bash-preview.test.ts +57 -0
  31. package/test/unit/detect-language.test.ts +124 -0
  32. package/test/unit/emphasis.test.ts +113 -0
  33. package/test/unit/extensions-index.test.ts +116 -0
  34. package/test/unit/grep-parse.test.ts +125 -0
  35. package/test/unit/highlight.test.ts +107 -0
  36. package/test/unit/markdown-fence-lang.test.ts +131 -0
  37. package/test/unit/markdown-pipeline.test.ts +153 -0
  38. package/test/unit/markdown-render-parity.test.ts +127 -0
  39. package/test/unit/markdown-style.test.ts +152 -0
  40. package/test/unit/markdown-theme.test.ts +86 -0
  41. package/test/unit/render-bash.test.ts +194 -0
  42. package/test/unit/render-edit.test.ts +214 -0
  43. package/test/unit/render-find.test.ts +139 -0
  44. package/test/unit/render-generic.test.ts +152 -0
  45. package/test/unit/render-grep.test.ts +338 -0
  46. package/test/unit/render-ls.test.ts +132 -0
  47. package/test/unit/render-read.test.ts +375 -0
  48. package/test/unit/render-utils.test.ts +324 -0
  49. package/test/unit/render-write.test.ts +143 -0
  50. package/test/unit/resolve-renderers.test.ts +474 -0
  51. package/test/unit/style-path.test.ts +74 -0
  52. package/tsconfig.json +15 -0
@@ -0,0 +1,124 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { detectLanguage } from "../../src/detect-language.ts";
4
+
5
+ test("detectLanguage: a JSON object and a JSON array are both json", () => {
6
+ assert.equal(detectLanguage(`{\n "name": "pi",\n "version": "1.0.0"\n}`), "json");
7
+ // A single line, short, with no newline: the strong signal bypasses the gate.
8
+ assert.equal(detectLanguage("[1, 2, 3]"), "json");
9
+ assert.equal(detectLanguage("{not json}"), undefined);
10
+ });
11
+
12
+ test("detectLanguage: a patch is diff", () => {
13
+ const patch = `diff --git a/src/a.ts b/src/a.ts
14
+ index 1111111..2222222 100644
15
+ --- a/src/a.ts
16
+ +++ b/src/a.ts
17
+ @@ -1,3 +1,3 @@
18
+ -old line
19
+ +new line`;
20
+ assert.equal(detectLanguage(patch), "diff");
21
+ });
22
+
23
+ test("detectLanguage: a shebang names its interpreter", () => {
24
+ assert.equal(detectLanguage(`#!/bin/bash\nset -euo pipefail\necho hi`), "bash");
25
+ assert.equal(detectLanguage(`#!/usr/bin/env python3\nprint("hi")`), "python");
26
+ assert.equal(detectLanguage(`#!/usr/bin/env node\nconsole.log("hi")`), "javascript");
27
+ });
28
+
29
+ test("detectLanguage: SQL needs a statement keyword and a clause keyword", () => {
30
+ assert.equal(detectLanguage(`SELECT id, name\nFROM users\nWHERE active = 1;`), "sql");
31
+ // Prose that opens with a statement keyword but carries no clause is prose.
32
+ assert.equal(detectLanguage("select the option that fits the current layout"), undefined);
33
+ assert.equal(detectLanguage("select the file"), undefined);
34
+ });
35
+
36
+ test("detectLanguage: a def with a colon is python", () => {
37
+ assert.equal(detectLanguage(`def f(x):\n if x:\n return x\n return None`), "python");
38
+ });
39
+
40
+ test("detectLanguage: key lines are yaml, and one colon in prose is not", () => {
41
+ assert.equal(detectLanguage(`name: pi-syntax-highlight\nversion: 0.1.0\ndependencies:\n left-pad: ^1.0.0`), "yaml");
42
+ assert.equal(detectLanguage("Note: the plugin adds syntax highlighting for tool output"), undefined);
43
+ });
44
+
45
+ test("detectLanguage: a section header plus assignments is toml", () => {
46
+ assert.equal(detectLanguage(`[package]\nname = "pi-syntax-highlight"\nversion = "0.1.0"`), "toml");
47
+ });
48
+
49
+ test("detectLanguage: annotations are typescript, plain declarations are javascript", () => {
50
+ assert.equal(detectLanguage(`interface User {\n id: number;\n name: string;\n}`), "typescript");
51
+ assert.equal(detectLanguage(`const items = [1, 2, 3];\nconst doubled = items.map((item) => item * 2);`), "javascript");
52
+ });
53
+
54
+ test("detectLanguage: go needs a package clause, a bare fn is not go", () => {
55
+ assert.equal(detectLanguage(`package main\n\nfunc main() {\n\tprintln("hi")\n}`), "go");
56
+ // A single fn signal is one of Rust's two, so this stays unlabeled instead of
57
+ // becoming a wrong guess.
58
+ assert.equal(detectLanguage(`fn main() {\n println!("hello");\n}`), undefined);
59
+ });
60
+
61
+ test("detectLanguage: go and rust outrank the JavaScript signals they share", () => {
62
+ // `import "fmt"` matches the JavaScript `import` signal, and `let mut` matches
63
+ // its `let` one, so a wrong guess is what happens without the precedence.
64
+ assert.equal(
65
+ detectLanguage(`package main\n\nimport "fmt"\n\nfunc main() {\n\tfmt.Println("hi")\n}`),
66
+ "go",
67
+ );
68
+ assert.equal(detectLanguage(`fn main() {\n let mut x = 1;\n use std::io;\n}`), "rust");
69
+ });
70
+
71
+ test("detectLanguage: only an XML declaration, a doctype, or an html tag are markup", () => {
72
+ assert.equal(detectLanguage(`<?xml version="1.0"?>\n<root>\n <item/>\n</root>`), "xml");
73
+ assert.equal(detectLanguage(`<!DOCTYPE html>\n<html lang="en">\n<body>hi</body>\n</html>`), "xml");
74
+ assert.equal(detectLanguage(`<html lang="en">\n<body>hi</body>\n</html>`), "html");
75
+ assert.equal(detectLanguage(`<div class="x">\n <span>hello</span>\n</div>`), undefined);
76
+ });
77
+
78
+ test("detectLanguage: an access modifier next to a class or void keyword is java", () => {
79
+ assert.equal(
80
+ detectLanguage(`public class Main {\n public static void main(String[] args) {\n System.out.println("hi");\n }\n}`),
81
+ "java",
82
+ );
83
+ assert.equal(detectLanguage(`using System;\n\nnamespace App;\n\npublic class Program { }`), "csharp");
84
+ });
85
+
86
+ test("detectLanguage: a decorator alone does not make a snippet python", () => {
87
+ // `@Injectable()` used to be a Python signal, which stole the block from the
88
+ // type annotation that identifies it.
89
+ assert.equal(detectLanguage(`@Injectable()\nexport class Api {\n base: string;\n}`), "typescript");
90
+ });
91
+
92
+ test("detectLanguage: interface bodies with annotation lines are typescript, not css", () => {
93
+ assert.equal(detectLanguage(`interface A {\n a: string;\n}\ninterface B {\n b: string;\n}`), "typescript");
94
+ });
95
+
96
+ test("detectLanguage: bare key lines are yaml even when the values look like annotations", () => {
97
+ assert.equal(detectLanguage(`name: string\nage: number\nenabled: boolean`), "yaml");
98
+ });
99
+
100
+ test("detectLanguage: a markdown table is not shell", () => {
101
+ assert.equal(
102
+ detectLanguage(`| Language | Runtime |\n|---|---|\n| Rust | native |\n| Go | native |`),
103
+ undefined,
104
+ );
105
+ });
106
+
107
+ test("detectLanguage: a log, prose, and empty text have no language", () => {
108
+ assert.equal(
109
+ detectLanguage(`2024-01-02T03:04:05Z INFO starting\n2024-01-02T03:04:06Z WARN slow\n2024-01-02T03:04:07Z INFO done`),
110
+ undefined,
111
+ );
112
+ assert.equal(
113
+ detectLanguage(
114
+ "This paragraph explains why the transformer runs before rendering.\nIt keeps the fence character and the body untouched.",
115
+ ),
116
+ undefined,
117
+ );
118
+ assert.equal(detectLanguage(""), undefined);
119
+ assert.equal(detectLanguage(" \n\t\n"), undefined);
120
+ });
121
+
122
+ test("detectLanguage: a single short line never carries a language", () => {
123
+ assert.equal(detectLanguage("x = 1"), undefined);
124
+ });
@@ -0,0 +1,113 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { applyEmphasis, findMatchRanges } from "../../src/emphasis.ts";
4
+ import { BOLD_OFF, BOLD_ON } from "../../src/tokens.ts";
5
+
6
+ // Literal mode off, because that is the flag combination the regex cases exercise.
7
+ const REGEX = { literal: false, ignoreCase: false } as const;
8
+ const LITERAL = { literal: true, ignoreCase: false } as const;
9
+
10
+ test("findMatchRanges: literal occurrences are returned in order", () => {
11
+ assert.deepEqual(findMatchRanges("hello world hello", "hello", LITERAL), [
12
+ { start: 0, end: 5 },
13
+ { start: 12, end: 17 },
14
+ ]);
15
+ });
16
+
17
+ test("findMatchRanges: metacharacters are regex unless literal mode escapes them", () => {
18
+ assert.deepEqual(findMatchRanges("a.b axb", "a.b", REGEX), [
19
+ { start: 0, end: 3 },
20
+ { start: 4, end: 7 },
21
+ ]);
22
+ assert.deepEqual(findMatchRanges("a.b axb", "a.b", LITERAL), [{ start: 0, end: 3 }]);
23
+ });
24
+
25
+ test("findMatchRanges: a pattern that is not a valid regex yields no ranges instead of throwing", () => {
26
+ assert.deepEqual(findMatchRanges("a[b", "[", REGEX), []);
27
+ assert.deepEqual(findMatchRanges("a[b", "[", LITERAL), [{ start: 1, end: 2 }]);
28
+ });
29
+
30
+ test("findMatchRanges: a unicode property escape addresses the letters it matches", () => {
31
+ assert.deepEqual(findMatchRanges("ab 12", "\\p{L}+", REGEX), [{ start: 0, end: 2 }]);
32
+ assert.deepEqual(findMatchRanges("12 ab", "\\p{L}+", REGEX), [{ start: 3, end: 5 }]);
33
+ });
34
+
35
+ test("findMatchRanges: the unicode flag rejects an escape only the loose parser accepts", () => {
36
+ // `\-` compiles without `u` and is an invalid escape with it: the pattern falls
37
+ // into the same fallback as any other broken one, so the render never throws.
38
+ assert.deepEqual(findMatchRanges("a-b", "\\-", REGEX), []);
39
+ });
40
+
41
+ test("findMatchRanges: ignoreCase matches the other casing and still reports the real range", () => {
42
+ assert.deepEqual(findMatchRanges("Function function", "FUNCTION", { literal: true, ignoreCase: true }), [
43
+ { start: 0, end: 8 },
44
+ { start: 9, end: 17 },
45
+ ]);
46
+ assert.deepEqual(findMatchRanges("Function function", "FUNCTION", LITERAL), []);
47
+ });
48
+
49
+ test("findMatchRanges: an empty pattern and zero-length matches produce no ranges and do not loop", () => {
50
+ assert.deepEqual(findMatchRanges("anything", "", LITERAL), []);
51
+ assert.deepEqual(findMatchRanges("bcd", "a*", REGEX), []);
52
+ // The empty match at zero is dropped, the real one is kept.
53
+ assert.deepEqual(findMatchRanges("baa", "a*", REGEX), [{ start: 1, end: 3 }]);
54
+ });
55
+
56
+ test("applyEmphasis: markers wrap the matched span of plain text", () => {
57
+ assert.equal(applyEmphasis("hello world", [{ start: 0, end: 5 }], "<b>", "</b>"), "<b>hello</b> world");
58
+ assert.equal(applyEmphasis("hello world", [{ start: 6, end: 11 }], "<b>", "</b>"), "hello <b>world</b>");
59
+ });
60
+
61
+ test("applyEmphasis: markers land on the visible column of a line carrying SGR colors", () => {
62
+ // Visible text is `foobar`; the sequences must survive byte for byte.
63
+ assert.equal(
64
+ applyEmphasis("\x1b[36mfoo\x1b[39mbar", [{ start: 3, end: 6 }], "<b>", "</b>"),
65
+ "\x1b[36mfoo<b>\x1b[39mbar</b>",
66
+ );
67
+ assert.equal(
68
+ applyEmphasis("\x1b[36mfoo\x1b[39mbar", [{ start: 0, end: 6 }], "<b>", "</b>"),
69
+ "<b>\x1b[36mfoo\x1b[39mbar</b>",
70
+ );
71
+ });
72
+
73
+ test("applyEmphasis: an OSC sequence is copied whole and does not shift the visible columns", () => {
74
+ assert.equal(
75
+ applyEmphasis("\x1b]0;title\x07visible", [{ start: 0, end: 3 }], "<b>", "</b>"),
76
+ "<b>\x1b]0;title\x07vis</b>ible",
77
+ );
78
+ });
79
+
80
+ test("applyEmphasis: ranges running past either end are clipped to the text", () => {
81
+ assert.equal(applyEmphasis("abc", [{ start: 1, end: 99 }], "<b>", "</b>"), "a<b>bc</b>");
82
+ assert.equal(applyEmphasis("abc", [{ start: -5, end: 2 }], "<b>", "</b>"), "<b>ab</b>c");
83
+ assert.equal(applyEmphasis("abc", [{ start: 9, end: 12 }], "<b>", "</b>"), "abc");
84
+ });
85
+
86
+ test("applyEmphasis: overlapping and touching ranges collapse into one pair of markers", () => {
87
+ assert.equal(applyEmphasis("abcde", [{ start: 2, end: 5 }, { start: 0, end: 3 }], "<b>", "</b>"), "<b>abcde</b>");
88
+ assert.equal(applyEmphasis("abcdef", [{ start: 0, end: 2 }, { start: 2, end: 4 }], "<b>", "</b>"), "<b>abcd</b>ef");
89
+ });
90
+
91
+ test("applyEmphasis: no usable range returns the text unchanged", () => {
92
+ const styled = "\x1b[36mfoo\x1b[39m";
93
+ assert.equal(applyEmphasis(styled, [], "<b>", "</b>"), styled);
94
+ assert.equal(applyEmphasis("", [], "<b>", "</b>"), "");
95
+ assert.equal(applyEmphasis("", [{ start: 0, end: 3 }], "<b>", "</b>"), "");
96
+ });
97
+
98
+ test("applyEmphasis: the match markers keep the weight only", () => {
99
+ const ranges = findMatchRanges("hello world", "world", LITERAL);
100
+ assert.equal(applyEmphasis("hello world", ranges, BOLD_ON, BOLD_OFF), `hello ${BOLD_ON}world${BOLD_OFF}`);
101
+ });
102
+
103
+ test("applyEmphasis: the bold markers bracket every occurrence found on the styled text", () => {
104
+ // Pins the accepted trade-off: `off` resets the weight only, so the color the
105
+ // line already had is not reopened.
106
+ assert.equal(BOLD_ON, "\x1b[1m");
107
+ assert.equal(BOLD_OFF, "\x1b[22m");
108
+ const ranges = findMatchRanges("function f(", "function", LITERAL);
109
+ assert.equal(
110
+ applyEmphasis("\x1b[36mfunction f(\x1b[39m", ranges, BOLD_ON, BOLD_OFF),
111
+ `${BOLD_ON}\x1b[36mfunction${BOLD_OFF} f(\x1b[39m`,
112
+ );
113
+ });
@@ -0,0 +1,116 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import type {
4
+ ExtensionAPI,
5
+ ExtensionContext,
6
+ MarkdownTransformer,
7
+ SessionStartEvent,
8
+ Theme,
9
+ ToolRendererResolver,
10
+ } from "@earendil-works/pi-coding-agent";
11
+ import piSyntaxHighlight from "../../extensions/index.ts";
12
+ import { activeTheme, resetActiveTheme } from "../../src/active-theme.ts";
13
+ import { resolveToolRenderers } from "../../src/resolve-renderers.ts";
14
+
15
+ const ACCENT = "\x1b[38;5;5m";
16
+
17
+ // The host's `Theme` cannot be constructed outside a session, so the fake supplies
18
+ // the only two members the markdown pass reads, with the accent a colored theme
19
+ // would give it.
20
+ function fakeTheme(): Theme {
21
+ const ansi: Record<string, string> = { accent: ACCENT, mdQuote: "\x1b[39m\x1b[2m" };
22
+ return {
23
+ getFgAnsi: (color: string) => ansi[color] ?? "",
24
+ getBgAnsi: () => "",
25
+ } as unknown as Theme;
26
+ }
27
+
28
+ function fakeContext(theme: Theme): ExtensionContext {
29
+ return { ui: { theme } } as unknown as ExtensionContext;
30
+ }
31
+
32
+ // The factory takes the host's `ExtensionAPI`, which carries dozens of members.
33
+ // This fake declares exactly the three registrations E1 makes; `ExtensionAPI` is a
34
+ // superset of it, so the fake narrows to it without going through `any`.
35
+ interface FakePi {
36
+ registerToolRenderer(resolver: ToolRendererResolver): void;
37
+ registerMarkdownTransformer(transformer: MarkdownTransformer): void;
38
+ on(event: "session_start", handler: (event: SessionStartEvent, ctx: ExtensionContext) => void): () => void;
39
+ }
40
+
41
+ function loadExtension(): {
42
+ renderers: ToolRendererResolver[];
43
+ transformers: MarkdownTransformer[];
44
+ sessionStart: Array<(event: SessionStartEvent, ctx: ExtensionContext) => void>;
45
+ } {
46
+ const renderers: ToolRendererResolver[] = [];
47
+ const transformers: MarkdownTransformer[] = [];
48
+ const sessionStart: Array<(event: SessionStartEvent, ctx: ExtensionContext) => void> = [];
49
+ const fakePi: FakePi = {
50
+ registerToolRenderer: (resolver) => {
51
+ renderers.push(resolver);
52
+ },
53
+ registerMarkdownTransformer: (transformer) => {
54
+ transformers.push(transformer);
55
+ },
56
+ on: (_event, handler) => {
57
+ sessionStart.push(handler);
58
+ return () => {};
59
+ },
60
+ };
61
+ piSyntaxHighlight(fakePi as ExtensionAPI);
62
+ return { renderers, transformers, sessionStart };
63
+ }
64
+
65
+ test("the extension registers exactly one tool renderer and one markdown transformer", () => {
66
+ const { renderers, transformers } = loadExtension();
67
+ assert.equal(renderers.length, 1);
68
+ assert.equal(transformers.length, 1);
69
+ assert.equal(renderers[0], resolveToolRenderers);
70
+ });
71
+
72
+ test("the markdown transformer labels a fence that arrives without a language", () => {
73
+ const { transformers } = loadExtension();
74
+ const markdown = "```\n{\n \"name\": \"pi\"\n}\n```\n";
75
+ assert.equal(
76
+ transformers[0](markdown, { messageType: "assistant", isStreaming: false, availableWidth: 80 }),
77
+ "```json\n{\n \"name\": \"pi\"\n}\n```\n",
78
+ );
79
+ });
80
+
81
+ test("the markdown transformer leaves a fence that already names a language alone", () => {
82
+ const { transformers } = loadExtension();
83
+ const markdown = "# Title\n\n```ts\nconst x = 1;\n```\n";
84
+ assert.equal(transformers[0](markdown, { messageType: "assistant", isStreaming: false, availableWidth: 80 }), markdown);
85
+ });
86
+
87
+ test("the extension registers exactly one session_start handler", () => {
88
+ const { sessionStart } = loadExtension();
89
+ assert.equal(sessionStart.length, 1);
90
+ });
91
+
92
+ test("the session_start handler captures the theme the context hands out", () => {
93
+ resetActiveTheme();
94
+ const { sessionStart } = loadExtension();
95
+ const theme = fakeTheme();
96
+ sessionStart[0]?.({ type: "session_start", reason: "startup" }, fakeContext(theme));
97
+ assert.equal(activeTheme(), theme);
98
+ resetActiveTheme();
99
+ });
100
+
101
+ test("without a captured theme the transformer adds no sequence", () => {
102
+ resetActiveTheme();
103
+ const { transformers } = loadExtension();
104
+ const markdown = "# Title\n\n> quoted\n";
105
+ assert.equal(transformers[0](markdown, { messageType: "assistant", isStreaming: false, availableWidth: 80 }), markdown);
106
+ });
107
+
108
+ test("with a captured theme the transformer styles the assistant message", () => {
109
+ resetActiveTheme();
110
+ const { transformers, sessionStart } = loadExtension();
111
+ sessionStart[0]?.({ type: "session_start", reason: "startup" }, fakeContext(fakeTheme()));
112
+ const styled = transformers[0]("# Title\n", { messageType: "assistant", isStreaming: false, availableWidth: 80 });
113
+ // The capture is the only thing that changed between this and the previous case.
114
+ assert.ok(styled.includes(ACCENT));
115
+ resetActiveTheme();
116
+ });
@@ -0,0 +1,125 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { GREP_LINE_PATTERN, parseGrepLine, parseGrepOutput } from "../../src/grep-parse.ts";
4
+
5
+ test("parseGrepLine: a plain match carries path, line number and text", () => {
6
+ assert.deepEqual(parseGrepLine("src/a.ts:12: const x = 1;"), {
7
+ kind: "match",
8
+ path: "src/a.ts",
9
+ line: 12,
10
+ text: "const x = 1;",
11
+ });
12
+ });
13
+
14
+ test("parseGrepLine: colons and separators inside the matched text stay in the text", () => {
15
+ assert.deepEqual(parseGrepLine("a.ts:3: foo:: bar: baz"), {
16
+ kind: "match",
17
+ path: "a.ts",
18
+ line: 3,
19
+ text: "foo:: bar: baz",
20
+ });
21
+ });
22
+
23
+ test("parseGrepLine: a repeated dash separator is a context line", () => {
24
+ assert.deepEqual(parseGrepLine("a.ts-12- some text"), {
25
+ kind: "context",
26
+ path: "a.ts",
27
+ line: 12,
28
+ text: "some text",
29
+ });
30
+ });
31
+
32
+ test("parseGrepLine: a path containing a colon is not split at the colon", () => {
33
+ assert.deepEqual(parseGrepLine("weird:name.ts:42: some text"), {
34
+ kind: "match",
35
+ path: "weird:name.ts",
36
+ line: 42,
37
+ text: "some text",
38
+ });
39
+ });
40
+
41
+ test("parseGrepLine: a path containing a colon and digits is not split early", () => {
42
+ assert.deepEqual(parseGrepLine("dir/sub:3/file.ts:9: some text"), {
43
+ kind: "match",
44
+ path: "dir/sub:3/file.ts",
45
+ line: 9,
46
+ text: "some text",
47
+ });
48
+ });
49
+
50
+ // pi always writes a space after the line number, so a digit directly after
51
+ // the second separator can only belong to the path (`foo:3` is the file name).
52
+ test("parseGrepLine: digits without the trailing space belong to the path", () => {
53
+ assert.deepEqual(parseGrepLine("foo:3:9: text"), {
54
+ kind: "match",
55
+ path: "foo:3",
56
+ line: 9,
57
+ text: "text",
58
+ });
59
+ });
60
+
61
+ test("parseGrepLine: numeric text after the line number is not mistaken for the line", () => {
62
+ assert.deepEqual(parseGrepLine("a.ts:7: 12: looks numeric"), {
63
+ kind: "match",
64
+ path: "a.ts",
65
+ line: 7,
66
+ text: "12: looks numeric",
67
+ });
68
+ });
69
+
70
+ test("parseGrepLine: the unreadable marker drops the text", () => {
71
+ assert.deepEqual(parseGrepLine("a.ts:9: (unable to read file)"), {
72
+ kind: "unreadable",
73
+ path: "a.ts",
74
+ line: 9,
75
+ });
76
+ });
77
+
78
+ test("parseGrepLine: mismatched separators are plain text", () => {
79
+ assert.deepEqual(parseGrepLine("a-5: x"), { kind: "plain", text: "a-5: x" });
80
+ });
81
+
82
+ test("parseGrepLine: sentinels and blank lines are plain text", () => {
83
+ for (const line of [
84
+ "No matches found",
85
+ "No files found matching pattern",
86
+ "(empty directory)",
87
+ "",
88
+ " ",
89
+ ]) {
90
+ assert.deepEqual(parseGrepLine(line), { kind: "plain", text: line });
91
+ }
92
+ });
93
+
94
+ test("parseGrepLine: a match with empty text is still a match", () => {
95
+ assert.deepEqual(parseGrepLine("a.ts:5: "), { kind: "match", path: "a.ts", line: 5, text: "" });
96
+ });
97
+
98
+ test("parseGrepOutput: keeps the block order and distinguishes matches from context", () => {
99
+ const parsed = parseGrepOutput("a.ts:1: one\na.ts-2- two\na.ts:3: three");
100
+ assert.deepEqual(
101
+ parsed.map((entry) => entry.kind),
102
+ ["match", "context", "match"],
103
+ );
104
+ assert.deepEqual(
105
+ parsed.map((entry) =>
106
+ entry.kind === "match" || entry.kind === "context" || entry.kind === "unreadable" ? entry.line : undefined,
107
+ ),
108
+ [1, 2, 3],
109
+ );
110
+ assert.deepEqual(parseGrepOutput(""), []);
111
+ });
112
+
113
+ test("parseGrepLine: a notice line is not classified as a match", () => {
114
+ for (const line of ["[2 matches limit]", "[Truncated: 100 matches limit, some lines truncated]"]) {
115
+ assert.deepEqual(parseGrepLine(line), { kind: "notice", text: line });
116
+ }
117
+ });
118
+
119
+ test("GREP_LINE_PATTERN: named groups are the ones the parser reads", () => {
120
+ const groups = GREP_LINE_PATTERN.exec("a.ts-8- text")?.groups;
121
+ assert.equal(groups?.path, "a.ts");
122
+ assert.equal(groups?.sep, "-");
123
+ assert.equal(groups?.line, "8");
124
+ assert.equal(groups?.text, "text");
125
+ });
@@ -0,0 +1,107 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { clearHighlightCache, highlight, syncHighlightTheme } from "../../src/highlight.ts";
4
+
5
+ const fakeTheme = {
6
+ fg: (c: string, t: string) => `<${c}>${t}</${c}>`,
7
+ bg: (c: string, t: string) => `<bg:${c}>${t}</bg:${c}>`,
8
+ bold: (t: string) => `<b>${t}</b>`,
9
+ };
10
+
11
+ // `highlightCode` paints through the host's module-global theme, which its proxy
12
+ // resolves from `globalThis` and throws without. Installing one here is what
13
+ // lets the real `highlight` run instead of failing before it reaches the cache.
14
+ const HOST_THEME_KEY = Symbol.for("@earendil-works/pi-coding-agent:theme");
15
+ Object.assign(globalThis, { [HOST_THEME_KEY]: fakeTheme });
16
+
17
+ const THEME_A = { ...fakeTheme };
18
+ const THEME_B = { ...fakeTheme };
19
+
20
+ // A language the highlighter does not know makes `highlightCode` paint every
21
+ // line with one color, so the assertions do not depend on tokenizer output.
22
+ const UNKNOWN_LANG = "no-such-language";
23
+
24
+ // A theme that answers `getFgAnsi` behaves like the host's: one object whose
25
+ // colors change under it. The map keeps the colors mutable, so a theme switch is
26
+ // simulated without swapping the reference.
27
+ function valueTheme(): { theme: { getFgAnsi: (color: string) => string }; ansi: Map<string, string> } {
28
+ const ansi = new Map<string, string>();
29
+ return { ansi, theme: { getFgAnsi: (color: string) => ansi.get(color) ?? `<${color}>` } };
30
+ }
31
+
32
+ test("highlight: the same language and code come back from the cache", () => {
33
+ clearHighlightCache();
34
+ const first = highlight("plain text", UNKNOWN_LANG);
35
+ const second = highlight("plain text", UNKNOWN_LANG);
36
+ // A hit returns the cached array itself, not a repainted copy.
37
+ assert.equal(first, second);
38
+ assert.deepEqual(first, [`<mdCodeBlock>plain text</mdCodeBlock>`]);
39
+ });
40
+
41
+ test("highlight: a different language is a different cache entry", () => {
42
+ clearHighlightCache();
43
+ const first = highlight("plain text", UNKNOWN_LANG);
44
+ const other = highlight("plain text", "another-missing-language");
45
+ assert.notEqual(first, other);
46
+ });
47
+
48
+ test("syncHighlightTheme: a new theme reference invalidates the cache", () => {
49
+ clearHighlightCache();
50
+ syncHighlightTheme(THEME_A);
51
+ const first = highlight("plain text", UNKNOWN_LANG);
52
+ assert.equal(highlight("plain text", UNKNOWN_LANG), first);
53
+
54
+ syncHighlightTheme(THEME_B);
55
+ const recomputed = highlight("plain text", UNKNOWN_LANG);
56
+ assert.notEqual(recomputed, first);
57
+ // Only the paint was dropped: the content is what the source says.
58
+ assert.deepEqual(recomputed, first);
59
+ });
60
+
61
+ test("syncHighlightTheme: the same theme reference keeps the cache", () => {
62
+ clearHighlightCache();
63
+ syncHighlightTheme(THEME_A);
64
+ const first = highlight("plain text", UNKNOWN_LANG);
65
+ syncHighlightTheme(THEME_A);
66
+ assert.equal(highlight("plain text", UNKNOWN_LANG), first);
67
+ });
68
+
69
+ test("syncHighlightTheme: a theme with unchanged colors keeps the cache", () => {
70
+ clearHighlightCache();
71
+ const { theme } = valueTheme();
72
+ syncHighlightTheme(theme);
73
+ const first = highlight("plain text", UNKNOWN_LANG);
74
+ // Every frame of a session renders with the same colors, so the second sync
75
+ // must not throw away what the first one painted.
76
+ syncHighlightTheme(theme);
77
+ assert.equal(highlight("plain text", UNKNOWN_LANG), first);
78
+ });
79
+
80
+ test("syncHighlightTheme: a color that changed under the same reference invalidates the cache", () => {
81
+ clearHighlightCache();
82
+ const { theme, ansi } = valueTheme();
83
+ syncHighlightTheme(theme);
84
+ const first = highlight("plain text", UNKNOWN_LANG);
85
+ ansi.set("syntaxKeyword", "\x1b[38;5;1m");
86
+ syncHighlightTheme(theme);
87
+ const repainted = highlight("plain text", UNKNOWN_LANG);
88
+ assert.notEqual(repainted, first);
89
+ // Only the paint was dropped: the content is what the source says.
90
+ assert.deepEqual(repainted, first);
91
+ });
92
+
93
+ test("syncHighlightTheme: a theme whose getFgAnsi throws is compared by reference", () => {
94
+ clearHighlightCache();
95
+ const getFgAnsi = (): string => {
96
+ throw new Error("Theme not initialized");
97
+ };
98
+ const throwing = { getFgAnsi };
99
+ syncHighlightTheme(throwing);
100
+ const first = highlight("plain text", UNKNOWN_LANG);
101
+ // The reference is the only signal left, and it did not move.
102
+ syncHighlightTheme(throwing);
103
+ assert.equal(highlight("plain text", UNKNOWN_LANG), first);
104
+ // A different reference is the one thing that still invalidates.
105
+ syncHighlightTheme({ getFgAnsi });
106
+ assert.notEqual(highlight("plain text", UNKNOWN_LANG), first);
107
+ });