pi-syntax-highlight 0.0.0-stage → 0.1.0
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.
- package/CHANGELOG.md +60 -0
- package/README.md +68 -2
- package/extensions/index.ts +22 -0
- package/package.json +35 -4
- package/src/active-theme.ts +26 -0
- package/src/detect-language.ts +294 -0
- package/src/emphasis.ts +160 -0
- package/src/grep-parse.ts +42 -0
- package/src/highlight.ts +109 -0
- package/src/markdown-fence-lang.ts +180 -0
- package/src/markdown-style.ts +134 -0
- package/src/markdown-theme.ts +53 -0
- package/src/render-bash.ts +180 -0
- package/src/render-edit.ts +207 -0
- package/src/render-find.ts +64 -0
- package/src/render-generic.ts +86 -0
- package/src/render-grep.ts +142 -0
- package/src/render-ls.ts +66 -0
- package/src/render-path.ts +54 -0
- package/src/render-read.ts +277 -0
- package/src/render-utils.ts +219 -0
- package/src/render-write.ts +81 -0
- package/src/resolve-renderers.ts +406 -0
- package/src/style-path.ts +48 -0
- package/src/tokens.ts +114 -0
- package/test/unit/active-theme.test.ts +38 -0
- package/test/unit/bash-preview.test.ts +57 -0
- package/test/unit/detect-language.test.ts +124 -0
- package/test/unit/emphasis.test.ts +113 -0
- package/test/unit/extensions-index.test.ts +116 -0
- package/test/unit/grep-parse.test.ts +125 -0
- package/test/unit/highlight.test.ts +107 -0
- package/test/unit/markdown-fence-lang.test.ts +131 -0
- package/test/unit/markdown-pipeline.test.ts +153 -0
- package/test/unit/markdown-render-parity.test.ts +127 -0
- package/test/unit/markdown-style.test.ts +152 -0
- package/test/unit/markdown-theme.test.ts +86 -0
- package/test/unit/render-bash.test.ts +194 -0
- package/test/unit/render-edit.test.ts +214 -0
- package/test/unit/render-find.test.ts +139 -0
- package/test/unit/render-generic.test.ts +152 -0
- package/test/unit/render-grep.test.ts +338 -0
- package/test/unit/render-ls.test.ts +132 -0
- package/test/unit/render-read.test.ts +375 -0
- package/test/unit/render-utils.test.ts +324 -0
- package/test/unit/render-write.test.ts +143 -0
- package/test/unit/resolve-renderers.test.ts +474 -0
- package/test/unit/style-path.test.ts +74 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import { activeTheme, captureTheme, resetActiveTheme } from "../../src/active-theme.ts";
|
|
5
|
+
|
|
6
|
+
// The module stores the reference and nothing else, so a bare object stands in
|
|
7
|
+
// for the host's `Theme`, which cannot be constructed outside a session.
|
|
8
|
+
function fakeTheme(label: string): Theme {
|
|
9
|
+
return { label } as unknown as Theme;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
test("activeTheme: no capture yet yields undefined", () => {
|
|
13
|
+
resetActiveTheme();
|
|
14
|
+
assert.equal(activeTheme(), undefined);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("activeTheme: the captured theme comes back as the same reference", () => {
|
|
18
|
+
resetActiveTheme();
|
|
19
|
+
const theme = fakeTheme("dark");
|
|
20
|
+
captureTheme(theme);
|
|
21
|
+
assert.equal(activeTheme(), theme);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("captureTheme: a second capture replaces the first", () => {
|
|
25
|
+
resetActiveTheme();
|
|
26
|
+
const first = fakeTheme("dark");
|
|
27
|
+
const second = fakeTheme("light");
|
|
28
|
+
captureTheme(first);
|
|
29
|
+
captureTheme(second);
|
|
30
|
+
assert.equal(activeTheme(), second);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("resetActiveTheme: the capture is dropped", () => {
|
|
34
|
+
resetActiveTheme();
|
|
35
|
+
captureTheme(fakeTheme("dark"));
|
|
36
|
+
resetActiveTheme();
|
|
37
|
+
assert.equal(activeTheme(), undefined);
|
|
38
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { visibleWidth } from "@earendil-works/pi-tui";
|
|
4
|
+
import { createBashPreview } from "../../src/render-bash.ts";
|
|
5
|
+
|
|
6
|
+
const fakeTheme = {
|
|
7
|
+
fg: (c: string, t: string) => `<${c}>${t}</${c}>`,
|
|
8
|
+
bg: (c: string, t: string) => `<bg:${c}>${t}</bg:${c}>`,
|
|
9
|
+
bold: (t: string) => `<b>${t}</b>`,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const EXPAND_HINT = "<hint>";
|
|
13
|
+
// Byte-identical to the built-in preview hint for five hidden visual lines.
|
|
14
|
+
const HINT_5 = "<muted>... (5 earlier lines,</muted> <hint><muted>)</muted>";
|
|
15
|
+
|
|
16
|
+
const WIDE = 200;
|
|
17
|
+
|
|
18
|
+
function styledLine(index: number): string {
|
|
19
|
+
return `<toolOutput>line${index}</toolOutput>`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function body(count: number): string {
|
|
23
|
+
return Array.from({ length: count }, (_, index) => styledLine(index + 1)).join("\n");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test("createBashPreview: ten lines collapse to the last five plus the hint", () => {
|
|
27
|
+
const lines = createBashPreview(body(10), EXPAND_HINT, fakeTheme).render(WIDE).map((line) => line.trimEnd());
|
|
28
|
+
assert.equal(lines.length, 6);
|
|
29
|
+
assert.equal(lines[0], HINT_5);
|
|
30
|
+
assert.deepEqual(lines.slice(1), [6, 7, 8, 9, 10].map(styledLine));
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("createBashPreview: fewer lines than the limit render without a hint", () => {
|
|
34
|
+
const lines = createBashPreview(body(3), EXPAND_HINT, fakeTheme).render(WIDE).map((line) => line.trimEnd());
|
|
35
|
+
assert.deepEqual(lines, [1, 2, 3].map(styledLine));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("createBashPreview: a narrow width keeps five visual lines and truncates each to the width", () => {
|
|
39
|
+
const long = "0123456789".repeat(10);
|
|
40
|
+
const lines = createBashPreview(long, EXPAND_HINT, fakeTheme).render(10);
|
|
41
|
+
assert.equal(lines.length, 6);
|
|
42
|
+
for (const line of lines) assert.ok(visibleWidth(line) <= 10, `${line} is wider than 10`);
|
|
43
|
+
// The tail of the wrapped line is kept, and the hint sits above it.
|
|
44
|
+
assert.deepEqual(lines.slice(1), Array.from({ length: 5 }, () => "0123456789"));
|
|
45
|
+
assert.ok(lines[0].startsWith("<muted>"), `${lines[0]} is not the hint`);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("createBashPreview: lines are cached per width and dropped by invalidate", () => {
|
|
49
|
+
const preview = createBashPreview(body(10), EXPAND_HINT, fakeTheme);
|
|
50
|
+
const first = preview.render(WIDE);
|
|
51
|
+
assert.equal(preview.render(WIDE), first);
|
|
52
|
+
|
|
53
|
+
preview.invalidate();
|
|
54
|
+
const afterInvalidate = preview.render(WIDE);
|
|
55
|
+
assert.notEqual(afterInvalidate, first);
|
|
56
|
+
assert.deepEqual(afterInvalidate, first);
|
|
57
|
+
});
|
|
@@ -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
|
+
});
|