pi-syntax-highlight 0.1.1 → 0.2.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.
- package/CHANGELOG.md +73 -58
- package/DEVELOPER.md +61 -34
- package/LICENSE +21 -0
- package/README.md +19 -18
- package/extensions/index.ts +5 -10
- package/package.json +5 -4
- package/src/detect-language.ts +205 -8
- package/src/emphasis.ts +82 -1
- package/src/highlight.ts +25 -9
- package/src/lru-cache.ts +30 -0
- package/src/markdown-fence-lang.ts +20 -87
- package/src/render-bash.ts +29 -2
- package/src/render-edit.ts +0 -6
- package/src/render-generic.ts +10 -21
- package/src/render-read.ts +8 -149
- package/src/render-utils.ts +8 -17
- package/src/resolve-renderers.ts +181 -240
- package/src/tokens.ts +1 -34
- package/assets/icon.png +0 -0
- package/assets/icon.svg +0 -36
- package/src/active-theme.ts +0 -26
- package/src/markdown-style.ts +0 -134
- package/src/markdown-theme.ts +0 -53
- package/src/render-path.ts +0 -54
- package/src/render-write.ts +0 -81
- package/test/unit/active-theme.test.ts +0 -38
- package/test/unit/bash-preview.test.ts +0 -57
- package/test/unit/detect-language.test.ts +0 -124
- package/test/unit/emphasis.test.ts +0 -113
- package/test/unit/extensions-index.test.ts +0 -116
- package/test/unit/grep-parse.test.ts +0 -125
- package/test/unit/highlight.test.ts +0 -107
- package/test/unit/markdown-fence-lang.test.ts +0 -131
- package/test/unit/markdown-pipeline.test.ts +0 -153
- package/test/unit/markdown-render-parity.test.ts +0 -127
- package/test/unit/markdown-style.test.ts +0 -152
- package/test/unit/markdown-theme.test.ts +0 -86
- package/test/unit/render-bash.test.ts +0 -194
- package/test/unit/render-edit.test.ts +0 -214
- package/test/unit/render-find.test.ts +0 -139
- package/test/unit/render-generic.test.ts +0 -152
- package/test/unit/render-grep.test.ts +0 -338
- package/test/unit/render-ls.test.ts +0 -132
- package/test/unit/render-read.test.ts +0 -375
- package/test/unit/render-utils.test.ts +0 -324
- package/test/unit/render-write.test.ts +0 -143
- package/test/unit/resolve-renderers.test.ts +0 -474
- package/test/unit/style-path.test.ts +0 -74
- package/tsconfig.json +0 -15
package/src/detect-language.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
// Language detection for markdown fences that arrive without an info string
|
|
1
|
+
// Language detection for markdown fences that arrive without an info string, and
|
|
2
|
+
// for the body of a tool result (`detectOutputLanguage`, below).
|
|
2
3
|
//
|
|
3
4
|
// A fence body is a short snippet, so every heuristic here is a regular
|
|
4
5
|
// expression over the raw text: parsing would cost more than the wrong color it
|
|
@@ -22,6 +23,22 @@ const DIFF_HEADER = /^(diff --git|--- |\+\+\+ |@@ )/;
|
|
|
22
23
|
|
|
23
24
|
const MARKUP_DECLARATIONS = ["<?xml", "<!DOCTYPE"];
|
|
24
25
|
|
|
26
|
+
// A fragment has no declaration to lean on, so it needs a lowercase tag from the
|
|
27
|
+
// HTML vocabulary (or a comment) first and a closing tag somewhere. `className=`
|
|
28
|
+
// and `={` are JSX, and an unknown tag is XML or a generic, which stay flat.
|
|
29
|
+
const HTML_FRAGMENT_START = new RegExp(
|
|
30
|
+
`^(?:<!--|<(?:${[
|
|
31
|
+
"div", "span", "p", "a", "ul", "ol", "li", "table", "thead", "tbody", "tr", "td", "th",
|
|
32
|
+
"h1", "h2", "h3", "h4", "h5", "h6", "head", "body", "section", "article", "nav", "header",
|
|
33
|
+
"footer", "main", "form", "button", "label", "select", "option", "textarea", "pre", "code",
|
|
34
|
+
"script", "style", "strong", "em", "b", "i",
|
|
35
|
+
].join("|")})[\\s>/])`,
|
|
36
|
+
);
|
|
37
|
+
const HTML_CLOSING_TAG = /<\/[a-z][a-z0-9]*>/;
|
|
38
|
+
const JSX_ONLY = /className=|=\{/;
|
|
39
|
+
|
|
40
|
+
const PHP_OPENER = "<?php";
|
|
41
|
+
|
|
25
42
|
const SHEBANG = /^#!/;
|
|
26
43
|
|
|
27
44
|
// Every interpreter here is painted by the one shell grammar, which highlight.js
|
|
@@ -42,8 +59,20 @@ const CSS_DECLARATION = /^\s*[-a-zA-Z][-\w]*\s*:\s*[^;{}\n]+;$/gm;
|
|
|
42
59
|
const CSS_FOREIGN_SIGNAL =
|
|
43
60
|
/(?:=>|\bfunction\s|\bdef\s|\bconst\s|\binterface\s|\btype[ \t]+\w+[ \t]*=|\bexport\s|\bimport\s)/;
|
|
44
61
|
|
|
62
|
+
const DOCKER_FROM = /^FROM[ \t]+\S/;
|
|
63
|
+
const DOCKER_INSTRUCTION = /^(?:RUN|COPY|ADD|CMD|ENTRYPOINT|WORKDIR|ENV|EXPOSE|ARG|USER|LABEL)[ \t]/m;
|
|
64
|
+
|
|
65
|
+
// A rule needs at least one dependency: `default:` and `else:` followed by an
|
|
66
|
+
// indented line are C and shell, and `make: *** [all] Error 1` has no tab recipe.
|
|
67
|
+
const MAKE_PHONY = /^\.PHONY:/m;
|
|
68
|
+
const MAKE_RULE = /^(?:[\w%-]*\.)*(?![A-Z])[\w%-]+[ \t]*:(?![=:])[ \t]+[\w.%$()/-][ \t\w.%$()/-]*\n\t\S/m;
|
|
69
|
+
|
|
70
|
+
const C_INCLUDE = /^[ \t]*#include[ \t]*[<"]/m;
|
|
71
|
+
const CPP_SIGNAL = /std::|\btemplate[ \t]*<|\bnamespace\b|\bclass[ \t]+\w+|\bcout\b/;
|
|
72
|
+
|
|
45
73
|
const SQL_STATEMENT_KEYWORD = /^(?:SELECT|INSERT|UPDATE|DELETE|CREATE|ALTER|DROP|WITH)\b/i;
|
|
46
74
|
const SQL_CLAUSE_KEYWORD = /\b(?:FROM|WHERE|VALUES|TABLE)\b/i;
|
|
75
|
+
const SQL_CLAUSE_KEYWORD_UPPER = /\b(?:FROM|WHERE|VALUES|TABLE)\b/g;
|
|
47
76
|
|
|
48
77
|
// Anchored to the start of a line and closed by the colon that ends a Python
|
|
49
78
|
// header: `public class Foo {` and `def f()` inside a patch are those other
|
|
@@ -60,6 +89,37 @@ const PYTHON_SIGNALS: readonly RegExp[] = [
|
|
|
60
89
|
// before a top-level `key:` line count as one of the two indented blocks.
|
|
61
90
|
const PYTHON_BLOCK_LINE = /^[ \t]+\S.*:[ \t]*$/gm;
|
|
62
91
|
|
|
92
|
+
// Two of these: `puts`, `end` and `def` without a colon each show up elsewhere.
|
|
93
|
+
// `class A < B` needs the spaces, so a generic `class Box<T>` is not one.
|
|
94
|
+
const RUBY_SIGNALS: readonly RegExp[] = [
|
|
95
|
+
/^[ \t]*def[ \t]+[\w.?!=]+(?:\(.*\))?[ \t]*$/m,
|
|
96
|
+
/^[ \t]*end[ \t]*$/m,
|
|
97
|
+
/^[ \t]*require(?:_relative)?[ \t]+['"]/m,
|
|
98
|
+
/\bdo[ \t]*\|[^|\n]*\|/,
|
|
99
|
+
/^[ \t]*class[ \t]+[A-Z]\w*[ \t]+<[ \t]+[A-Z]/m,
|
|
100
|
+
/^[ \t]*puts[ \t]+\S/m,
|
|
101
|
+
/^[ \t]*attr_(?:reader|writer|accessor)\b/m,
|
|
102
|
+
];
|
|
103
|
+
|
|
104
|
+
// Verb-Noun with a verb from the approved list: `Content-Type` and the like are
|
|
105
|
+
// header names, and a header such as `Set-Cookie:` is excluded by the colon.
|
|
106
|
+
const POWERSHELL_SIGNALS: readonly RegExp[] = [
|
|
107
|
+
/\b(?:Get|Set|New|Remove|Write|Invoke|Import|Export|Select|Where|ForEach|Test|Start|Stop|Add|Out|Format|Read|Install|Copy|Move|Clear)-[A-Z]\w+\b(?!:)/,
|
|
108
|
+
/\$env:\w+/i,
|
|
109
|
+
/\bparam[ \t]*\(/i,
|
|
110
|
+
/\[CmdletBinding\(/i,
|
|
111
|
+
/[ \t]-(?:eq|ne|like|match|notmatch|gt|lt)\b/i,
|
|
112
|
+
/\|[ \t]*(?:Where|Select|ForEach)-Object\b/,
|
|
113
|
+
];
|
|
114
|
+
|
|
115
|
+
const JSX_SIGNALS: readonly RegExp[] = [
|
|
116
|
+
/<\/[A-Z]\w*>/,
|
|
117
|
+
/<[A-Z]\w*[ \t]+[\w-]+=/,
|
|
118
|
+
/<[A-Z]\w*[ \t]*\/>/,
|
|
119
|
+
/\bclassName=/,
|
|
120
|
+
/\breturn[ \t]*\(\s*</,
|
|
121
|
+
];
|
|
122
|
+
|
|
63
123
|
const TYPESCRIPT_SIGNALS: readonly RegExp[] = [
|
|
64
124
|
/\binterface\s+\w+/,
|
|
65
125
|
/^[ \t]*(?:export[ \t]+)?type[ \t]+\w+[ \t]*=/m,
|
|
@@ -101,10 +161,21 @@ const STATEMENT_TERMINATOR = /[;{}][ \t]*$/m;
|
|
|
101
161
|
// annotation and `{ a: 1 }` is code, both ruled out by the pattern below.
|
|
102
162
|
const YAML_PLAIN_KEY = /^[A-Za-z_][\w.-]*:[ \t]*\S/gm;
|
|
103
163
|
const YAML_FOREIGN_SIGNAL = /[{(=]|\b(?:const|let|function)\s/;
|
|
164
|
+
// A key indented under another one, which is what separates a document with two
|
|
165
|
+
// top-level keys from two log lines that happen to carry a colon.
|
|
166
|
+
const YAML_INDENTED_KEY = /^[ \t]+[A-Za-z_][\w.-]*:/gm;
|
|
167
|
+
// A document key starts in lowercase and may carry no value at all, which is
|
|
168
|
+
// what a parent of a nested block looks like (`metadata:`). HTTP header names
|
|
169
|
+
// are Title-Case and are the reason for the case rule: `curl -I` output is not
|
|
170
|
+
// a document, but three of its headers satisfy any count that ignores case.
|
|
171
|
+
const YAML_DOCUMENT_KEY = /^[a-z_][\w.-]*:[ \t]*(?:\S.*)?$/gm;
|
|
104
172
|
|
|
105
173
|
const TOML_SECTION = /^\[[\w.-]+\][ \t]*$/m;
|
|
106
174
|
const TOML_KEY_VALUE = /^\w[\w.-]*[ \t]*=/gm;
|
|
107
175
|
|
|
176
|
+
const PROPERTIES_LINE = /^[a-z][\w-]*(?:\.[\w-]+)+[ \t]*=.*$/;
|
|
177
|
+
const PROPERTIES_COMMENT = /^[ \t]*[#!]/;
|
|
178
|
+
|
|
108
179
|
const SHELL_COMMAND =
|
|
109
180
|
/^(?:\$[ \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
181
|
const SHELL_PIPELINE = /\|[ \t]*[\w./-]+/gm;
|
|
@@ -113,12 +184,50 @@ const SHELL_PIPELINE = /\|[ \t]*[\w./-]+/gm;
|
|
|
113
184
|
const TABLE_ROW = /^[ \t]*\|/;
|
|
114
185
|
const TABLE_RULE = "|---";
|
|
115
186
|
|
|
187
|
+
// Languages worth detecting in shell output. A wrong label paints the body with
|
|
188
|
+
// a grammar that does not fit it, which reads worse than flat text, so a
|
|
189
|
+
// language enters only when it really shows up in a terminal and its detector
|
|
190
|
+
// is strict enough not to fire on a log. Left out for the first reason or the
|
|
191
|
+
// second: java, csharp and kotlin (`public`, `static`, `using`, `fun` are log
|
|
192
|
+
// words), css, scss and less (`key: value;` is also every environment dump),
|
|
193
|
+
// lua, haskell, elixir, cmake, gradle, protobuf, clojure and erlang (no realistic
|
|
194
|
+
// shell-output scenario), and properties (`key=value` is every config dump).
|
|
195
|
+
const OUTPUT_LANGUAGES: ReadonlySet<string> = new Set([
|
|
196
|
+
"yaml", // kubectl, helm, compose
|
|
197
|
+
"toml", // cat pyproject.toml, Cargo.toml
|
|
198
|
+
"sql", // psql -f
|
|
199
|
+
"bash", // environment dumps, transcripts
|
|
200
|
+
"python",
|
|
201
|
+
"javascript",
|
|
202
|
+
"typescript",
|
|
203
|
+
"go",
|
|
204
|
+
"rust", // cat of source files
|
|
205
|
+
"xml",
|
|
206
|
+
"html", // curl
|
|
207
|
+
"dockerfile",
|
|
208
|
+
"makefile",
|
|
209
|
+
"c",
|
|
210
|
+
"cpp",
|
|
211
|
+
"php",
|
|
212
|
+
"ruby",
|
|
213
|
+
"powershell",
|
|
214
|
+
"jsx",
|
|
215
|
+
"tsx",
|
|
216
|
+
]);
|
|
217
|
+
|
|
116
218
|
type Detector = (text: string) => string | undefined;
|
|
117
219
|
|
|
118
220
|
function detectJson(text: string): string | undefined {
|
|
119
221
|
return isJsonDocument(text) ? "json" : undefined;
|
|
120
222
|
}
|
|
121
223
|
|
|
224
|
+
// Two or more lines that each parse as an object or array. A single document
|
|
225
|
+
// never reaches this: `detectJson` answers first.
|
|
226
|
+
function detectNdjson(text: string): string | undefined {
|
|
227
|
+
const lines = nonBlankLines(text);
|
|
228
|
+
return lines.length >= 2 && lines.every((line) => isJsonDocument(line)) ? "json" : undefined;
|
|
229
|
+
}
|
|
230
|
+
|
|
122
231
|
function detectDiff(text: string): string | undefined {
|
|
123
232
|
return DIFF_HEADER.test(firstNonBlankLine(text)) ? "diff" : undefined;
|
|
124
233
|
}
|
|
@@ -134,8 +243,32 @@ function detectShebang(text: string): string | undefined {
|
|
|
134
243
|
|
|
135
244
|
function detectMarkup(text: string): string | undefined {
|
|
136
245
|
const first = firstNonBlankLine(text);
|
|
137
|
-
|
|
138
|
-
|
|
246
|
+
return declaredMarkup(first) ?? (first.includes("<html") ? "html" : undefined);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function declaredMarkup(first: string): "php" | "xml" | undefined {
|
|
250
|
+
if (first.includes(PHP_OPENER)) return "php";
|
|
251
|
+
return MARKUP_DECLARATIONS.some((declaration) => first.includes(declaration)) ? "xml" : undefined;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function detectHtmlFragment(text: string): string | undefined {
|
|
255
|
+
if (!HTML_FRAGMENT_START.test(firstNonBlankLine(text).trimStart())) return undefined;
|
|
256
|
+
return HTML_CLOSING_TAG.test(text) && !JSX_ONLY.test(text) ? "html" : undefined;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function detectDockerfile(text: string): string | undefined {
|
|
260
|
+
const instructions = nonBlankLines(text).filter((line) => !line.startsWith("#"));
|
|
261
|
+
if (!DOCKER_FROM.test(instructions[0] ?? "")) return undefined;
|
|
262
|
+
return DOCKER_INSTRUCTION.test(instructions.slice(1).join("\n")) ? "dockerfile" : undefined;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function detectMakefile(text: string): string | undefined {
|
|
266
|
+
return MAKE_PHONY.test(text) || MAKE_RULE.test(text) ? "makefile" : undefined;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function detectC(text: string): string | undefined {
|
|
270
|
+
if (!C_INCLUDE.test(text)) return undefined;
|
|
271
|
+
return CPP_SIGNAL.test(text) ? "cpp" : "c";
|
|
139
272
|
}
|
|
140
273
|
|
|
141
274
|
function detectCss(text: string): string | undefined {
|
|
@@ -150,17 +283,35 @@ function detectSql(text: string): string | undefined {
|
|
|
150
283
|
return SQL_CLAUSE_KEYWORD.test(text) ? "sql" : undefined;
|
|
151
284
|
}
|
|
152
285
|
|
|
286
|
+
// A statement terminator or a second clause in uppercase is what makes the block
|
|
287
|
+
// a query: prose that starts with "Update" and mentions a table and a form of
|
|
288
|
+
// "from" satisfies the fence detector, and its lowercase is what keeps it out of
|
|
289
|
+
// here. The cost is a lowercase query with no terminator, which stays flat.
|
|
290
|
+
function sqlSurvivesOutput(text: string): boolean {
|
|
291
|
+
return text.includes(";") || countMatches(text, SQL_CLAUSE_KEYWORD_UPPER) >= 2;
|
|
292
|
+
}
|
|
293
|
+
|
|
153
294
|
function detectPython(text: string): string | undefined {
|
|
154
295
|
if (countSignals(text, PYTHON_SIGNALS) > 0) return "python";
|
|
155
296
|
return countMatches(text, PYTHON_BLOCK_LINE) >= 2 ? "python" : undefined;
|
|
156
297
|
}
|
|
157
298
|
|
|
299
|
+
function detectRuby(text: string): string | undefined {
|
|
300
|
+
return countSignals(text, RUBY_SIGNALS) >= 2 ? "ruby" : undefined;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function detectPowerShell(text: string): string | undefined {
|
|
304
|
+
return countSignals(text, POWERSHELL_SIGNALS) >= 2 ? "powershell" : undefined;
|
|
305
|
+
}
|
|
306
|
+
|
|
158
307
|
function detectTypeScript(text: string): string | undefined {
|
|
159
|
-
|
|
308
|
+
if (!hasTypeScriptSignal(text)) return undefined;
|
|
309
|
+
return hasJsxSignal(text) ? "tsx" : "typescript";
|
|
160
310
|
}
|
|
161
311
|
|
|
162
312
|
function detectJavaScript(text: string): string | undefined {
|
|
163
|
-
|
|
313
|
+
if (countSignals(text, JAVASCRIPT_SIGNALS) === 0) return undefined;
|
|
314
|
+
return hasJsxSignal(text) ? "jsx" : "javascript";
|
|
164
315
|
}
|
|
165
316
|
|
|
166
317
|
function detectGo(text: string): string | undefined {
|
|
@@ -191,11 +342,28 @@ function detectYamlKeys(text: string): string | undefined {
|
|
|
191
342
|
return YAML_FOREIGN_SIGNAL.test(text) ? undefined : "yaml";
|
|
192
343
|
}
|
|
193
344
|
|
|
345
|
+
// Two bare `key: value` lines are as much git log and HTTP headers as they are a
|
|
346
|
+
// document, so the guard asks for a third key or for structure.
|
|
347
|
+
function yamlSurvivesOutput(text: string): boolean {
|
|
348
|
+
const keys = countMatches(text, YAML_DOCUMENT_KEY);
|
|
349
|
+
if (keys >= 3) return true;
|
|
350
|
+
if (keys < 2) return false;
|
|
351
|
+
return countMatches(text, YAML_INDENTED_KEY) > 0 || countMatches(text, YAML_ITEM) > 0;
|
|
352
|
+
}
|
|
353
|
+
|
|
194
354
|
function detectToml(text: string): string | undefined {
|
|
195
355
|
if (!TOML_SECTION.test(text)) return undefined;
|
|
196
356
|
return countMatches(text, TOML_KEY_VALUE) >= 2 ? "toml" : undefined;
|
|
197
357
|
}
|
|
198
358
|
|
|
359
|
+
// Every non-comment line is a dotted lowercase pair: one stray line, a `$`
|
|
360
|
+
// expansion or a statement terminator means a shell script or a program.
|
|
361
|
+
function detectProperties(text: string): string | undefined {
|
|
362
|
+
if (text.includes("$") || STATEMENT_TERMINATOR.test(text)) return undefined;
|
|
363
|
+
const lines = nonBlankLines(text).filter((line) => !PROPERTIES_COMMENT.test(line));
|
|
364
|
+
return lines.length >= 2 && lines.every((line) => PROPERTIES_LINE.test(line)) ? "properties" : undefined;
|
|
365
|
+
}
|
|
366
|
+
|
|
199
367
|
function detectShell(text: string): string | undefined {
|
|
200
368
|
if (text.includes(TABLE_RULE)) return undefined;
|
|
201
369
|
const body = nonTableLines(text);
|
|
@@ -224,20 +392,28 @@ function nonTableLines(text: string): string {
|
|
|
224
392
|
// assignments is the YAML document.
|
|
225
393
|
const DETECTORS: readonly Detector[] = [
|
|
226
394
|
detectJson,
|
|
395
|
+
detectNdjson,
|
|
227
396
|
detectDiff,
|
|
228
397
|
detectShebang,
|
|
229
398
|
detectMarkup,
|
|
399
|
+
detectHtmlFragment,
|
|
400
|
+
detectDockerfile,
|
|
401
|
+
detectMakefile,
|
|
402
|
+
detectC,
|
|
230
403
|
detectCss,
|
|
231
404
|
detectSql,
|
|
232
405
|
detectPython,
|
|
406
|
+
detectRuby,
|
|
233
407
|
detectGo,
|
|
234
408
|
detectRust,
|
|
235
409
|
detectYamlKeys,
|
|
410
|
+
detectPowerShell,
|
|
236
411
|
detectTypeScript,
|
|
237
412
|
detectJavaScript,
|
|
238
413
|
detectManagedLanguage,
|
|
239
414
|
detectYaml,
|
|
240
415
|
detectToml,
|
|
416
|
+
detectProperties,
|
|
241
417
|
detectShell,
|
|
242
418
|
];
|
|
243
419
|
|
|
@@ -253,6 +429,24 @@ export function detectLanguage(text: string): string | undefined {
|
|
|
253
429
|
return undefined;
|
|
254
430
|
}
|
|
255
431
|
|
|
432
|
+
// The language of a tool-output body, or undefined when the body should keep its
|
|
433
|
+
// per-line styling. Shell output is log-shaped far more often than code-shaped,
|
|
434
|
+
// so this is `detectLanguage` plus two guard rails: the allowlist above, and the
|
|
435
|
+
// higher thresholds the two loosest detectors need to survive a log.
|
|
436
|
+
export function detectOutputLanguage(text: string): string | undefined {
|
|
437
|
+
const language = detectLanguage(text);
|
|
438
|
+
if (language === undefined) return undefined;
|
|
439
|
+
// The caller checks these two shapes over the whole body, so a sample that
|
|
440
|
+
// parses as JSON is a prefix of it, not the document; NDJSON is the exception
|
|
441
|
+
// (the only case where "json" gets past this line, so it needs no allowlist entry).
|
|
442
|
+
if (language === "json") return isJsonDocument(text) ? undefined : language;
|
|
443
|
+
if (language === "diff") return undefined;
|
|
444
|
+
if (!OUTPUT_LANGUAGES.has(language)) return undefined;
|
|
445
|
+
if (language === "yaml") return yamlSurvivesOutput(text) ? language : undefined;
|
|
446
|
+
if (language === "sql") return sqlSurvivesOutput(text) ? language : undefined;
|
|
447
|
+
return language;
|
|
448
|
+
}
|
|
449
|
+
|
|
256
450
|
function nonBlankLines(text: string): string[] {
|
|
257
451
|
return text.split("\n").filter((line) => line.trim() !== "");
|
|
258
452
|
}
|
|
@@ -274,9 +468,8 @@ function isJsonDocument(text: string): boolean {
|
|
|
274
468
|
function hasStrongSignal(text: string, lines: readonly string[]): boolean {
|
|
275
469
|
if (isJsonDocument(text)) return true;
|
|
276
470
|
const first = lines[0] ?? "";
|
|
277
|
-
if (first.startsWith("#!")) return true;
|
|
278
|
-
|
|
279
|
-
return MARKUP_DECLARATIONS.some((declaration) => first.includes(declaration));
|
|
471
|
+
if (first.startsWith("#!") || first.startsWith("diff --git")) return true;
|
|
472
|
+
return declaredMarkup(first) !== undefined;
|
|
280
473
|
}
|
|
281
474
|
|
|
282
475
|
// Every pattern passed here carries the global flag, so `match` returns all the
|
|
@@ -292,3 +485,7 @@ function countSignals(text: string, signals: readonly RegExp[]): number {
|
|
|
292
485
|
function hasTypeScriptSignal(text: string): boolean {
|
|
293
486
|
return countSignals(text, TYPESCRIPT_SIGNALS) > 0;
|
|
294
487
|
}
|
|
488
|
+
|
|
489
|
+
function hasJsxSignal(text: string): boolean {
|
|
490
|
+
return countSignals(text, JSX_SIGNALS) > 0;
|
|
491
|
+
}
|
package/src/emphasis.ts
CHANGED
|
@@ -23,6 +23,11 @@ const ANSI_SEQUENCE = /\x1b\[[0-9;]*m|\x1b\][^\x07]*\x07/y;
|
|
|
23
23
|
|
|
24
24
|
const ESCAPE = "\x1b";
|
|
25
25
|
|
|
26
|
+
// Past these sizes the backtracking matcher is not worth the risk on a render
|
|
27
|
+
// path; the cost is a line without emphasis, never a missing line.
|
|
28
|
+
const MAX_REGEX_LENGTH = 256;
|
|
29
|
+
const GREP_MAX_LINE_LENGTH = 500;
|
|
30
|
+
|
|
26
31
|
function escapeLiteral(pattern: string): string {
|
|
27
32
|
return pattern.replace(REGEXP_METACHARACTERS, "\\$&");
|
|
28
33
|
}
|
|
@@ -82,10 +87,86 @@ function clipRanges(ranges: Range[], limit: number): Range[] {
|
|
|
82
87
|
return mergeRanges(clipped);
|
|
83
88
|
}
|
|
84
89
|
|
|
90
|
+
interface GroupFrame {
|
|
91
|
+
hasQuantifier: boolean;
|
|
92
|
+
hasAlternation: boolean;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Index just past the `?` prefix of a group (`?:`, `?=`, `?!`, `?<=`, `?<!`,
|
|
96
|
+
// `?<name>`), so that prefix is not mistaken for a quantifier.
|
|
97
|
+
function skipGroupPrefix(source: string, index: number): number {
|
|
98
|
+
if (source[index] !== "?") return index;
|
|
99
|
+
const next = source[index + 1];
|
|
100
|
+
if (next === "<" && source[index + 2] !== "=" && source[index + 2] !== "!") {
|
|
101
|
+
const close = source.indexOf(">", index);
|
|
102
|
+
return close === -1 ? source.length : close + 1;
|
|
103
|
+
}
|
|
104
|
+
return next === "<" ? index + 3 : index + 2;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Index past a two-character escape, or -1 for a backreference (`\1`-`\9`,
|
|
108
|
+
// `\k<name>`), which no static check can bound.
|
|
109
|
+
function skipEscape(source: string, index: number): number {
|
|
110
|
+
const next = source[index + 1];
|
|
111
|
+
if ((next >= "1" && next <= "9") || (next === "k" && source[index + 2] === "<")) return -1;
|
|
112
|
+
return index + 2;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Index just past the `]` closing the class that opens at `index`.
|
|
116
|
+
function skipCharClass(source: string, index: number): number {
|
|
117
|
+
let cursor = index + 1;
|
|
118
|
+
while (cursor < source.length && source[cursor] !== "]") cursor += source[cursor] === "\\" ? 2 : 1;
|
|
119
|
+
return cursor + 1;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Pops the group closed at `index`: false when it is repeated while its body can
|
|
123
|
+
// repeat or branch, otherwise the parent frame inherits what the group held.
|
|
124
|
+
function closeGroup(stack: GroupFrame[], source: string, index: number): boolean {
|
|
125
|
+
const frame = stack.pop() as GroupFrame;
|
|
126
|
+
const parent = stack[stack.length - 1];
|
|
127
|
+
const after = source[index + 1];
|
|
128
|
+
const repeated = after === "+" || after === "*" || after === "{";
|
|
129
|
+
if (repeated && (frame.hasQuantifier || frame.hasAlternation)) return false;
|
|
130
|
+
parent.hasQuantifier ||= frame.hasQuantifier || repeated || after === "?";
|
|
131
|
+
parent.hasAlternation ||= frame.hasAlternation;
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Catastrophic backtracking needs a repeated group whose body can repeat or
|
|
136
|
+
// branch, and a backreference defeats any static check: both are rejected by a
|
|
137
|
+
// scan that follows escapes, character classes and the group nesting. A lone
|
|
138
|
+
// `?` after a group repeats nothing, so `(https?://)?` stays allowed.
|
|
139
|
+
function isBacktrackingSafe(source: string): boolean {
|
|
140
|
+
const stack: GroupFrame[] = [{ hasQuantifier: false, hasAlternation: false }];
|
|
141
|
+
let index = 0;
|
|
142
|
+
while (index < source.length) {
|
|
143
|
+
const char = source[index];
|
|
144
|
+
const top = stack[stack.length - 1];
|
|
145
|
+
if (char === "\\") {
|
|
146
|
+
index = skipEscape(source, index);
|
|
147
|
+
if (index === -1) return false;
|
|
148
|
+
} else if (char === "[") {
|
|
149
|
+
index = skipCharClass(source, index);
|
|
150
|
+
} else if (char === "(") {
|
|
151
|
+
stack.push({ hasQuantifier: false, hasAlternation: false });
|
|
152
|
+
index = skipGroupPrefix(source, index + 1);
|
|
153
|
+
} else if (char === ")" && stack.length > 1) {
|
|
154
|
+
if (!closeGroup(stack, source, index)) return false;
|
|
155
|
+
index += 1;
|
|
156
|
+
} else {
|
|
157
|
+
if (char === "|") top.hasAlternation = true;
|
|
158
|
+
else if (char === "+" || char === "*" || char === "?" || char === "{") top.hasQuantifier = true;
|
|
159
|
+
index += 1;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
|
|
85
165
|
export function findMatchRanges(text: string, pattern: string, options: MatchOptions): Range[] {
|
|
86
166
|
// An empty pattern matches everywhere and nowhere at once, and grep never
|
|
87
167
|
// sends it; treating it as "no match" keeps the line plain.
|
|
88
|
-
if (pattern === "") return [];
|
|
168
|
+
if (pattern === "" || text.length > GREP_MAX_LINE_LENGTH) return [];
|
|
169
|
+
if (!options.literal && (pattern.length > MAX_REGEX_LENGTH || !isBacktrackingSafe(pattern))) return [];
|
|
89
170
|
const source = options.literal ? escapeLiteral(pattern) : pattern;
|
|
90
171
|
let regex: RegExp;
|
|
91
172
|
try {
|
package/src/highlight.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { highlightCode, type ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
1
|
+
import { getMarkdownTheme, highlightCode, type ThemeColor } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { LruCache } from "./lru-cache.ts";
|
|
2
3
|
|
|
3
4
|
export type HighlightFn = (code: string, lang: string) => string[];
|
|
4
5
|
|
|
5
|
-
// One batched pass over a huge blob is the slowest step of a render
|
|
6
|
+
// One batched pass over a huge blob is the slowest step of a render. Only the
|
|
7
|
+
// shell, whose output is unbounded, passes it as a `highlightLines` budget: past
|
|
6
8
|
// this many lines the tail is painted with the caller's plain fallback.
|
|
7
9
|
export const HIGHLIGHT_LINE_BUDGET = 400;
|
|
8
10
|
|
|
@@ -11,7 +13,7 @@ const CACHE_LIMIT = 64;
|
|
|
11
13
|
// by language and code only, so the cached lines go stale as soon as the theme
|
|
12
14
|
// changes: `syncHighlightTheme` is what drops them, and every renderer calls it
|
|
13
15
|
// with the theme it is about to paint with.
|
|
14
|
-
const cache = new
|
|
16
|
+
const cache = new LruCache<string, string[]>(CACHE_LIMIT);
|
|
15
17
|
|
|
16
18
|
// The tokens `highlightCode` paints with. Reading their sequences is the only
|
|
17
19
|
// observable the theme offers that changes when the user switches themes.
|
|
@@ -24,6 +26,10 @@ const SYNC_TOKENS = [
|
|
|
24
26
|
"syntaxType",
|
|
25
27
|
"syntaxVariable",
|
|
26
28
|
"syntaxPunctuation",
|
|
29
|
+
"syntaxOperator",
|
|
30
|
+
"toolDiffAdded",
|
|
31
|
+
"toolDiffRemoved",
|
|
32
|
+
"mdCodeBlock",
|
|
27
33
|
"text",
|
|
28
34
|
"muted",
|
|
29
35
|
] as const satisfies readonly ThemeColor[];
|
|
@@ -66,16 +72,25 @@ export function syncHighlightTheme(theme: unknown): void {
|
|
|
66
72
|
syncedThemeSet = true;
|
|
67
73
|
}
|
|
68
74
|
|
|
75
|
+
// `highlightCode` paints with the code-block color when it has no grammar for the
|
|
76
|
+
// language, which is also what a grammar that has not loaded yet looks like.
|
|
77
|
+
// Such a result carries no syntax, so `highlight` returns it empty and uncached.
|
|
78
|
+
function isCodeBlockPaint(code: string, lines: readonly string[]): boolean {
|
|
79
|
+
try {
|
|
80
|
+
const input = code.split("\n");
|
|
81
|
+
const { codeBlock } = getMarkdownTheme();
|
|
82
|
+
return lines.length === input.length && input.every((line, index) => lines[index] === codeBlock(line));
|
|
83
|
+
} catch {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
69
88
|
export const highlight: HighlightFn = (code, lang) => {
|
|
70
89
|
const key = `${lang}\u0000${code}`;
|
|
71
90
|
const cached = cache.get(key);
|
|
72
91
|
if (cached !== undefined) return cached;
|
|
73
92
|
const lines = highlightCode(code, lang);
|
|
74
|
-
if (
|
|
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
|
-
}
|
|
93
|
+
if (isCodeBlockPaint(code, lines)) return [];
|
|
79
94
|
cache.set(key, lines);
|
|
80
95
|
return lines;
|
|
81
96
|
};
|
|
@@ -92,9 +107,10 @@ export function highlightLines(
|
|
|
92
107
|
lang: string,
|
|
93
108
|
fallback: (line: string) => string,
|
|
94
109
|
highlightFn: HighlightFn,
|
|
110
|
+
budget = Infinity,
|
|
95
111
|
): string[] {
|
|
96
112
|
const lines = code.split("\n");
|
|
97
|
-
const headCount = Math.min(lines.length,
|
|
113
|
+
const headCount = Math.min(lines.length, budget);
|
|
98
114
|
const head = lines.slice(0, headCount);
|
|
99
115
|
let highlighted: string[] = [];
|
|
100
116
|
try {
|
package/src/lru-cache.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export class LruCache<K, V> {
|
|
2
|
+
private readonly entries = new Map<K, V>();
|
|
3
|
+
private readonly limit: number;
|
|
4
|
+
|
|
5
|
+
constructor(limit: number) {
|
|
6
|
+
this.limit = limit;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
get(key: K): V | undefined {
|
|
10
|
+
if (!this.entries.has(key)) return undefined;
|
|
11
|
+
const value = this.entries.get(key) as V;
|
|
12
|
+
this.entries.delete(key);
|
|
13
|
+
this.entries.set(key, value);
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
set(key: K, value: V): void {
|
|
18
|
+
if (this.entries.has(key)) {
|
|
19
|
+
this.entries.delete(key);
|
|
20
|
+
} else if (this.entries.size >= this.limit) {
|
|
21
|
+
const oldest = this.entries.keys().next();
|
|
22
|
+
if (!oldest.done) this.entries.delete(oldest.value);
|
|
23
|
+
}
|
|
24
|
+
this.entries.set(key, value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
clear(): void {
|
|
28
|
+
this.entries.clear();
|
|
29
|
+
}
|
|
30
|
+
}
|