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,294 @@
1
+ // Language detection for markdown fences that arrive without an info string.
2
+ //
3
+ // A fence body is a short snippet, so every heuristic here is a regular
4
+ // expression over the raw text: parsing would cost more than the wrong color it
5
+ // saves. The order of the detectors is part of the contract, because the looser
6
+ // signals (`import `, `class `, `: string`) would otherwise take snippets away
7
+ // from the stricter languages that share them.
8
+ //
9
+ // Two regex conventions run through the file: `[ \t]` where a line break must
10
+ // not count as a separator, and `\s` where the spec defines the signal that way.
11
+
12
+ // Guessing from a one-liner is a coin flip, and a wrong language colors a block
13
+ // worse than no language at all, so short input needs two non-blank lines or
14
+ // forty characters. Strong signals are conclusive on their own and bypass the
15
+ // gate: a parseable JSON document, a shebang, a diff header, an XML declaration.
16
+ const MIN_NON_BLANK_LINES = 2;
17
+ const MIN_CHARACTERS = 40;
18
+
19
+ const JSON_OPENERS = new Set(["{", "["]);
20
+
21
+ const DIFF_HEADER = /^(diff --git|--- |\+\+\+ |@@ )/;
22
+
23
+ const MARKUP_DECLARATIONS = ["<?xml", "<!DOCTYPE"];
24
+
25
+ const SHEBANG = /^#!/;
26
+
27
+ // Every interpreter here is painted by the one shell grammar, which highlight.js
28
+ // registers as `bash`; only the shebang decides which of them was meant.
29
+ const SHEBANG_INTERPRETERS: ReadonlyArray<readonly [RegExp, string]> = [
30
+ [/python/, "python"],
31
+ [/\bnode\b/, "javascript"],
32
+ [/\bruby\b/, "ruby"],
33
+ [/\bperl\b/, "perl"],
34
+ [/\b(?:bash|zsh|sh)\b/, "bash"],
35
+ ];
36
+
37
+ const CSS_SELECTOR_BLOCK = /^[^{}\n]*?\{\s*$/gm;
38
+ const CSS_DECLARATION = /^\s*[-a-zA-Z][-\w]*\s*:\s*[^;{}\n]+;$/gm;
39
+ // Any of these means the block is a program that happens to carry braces and
40
+ // `name: value;` lines, not a stylesheet. A type annotation or an import list
41
+ // carries the same `key: value` shape as a declaration.
42
+ const CSS_FOREIGN_SIGNAL =
43
+ /(?:=>|\bfunction\s|\bdef\s|\bconst\s|\binterface\s|\btype[ \t]+\w+[ \t]*=|\bexport\s|\bimport\s)/;
44
+
45
+ const SQL_STATEMENT_KEYWORD = /^(?:SELECT|INSERT|UPDATE|DELETE|CREATE|ALTER|DROP|WITH)\b/i;
46
+ const SQL_CLAUSE_KEYWORD = /\b(?:FROM|WHERE|VALUES|TABLE)\b/i;
47
+
48
+ // Anchored to the start of a line and closed by the colon that ends a Python
49
+ // header: `public class Foo {` and `def f()` inside a patch are those other
50
+ // languages, even though they carry the bare words. A decorator is not a signal
51
+ // of its own: `@Component` and `@Override` are TypeScript and Java too.
52
+ const PYTHON_SIGNALS: readonly RegExp[] = [
53
+ /^[ \t]*(?:async[ \t]+)?def[ \t]+\w+.*:[ \t]*$/m,
54
+ /^[ \t]*class[ \t]+\w+.*:[ \t]*$/m,
55
+ /^[ \t]*from[ \t]+\S+[ \t]+import[ \t]/m,
56
+ /^[ \t]*import[ \t]+[\w.]+[ \t]*(?:,[ \t]*[\w.]+[ \t]*)*(?:as[ \t]+\w+)?$/m,
57
+ /\bif __name__\b/,
58
+ ];
59
+ // Indentation is spaces and tabs only: a `\s` here would let the line break
60
+ // before a top-level `key:` line count as one of the two indented blocks.
61
+ const PYTHON_BLOCK_LINE = /^[ \t]+\S.*:[ \t]*$/gm;
62
+
63
+ const TYPESCRIPT_SIGNALS: readonly RegExp[] = [
64
+ /\binterface\s+\w+/,
65
+ /^[ \t]*(?:export[ \t]+)?type[ \t]+\w+[ \t]*=/m,
66
+ /\bimport[ \t]+type\b/,
67
+ /:[ \t]*(?:string|number|boolean)\b/,
68
+ ];
69
+
70
+ const JAVASCRIPT_SIGNALS: readonly RegExp[] = [
71
+ /^[ \t]*(?:const|let|var|function|export|import)[ \t]/m,
72
+ /=>/,
73
+ ];
74
+
75
+ const GO_PACKAGE = /^package[ \t]+\w+[ \t]*$/m;
76
+ const GO_FUNC = /^func[ \t]/m;
77
+
78
+ // Rust needs two of these: `fn` and `use` also open single-line snippets of
79
+ // other languages, and `pub fn` is the one signal that stands alone.
80
+ const RUST_SIGNALS: readonly RegExp[] = [
81
+ /^[ \t]*fn[ \t]+\w+/m,
82
+ /^[ \t]*use[ \t]+[\w:]/m,
83
+ /^[ \t]*impl\b/m,
84
+ /\blet[ \t]+mut[ \t]/,
85
+ ];
86
+ const RUST_PUBLIC_FUNC = /\bpub[ \t]+(?:async[ \t]+)?fn[ \t]+\w+/;
87
+
88
+ const CSHARP_DIRECTIVE = /^[ \t]*(?:namespace[ \t]+[\w.]+|using[ \t]+[\w.]+)/m;
89
+ const KOTLIN_FUNC = /^[ \t]*fun[ \t]+\w+/m;
90
+ const ACCESS_MODIFIER = /\b(?:public|private|protected)\b/;
91
+ const CLASS_MEMBER_KEYWORD = /\b(?:class|void|static)\b/;
92
+
93
+ const YAML_KEY = /^[A-Za-z_][\w.-]*:\s/gm;
94
+ const YAML_ITEM = /^[ \t]*-[ \t]/gm;
95
+ // A statement terminator or a brace means the block is code carrying
96
+ // `key: value` pairs, which JSON, TypeScript and C all do.
97
+ const STATEMENT_TERMINATOR = /[;{}][ \t]*$/m;
98
+
99
+ // A bare `key: value` line at the left margin. Two of them with none of the
100
+ // syntax a program needs is a YAML document; `name: string;` is a type
101
+ // annotation and `{ a: 1 }` is code, both ruled out by the pattern below.
102
+ const YAML_PLAIN_KEY = /^[A-Za-z_][\w.-]*:[ \t]*\S/gm;
103
+ const YAML_FOREIGN_SIGNAL = /[{(=]|\b(?:const|let|function)\s/;
104
+
105
+ const TOML_SECTION = /^\[[\w.-]+\][ \t]*$/m;
106
+ const TOML_KEY_VALUE = /^\w[\w.-]*[ \t]*=/gm;
107
+
108
+ const SHELL_COMMAND =
109
+ /^(?:\$[ \t]|\w+=|export[ \t]|cd[ \t]|sudo[ \t]|git[ \t]|npm[ \t]|node[ \t]|ls[ \t]|cat[ \t]|echo[ \t]|mkdir[ \t]|rm[ \t]|cp[ \t]|mv[ \t]|make[ \t]|docker[ \t])/gm;
110
+ const SHELL_PIPELINE = /\|[ \t]*[\w./-]+/gm;
111
+ // A markdown table is a grid of pipes whose rows read like a pipeline, and its
112
+ // separator row is the one line shell output never prints.
113
+ const TABLE_ROW = /^[ \t]*\|/;
114
+ const TABLE_RULE = "|---";
115
+
116
+ type Detector = (text: string) => string | undefined;
117
+
118
+ function detectJson(text: string): string | undefined {
119
+ return isJsonDocument(text) ? "json" : undefined;
120
+ }
121
+
122
+ function detectDiff(text: string): string | undefined {
123
+ return DIFF_HEADER.test(firstNonBlankLine(text)) ? "diff" : undefined;
124
+ }
125
+
126
+ function detectShebang(text: string): string | undefined {
127
+ const first = firstNonBlankLine(text);
128
+ if (!SHEBANG.test(first)) return undefined;
129
+ for (const [pattern, language] of SHEBANG_INTERPRETERS) {
130
+ if (pattern.test(first)) return language;
131
+ }
132
+ return undefined;
133
+ }
134
+
135
+ function detectMarkup(text: string): string | undefined {
136
+ const first = firstNonBlankLine(text);
137
+ if (MARKUP_DECLARATIONS.some((declaration) => first.includes(declaration))) return "xml";
138
+ return first.includes("<html") ? "html" : undefined;
139
+ }
140
+
141
+ function detectCss(text: string): string | undefined {
142
+ if (CSS_FOREIGN_SIGNAL.test(text)) return undefined;
143
+ if (countMatches(text, CSS_SELECTOR_BLOCK) < 2) return undefined;
144
+ return countMatches(text, CSS_DECLARATION) >= 2 ? "css" : undefined;
145
+ }
146
+
147
+ function detectSql(text: string): string | undefined {
148
+ const firstWord = firstNonBlankLine(text).trim().split(/\s+/)[0] ?? "";
149
+ if (!SQL_STATEMENT_KEYWORD.test(firstWord)) return undefined;
150
+ return SQL_CLAUSE_KEYWORD.test(text) ? "sql" : undefined;
151
+ }
152
+
153
+ function detectPython(text: string): string | undefined {
154
+ if (countSignals(text, PYTHON_SIGNALS) > 0) return "python";
155
+ return countMatches(text, PYTHON_BLOCK_LINE) >= 2 ? "python" : undefined;
156
+ }
157
+
158
+ function detectTypeScript(text: string): string | undefined {
159
+ return hasTypeScriptSignal(text) ? "typescript" : undefined;
160
+ }
161
+
162
+ function detectJavaScript(text: string): string | undefined {
163
+ return countSignals(text, JAVASCRIPT_SIGNALS) > 0 ? "javascript" : undefined;
164
+ }
165
+
166
+ function detectGo(text: string): string | undefined {
167
+ return GO_PACKAGE.test(text) && GO_FUNC.test(text) ? "go" : undefined;
168
+ }
169
+
170
+ function detectRust(text: string): string | undefined {
171
+ if (RUST_PUBLIC_FUNC.test(text)) return "rust";
172
+ return countSignals(text, RUST_SIGNALS) >= 2 ? "rust" : undefined;
173
+ }
174
+
175
+ function detectManagedLanguage(text: string): string | undefined {
176
+ // `using`/`namespace` and `fun` identify one language on their own; a bare
177
+ // access modifier is shared by all three, and Java is the most common source
178
+ // of that shape.
179
+ if (CSHARP_DIRECTIVE.test(text)) return "csharp";
180
+ if (KOTLIN_FUNC.test(text)) return "kotlin";
181
+ return ACCESS_MODIFIER.test(text) && CLASS_MEMBER_KEYWORD.test(text) ? "java" : undefined;
182
+ }
183
+
184
+ function detectYaml(text: string): string | undefined {
185
+ if (countMatches(text, YAML_KEY) + countMatches(text, YAML_ITEM) < 2) return undefined;
186
+ return STATEMENT_TERMINATOR.test(text) ? undefined : "yaml";
187
+ }
188
+
189
+ function detectYamlKeys(text: string): string | undefined {
190
+ if (countMatches(text, YAML_PLAIN_KEY) < 2) return undefined;
191
+ return YAML_FOREIGN_SIGNAL.test(text) ? undefined : "yaml";
192
+ }
193
+
194
+ function detectToml(text: string): string | undefined {
195
+ if (!TOML_SECTION.test(text)) return undefined;
196
+ return countMatches(text, TOML_KEY_VALUE) >= 2 ? "toml" : undefined;
197
+ }
198
+
199
+ function detectShell(text: string): string | undefined {
200
+ if (text.includes(TABLE_RULE)) return undefined;
201
+ const body = nonTableLines(text);
202
+ if (countMatches(body, SHELL_COMMAND) >= 2) return "bash";
203
+ return countMatches(body, SHELL_PIPELINE) >= 2 ? "bash" : undefined;
204
+ }
205
+
206
+ // Rows of a markdown table look like a pipeline, so they are dropped before
207
+ // either shell signal is counted.
208
+ function nonTableLines(text: string): string {
209
+ return text
210
+ .split("\n")
211
+ .filter((line) => !TABLE_ROW.test(line))
212
+ .join("\n");
213
+ }
214
+
215
+ // The first detector that recognizes the snippet wins; the list runs from the
216
+ // most to the least specific signal. No detector answers `markdown` or
217
+ // `plaintext`, which would repaint the block worse than the flat color it has.
218
+ // `go` and `rust` precede TypeScript and JavaScript because `import "fmt"` and
219
+ // `let mut` match the JavaScript signals, and their own gates are stricter.
220
+ // TypeScript precedes JavaScript for the same reason: `interface A { b: string }`
221
+ // matches both, and the annotation is the more specific answer.
222
+ // Bare `key: value` lines precede TypeScript too, because `name: string` is a
223
+ // YAML entry as much as it is a type annotation; the one without braces and
224
+ // assignments is the YAML document.
225
+ const DETECTORS: readonly Detector[] = [
226
+ detectJson,
227
+ detectDiff,
228
+ detectShebang,
229
+ detectMarkup,
230
+ detectCss,
231
+ detectSql,
232
+ detectPython,
233
+ detectGo,
234
+ detectRust,
235
+ detectYamlKeys,
236
+ detectTypeScript,
237
+ detectJavaScript,
238
+ detectManagedLanguage,
239
+ detectYaml,
240
+ detectToml,
241
+ detectShell,
242
+ ];
243
+
244
+ export function detectLanguage(text: string): string | undefined {
245
+ const lines = nonBlankLines(text);
246
+ if (!hasStrongSignal(text, lines) && lines.length < MIN_NON_BLANK_LINES && text.trim().length < MIN_CHARACTERS) {
247
+ return undefined;
248
+ }
249
+ for (const detect of DETECTORS) {
250
+ const language = detect(text);
251
+ if (language !== undefined) return language;
252
+ }
253
+ return undefined;
254
+ }
255
+
256
+ function nonBlankLines(text: string): string[] {
257
+ return text.split("\n").filter((line) => line.trim() !== "");
258
+ }
259
+
260
+ function firstNonBlankLine(text: string): string {
261
+ return nonBlankLines(text)[0] ?? "";
262
+ }
263
+
264
+ function isJsonDocument(text: string): boolean {
265
+ if (!JSON_OPENERS.has(text.trimStart().charAt(0))) return false;
266
+ try {
267
+ JSON.parse(text);
268
+ return true;
269
+ } catch {
270
+ return false;
271
+ }
272
+ }
273
+
274
+ function hasStrongSignal(text: string, lines: readonly string[]): boolean {
275
+ if (isJsonDocument(text)) return true;
276
+ const first = lines[0] ?? "";
277
+ if (first.startsWith("#!")) return true;
278
+ if (first.startsWith("diff --git")) return true;
279
+ return MARKUP_DECLARATIONS.some((declaration) => first.includes(declaration));
280
+ }
281
+
282
+ // Every pattern passed here carries the global flag, so `match` returns all the
283
+ // hits instead of the first one.
284
+ function countMatches(text: string, pattern: RegExp): number {
285
+ return text.match(pattern)?.length ?? 0;
286
+ }
287
+
288
+ function countSignals(text: string, signals: readonly RegExp[]): number {
289
+ return signals.filter((signal) => signal.test(text)).length;
290
+ }
291
+
292
+ function hasTypeScriptSignal(text: string): boolean {
293
+ return countSignals(text, TYPESCRIPT_SIGNALS) > 0;
294
+ }
@@ -0,0 +1,160 @@
1
+ // Emphasis marks the spans that `grep` matched inside a line that is already
2
+ // colored. Ranges are expressed in visible columns, because the styled text
3
+ // carries ANSI sequences that take no room on screen.
4
+
5
+ export interface Range {
6
+ start: number;
7
+ end: number;
8
+ }
9
+
10
+ export interface MatchOptions {
11
+ literal: boolean;
12
+ ignoreCase: boolean;
13
+ }
14
+
15
+ // The full set the RegExp constructor treats specially; escaping all of it keeps
16
+ // a literal pattern (the usual grep input) out of the parser.
17
+ const REGEXP_METACHARACTERS = /[.*+?^${}()|[\]\\]/g;
18
+
19
+ // Sticky so the scan reads each escape once instead of rescanning the rest of
20
+ // the string. SGR (`ESC [ ... m`) and OSC (`ESC ] ... BEL`) are the only
21
+ // sequences the theme and the highlighter emit.
22
+ const ANSI_SEQUENCE = /\x1b\[[0-9;]*m|\x1b\][^\x07]*\x07/y;
23
+
24
+ const ESCAPE = "\x1b";
25
+
26
+ function escapeLiteral(pattern: string): string {
27
+ return pattern.replace(REGEXP_METACHARACTERS, "\\$&");
28
+ }
29
+
30
+ // Length of the escape sequence starting at `index`, or zero when the character
31
+ // there is visible. The early return is the fast path: this runs once per
32
+ // character of every emphasized line.
33
+ function ansiLengthAt(text: string, index: number): number {
34
+ if (text[index] !== ESCAPE) return 0;
35
+ ANSI_SEQUENCE.lastIndex = index;
36
+ const match = ANSI_SEQUENCE.exec(text);
37
+ return match === null ? 0 : match[0].length;
38
+ }
39
+
40
+ function visibleLength(text: string): number {
41
+ let raw = 0;
42
+ let visible = 0;
43
+ while (raw < text.length) {
44
+ const ansi = ansiLengthAt(text, raw);
45
+ if (ansi > 0) {
46
+ raw += ansi;
47
+ continue;
48
+ }
49
+ raw += 1;
50
+ visible += 1;
51
+ }
52
+ return visible;
53
+ }
54
+
55
+ function mergeRanges(ranges: Range[]): Range[] {
56
+ const sorted = [...ranges].sort((left, right) => left.start - right.start);
57
+ const merged: Range[] = [];
58
+ for (const range of sorted) {
59
+ // Touching ranges are merged as well: two marker pairs back to back render
60
+ // exactly like one, and one pair is cheaper to insert.
61
+ if (merged.length > 0) {
62
+ const previous = merged[merged.length - 1];
63
+ if (range.start <= previous.end) {
64
+ previous.end = Math.max(previous.end, range.end);
65
+ continue;
66
+ }
67
+ }
68
+ merged.push({ start: range.start, end: range.end });
69
+ }
70
+ return merged;
71
+ }
72
+
73
+ // Ranges become visible columns here: anything outside the text is dropped
74
+ // before the caller can insert a marker where there is no character to wrap.
75
+ function clipRanges(ranges: Range[], limit: number): Range[] {
76
+ const clipped: Range[] = [];
77
+ for (const range of ranges) {
78
+ const start = Math.max(0, Math.min(range.start, limit));
79
+ const end = Math.max(0, Math.min(range.end, limit));
80
+ if (start < end) clipped.push({ start, end });
81
+ }
82
+ return mergeRanges(clipped);
83
+ }
84
+
85
+ export function findMatchRanges(text: string, pattern: string, options: MatchOptions): Range[] {
86
+ // An empty pattern matches everywhere and nowhere at once, and grep never
87
+ // sends it; treating it as "no match" keeps the line plain.
88
+ if (pattern === "") return [];
89
+ const source = options.literal ? escapeLiteral(pattern) : pattern;
90
+ let regex: RegExp;
91
+ try {
92
+ // The unicode flag makes `\p{L}` and friends address the characters grep
93
+ // matched; a pattern only the non-unicode parser accepts (`\-`) fails here
94
+ // and takes the same fallback as any other broken pattern.
95
+ regex = new RegExp(source, options.ignoreCase ? "giu" : "gu");
96
+ } catch {
97
+ // A half-typed pattern must not break the render: no ranges means no emphasis.
98
+ return [];
99
+ }
100
+ const ranges: Range[] = [];
101
+ for (const match of text.matchAll(regex)) {
102
+ // `matchAll` always reports `index`; the `?? 0` also satisfies the optional
103
+ // declaration older lib versions carry.
104
+ const start = match.index ?? 0;
105
+ const end = start + match[0].length;
106
+ // Zero-length matches (`a*`, `\b`) would repeat at the same offset forever
107
+ // and there is no character to wrap.
108
+ if (start === end) continue;
109
+ ranges.push({ start, end });
110
+ }
111
+ return clipRanges(ranges, text.length);
112
+ }
113
+
114
+ type MarkerEvent = { column: number; marker: string };
115
+
116
+ // `text` arrives from `highlightCode`, so it interleaves visible characters with
117
+ // ANSI sequences. Both indices advance together: `raw` walks the string, and
118
+ // `visible` is the column a range can address. Markers are only ever inserted on
119
+ // a boundary of the second index, which is why a sequence is never split. The
120
+ // outer color is not restored after `off`: `\x1b[22m` only cancels the weight,
121
+ // which is the accepted behaviour.
122
+ export function applyEmphasis(text: string, ranges: Range[], on: string, off: string): string {
123
+ if (ranges.length === 0) return text;
124
+ const inRange = clipRanges(ranges, visibleLength(text));
125
+ if (inRange.length === 0) return text;
126
+ // One open and one close per range, already in column order because the ranges
127
+ // come out of `clipRanges` sorted and non-overlapping.
128
+ const events: MarkerEvent[] = inRange.flatMap((range) => [
129
+ { column: range.start, marker: on },
130
+ { column: range.end, marker: off },
131
+ ]);
132
+ let styled = "";
133
+ let raw = 0;
134
+ let visible = 0;
135
+ let next = 0;
136
+ while (raw < text.length) {
137
+ // Markers go before the token that starts at this column, so an escape
138
+ // sequence is copied whole.
139
+ while (next < events.length && events[next].column === visible) {
140
+ styled += events[next].marker;
141
+ next += 1;
142
+ }
143
+ const ansi = ansiLengthAt(text, raw);
144
+ if (ansi > 0) {
145
+ styled += text.slice(raw, raw + ansi);
146
+ raw += ansi;
147
+ continue;
148
+ }
149
+ styled += text[raw];
150
+ raw += 1;
151
+ visible += 1;
152
+ }
153
+ // A range ending on the last column has no token after it to trigger the check
154
+ // above, and by now only closes can be pending.
155
+ while (next < events.length) {
156
+ styled += events[next].marker;
157
+ next += 1;
158
+ }
159
+ return styled;
160
+ }
@@ -0,0 +1,42 @@
1
+ // Grep writes matches as `path:line: text` and context lines as
2
+ // `path-line- text`. The separator is repeated around the line number, which is
3
+ // what makes paths containing `:` or `-` parseable: the lazy path plus the digit
4
+ // anchor backtrack until both separators agree.
5
+ export const GREP_LINE_PATTERN = /^(?<path>.*?)(?<sep>:|-)(?<line>\d+)\k<sep> (?<text>[\s\S]*)$/;
6
+
7
+ export const UNREADABLE_TEXT = "(unable to read file)";
8
+
9
+ export type GrepLine =
10
+ | { kind: "match"; path: string; line: number; text: string }
11
+ | { kind: "context"; path: string; line: number; text: string }
12
+ | { kind: "unreadable"; path: string; line: number }
13
+ | { kind: "notice"; text: string }
14
+ | { kind: "plain"; text: string };
15
+
16
+ // The tool appends its limit block as one bracketed line and stock pi counts it
17
+ // as body text, so it is a body line here too, painted apart from a plain one.
18
+ const NOTICE_LINE = /^\[.*\]$/;
19
+
20
+ export function parseGrepLine(line: string): GrepLine {
21
+ const matched = parseMatchLine(line);
22
+ if (matched !== undefined) return matched;
23
+ return NOTICE_LINE.test(line) ? { kind: "notice", text: line } : { kind: "plain", text: line };
24
+ }
25
+
26
+ function parseMatchLine(line: string): GrepLine | undefined {
27
+ const groups = GREP_LINE_PATTERN.exec(line)?.groups;
28
+ if (groups === undefined) return undefined;
29
+ const path = groups.path ?? "";
30
+ const number = Number(groups.line ?? "");
31
+ const text = groups.text ?? "";
32
+ if (text === UNREADABLE_TEXT) return { kind: "unreadable", path, line: number };
33
+ // The notice is decided after the unreadable marker and outranks a match whose
34
+ // own line happens to be one bracketed block.
35
+ if (NOTICE_LINE.test(line)) return undefined;
36
+ if (groups.sep === ":") return { kind: "match", path, line: number, text };
37
+ return { kind: "context", path, line: number, text };
38
+ }
39
+
40
+ export function parseGrepOutput(output: string): GrepLine[] {
41
+ return output === "" ? [] : output.split("\n").map((line) => parseGrepLine(line));
42
+ }
@@ -0,0 +1,109 @@
1
+ import { highlightCode, type ThemeColor } from "@earendil-works/pi-coding-agent";
2
+
3
+ export type HighlightFn = (code: string, lang: string) => string[];
4
+
5
+ // One batched pass over a huge blob is the slowest step of a render, so past
6
+ // this many lines the tail is painted with the caller's plain fallback.
7
+ export const HIGHLIGHT_LINE_BUDGET = 400;
8
+
9
+ const CACHE_LIMIT = 64;
10
+ // `highlightCode` paints through the module-global theme and this cache is keyed
11
+ // by language and code only, so the cached lines go stale as soon as the theme
12
+ // changes: `syncHighlightTheme` is what drops them, and every renderer calls it
13
+ // with the theme it is about to paint with.
14
+ const cache = new Map<string, string[]>();
15
+
16
+ // The tokens `highlightCode` paints with. Reading their sequences is the only
17
+ // observable the theme offers that changes when the user switches themes.
18
+ const SYNC_TOKENS = [
19
+ "syntaxKeyword",
20
+ "syntaxString",
21
+ "syntaxNumber",
22
+ "syntaxComment",
23
+ "syntaxFunction",
24
+ "syntaxType",
25
+ "syntaxVariable",
26
+ "syntaxPunctuation",
27
+ "text",
28
+ "muted",
29
+ ] as const satisfies readonly ThemeColor[];
30
+
31
+ let syncedTheme: unknown;
32
+ let syncedSignature: string | undefined;
33
+ let syncedThemeSet = false;
34
+
35
+ // The theme a renderer receives is a proxy of stable identity, so comparing
36
+ // references would never invalidate: the signature is the concatenation of the
37
+ // sequences the highlighter paints with, which is what actually changes. It stays
38
+ // a value comparison on purpose; a theme whose colors did not move leaves the
39
+ // cache alone, which is every frame of a normal session.
40
+ // Themes without `getFgAnsi` (the fakes in tests) and themes that throw before
41
+ // initialization fall back to the identity comparison the module started with.
42
+ function themeSignature(theme: unknown): string | undefined {
43
+ if (typeof theme !== "object" || theme === null) return undefined;
44
+ const getFgAnsi = (theme as { getFgAnsi?: unknown }).getFgAnsi;
45
+ if (typeof getFgAnsi !== "function") return undefined;
46
+ const read = getFgAnsi as (this: unknown, color: ThemeColor) => unknown;
47
+ try {
48
+ return SYNC_TOKENS.map((token) => String(read.call(theme, token))).join("\u0000");
49
+ } catch {
50
+ return undefined;
51
+ }
52
+ }
53
+
54
+ // The first call only registers the theme; a later call with a different value
55
+ // means the module-global theme changed, and every cached line is stale.
56
+ export function syncHighlightTheme(theme: unknown): void {
57
+ const signature = themeSignature(theme);
58
+ if (syncedThemeSet) {
59
+ const unchanged =
60
+ signature !== undefined ? signature === syncedSignature : theme === syncedTheme;
61
+ if (unchanged) return;
62
+ cache.clear();
63
+ }
64
+ syncedTheme = theme;
65
+ syncedSignature = signature;
66
+ syncedThemeSet = true;
67
+ }
68
+
69
+ export const highlight: HighlightFn = (code, lang) => {
70
+ const key = `${lang}\u0000${code}`;
71
+ const cached = cache.get(key);
72
+ if (cached !== undefined) return cached;
73
+ const lines = highlightCode(code, lang);
74
+ if (cache.size >= CACHE_LIMIT) {
75
+ // FIFO: the oldest insertion goes, hits do not refresh order.
76
+ const oldest = cache.keys().next();
77
+ if (!oldest.done) cache.delete(oldest.value);
78
+ }
79
+ cache.set(key, lines);
80
+ return lines;
81
+ };
82
+
83
+ export function clearHighlightCache(): void {
84
+ cache.clear();
85
+ }
86
+
87
+ // `highlightFn` is injected by every caller: in tests because `highlightCode`
88
+ // would read the module-global theme and throw before any theme is initialized,
89
+ // and in production because the resolver passes the cached `highlight`.
90
+ export function highlightLines(
91
+ code: string,
92
+ lang: string,
93
+ fallback: (line: string) => string,
94
+ highlightFn: HighlightFn,
95
+ ): string[] {
96
+ const lines = code.split("\n");
97
+ const headCount = Math.min(lines.length, HIGHLIGHT_LINE_BUDGET);
98
+ const head = lines.slice(0, headCount);
99
+ let highlighted: string[] = [];
100
+ try {
101
+ highlighted = highlightFn(head.join("\n"), lang);
102
+ } catch {
103
+ highlighted = [];
104
+ }
105
+ // `highlighted` is the source of truth only where it has a line for the same
106
+ // index; anything shorter falls back so the result always matches the input.
107
+ const headLines = head.map((line, index) => highlighted[index] ?? fallback(line));
108
+ return [...headLines, ...lines.slice(headCount).map(fallback)];
109
+ }