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.
Files changed (49) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +68 -2
  3. package/extensions/index.ts +22 -0
  4. package/package.json +35 -4
  5. package/src/active-theme.ts +26 -0
  6. package/src/detect-language.ts +294 -0
  7. package/src/emphasis.ts +160 -0
  8. package/src/grep-parse.ts +42 -0
  9. package/src/highlight.ts +109 -0
  10. package/src/markdown-fence-lang.ts +180 -0
  11. package/src/markdown-style.ts +134 -0
  12. package/src/markdown-theme.ts +53 -0
  13. package/src/render-bash.ts +180 -0
  14. package/src/render-edit.ts +207 -0
  15. package/src/render-find.ts +64 -0
  16. package/src/render-generic.ts +86 -0
  17. package/src/render-grep.ts +142 -0
  18. package/src/render-ls.ts +66 -0
  19. package/src/render-path.ts +54 -0
  20. package/src/render-read.ts +277 -0
  21. package/src/render-utils.ts +219 -0
  22. package/src/render-write.ts +81 -0
  23. package/src/resolve-renderers.ts +406 -0
  24. package/src/style-path.ts +48 -0
  25. package/src/tokens.ts +114 -0
  26. package/test/unit/active-theme.test.ts +38 -0
  27. package/test/unit/bash-preview.test.ts +57 -0
  28. package/test/unit/detect-language.test.ts +124 -0
  29. package/test/unit/emphasis.test.ts +113 -0
  30. package/test/unit/extensions-index.test.ts +116 -0
  31. package/test/unit/grep-parse.test.ts +125 -0
  32. package/test/unit/highlight.test.ts +107 -0
  33. package/test/unit/markdown-fence-lang.test.ts +131 -0
  34. package/test/unit/markdown-pipeline.test.ts +153 -0
  35. package/test/unit/markdown-render-parity.test.ts +127 -0
  36. package/test/unit/markdown-style.test.ts +152 -0
  37. package/test/unit/markdown-theme.test.ts +86 -0
  38. package/test/unit/render-bash.test.ts +194 -0
  39. package/test/unit/render-edit.test.ts +214 -0
  40. package/test/unit/render-find.test.ts +139 -0
  41. package/test/unit/render-generic.test.ts +152 -0
  42. package/test/unit/render-grep.test.ts +338 -0
  43. package/test/unit/render-ls.test.ts +132 -0
  44. package/test/unit/render-read.test.ts +375 -0
  45. package/test/unit/render-utils.test.ts +324 -0
  46. package/test/unit/render-write.test.ts +143 -0
  47. package/test/unit/resolve-renderers.test.ts +474 -0
  48. package/test/unit/style-path.test.ts +74 -0
  49. package/tsconfig.json +15 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,60 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented in this file. The format
4
+ follows Keep a Changelog, and this project uses semantic versioning.
5
+
6
+ ## [0.1.0] - 2026-10-25
7
+
8
+ ### Added
9
+
10
+ - `read` result renderer: line-number gutter counted from the line the tool
11
+ actually read, body highlighted by file language, continuation and truncation
12
+ notices in the warning color, stock footer and collapse behavior.
13
+ - `read` and `write` call headers: path with a hyperlink, line range, compact
14
+ header for skills and agent resource files, and a highlighted content preview
15
+ for `write` that is capped while the argument streams.
16
+ - `edit` result renderer: diff highlighted by file language, dimmed line numbers,
17
+ changed words in reverse video, error body in the error color. The diff fills
18
+ the body of the call's own box, so the header keeps its success or failure
19
+ background.
20
+ - Markdown styling for assistant messages: headings in the accent color with one
21
+ attribute per level, inline code with its backtick delimiters restored, and
22
+ blockquote text in the accent color. Sequences close the attribute they opened
23
+ and never cross a line; the visible text is unchanged apart from the restored
24
+ delimiters.
25
+ - `diff` dependency for the word-level highlight of an edited line.
26
+
27
+ ### Changed
28
+
29
+ - `read` numbers its body and lifts the tool's continuation notice out of the
30
+ highlighted text.
31
+ - `edit` no longer paints its diff through pi's `renderDiff`, which reads the
32
+ module-global theme and does not highlight code.
33
+
34
+ ### Fixed
35
+
36
+ - The highlight cache now invalidates on a theme change. The theme object is a
37
+ proxy of stable identity, so comparing references never saw the switch; the
38
+ cache is keyed by the color sequences the highlighter actually reads.
39
+ - Tool failures are detected through the render context. The host passes no
40
+ `isError` inside the result, so the previous check was always false and
41
+ `grep`, `find`, `ls` and the generic overlay never painted an error.
42
+
43
+ ## [0.0.1] - 2026-10-24
44
+
45
+ ### Added
46
+
47
+ - `grep` result renderer: styled paths and line numbers, matched lines
48
+ highlighted with the file's language, match emphasized, truncation footer
49
+ preserved.
50
+ - `find` and `ls` result renderers: dimmed directories, basenames colored by
51
+ file family, limit footer preserved.
52
+ - `bash` and `powershell` result renderers: diffs, JSON, paths and errors
53
+ styled, collapsed preview with the expand hint, duration line.
54
+ - Generic result overlay for `codemode`, MCP and unknown tools that have no
55
+ renderer of their own: JSON and diff bodies highlighted, 10-line collapsed
56
+ preview.
57
+ - Markdown fence transformer: unlabeled fences get a detected language
58
+ injected; labeled fences and `mermaid`, `math`, `latex`, `katex` are left
59
+ untouched.
60
+ - `read`, `write` and `edit` are passed through unchanged.
package/README.md CHANGED
@@ -1,3 +1,69 @@
1
- # Temporary Holding Version
1
+ # pi-syntax-highlight
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Pi extension that adds syntax highlighting to tool output and to assistant markdown: tool renderers for `grep`, `find`, `ls`, the shells, `read`, `edit` and `write`, and a markdown pass that colors headings, blockquotes and inline code without changing the text.
4
+
5
+ Scope is limited to rendering: no tool behavior, no settings, no other plugins.
6
+
7
+ ## What it touches
8
+
9
+ | Tool | Change |
10
+ |---|---|
11
+ | `grep` | Paths and line numbers styled, matched line content highlighted with the language of the file, match emphasized, truncation footer preserved |
12
+ | `find`, `ls` | Directories dimmed with a trailing slash, basenames colored by file family, limit footer preserved |
13
+ | `bash`, `powershell` | Completed output styled (diffs, JSON, paths, errors), collapsed preview with the expand hint, duration line |
14
+ | `read` | Call header with path and line range; expanded body numbered from the real line the tool read (`offset`), highlighted by file language, continuation and truncation notices in the warning color; a collapsed call stays silent like stock pi |
15
+ | `edit` | Diff highlighted by file language, line numbers dimmed, changed words in reverse video; the diff fills the body of the call's box, whose background follows success or failure |
16
+ | `write` | Header preview of the content, highlighted and capped while the argument is still arriving, with the stock line footer; failures in the error color |
17
+ | `codemode`, MCP tools, unknown tools | Only when the tool has no result renderer of its own: JSON and diff bodies highlighted, anything else in the plain content color, collapsed to 10 lines with the expand hint |
18
+ | markdown fences | A fence with no info string gets a detected language injected so the existing highlighter colors it; a fence that already carries a label is never rewritten. Detection never answers `markdown` or `plaintext`, and fences tagged `mermaid`, `math`, `latex`, or `katex` are left untouched |
19
+ | assistant markdown | Headings in the theme's accent color with one attribute per level, inline code with its backtick delimiters visible, and blockquote text in the accent color |
20
+
21
+ ## How the markdown styling works
22
+
23
+ There is no hook for the markdown theme, so the styles are written into the markdown text itself as terminal sequences, through the same transformer that labels fences. Three rules keep that contained:
24
+
25
+ - Only `assistant` messages are touched. In a user message the line background and the preserved backslash escapes would fight the injected sequences; thinking blocks keep their own default style.
26
+ - Every sequence opens and closes inside its line and closes only the attribute it opened, never with a full reset, so the style of the surrounding construct survives the span.
27
+ - The visible text is unchanged: stripping the sequences from the render leaves the same characters as the render without the transformer, except the backtick delimiters of inline code, which the stock renderer drops.
28
+
29
+ The colors come from the theme captured on the last rendered tool result, so a theme switch repaints them; a line that already carries a sequence is copied verbatim, which is what keeps a second pass, a Mermaid diagram, or a partially streamed message untouched.
30
+
31
+ ## What it cannot do
32
+
33
+ `registerMarkdownTransformer` is a display-only `string => string` step applied before parsing. There is no extension hook that replaces the `Markdown` component from `@earendil-works/pi-tui` or its theme, so table layout, list colors, and column alignment are out of reach, and the markdown styling above is the whole of what the hook allows.
34
+
35
+ Three constructs stay out of reach even for that hook:
36
+
37
+ - Fence chrome and the language label. The info string of a fence *is* the language, so any sequence inside it breaks both the label and the highlighting of the body; a sequence on the closing line stops it from closing.
38
+ - The `│ ` border of a blockquote, which the renderer prepends after wrapping the content.
39
+ - The accent color of a heading stops at its first inline token: the renderer reopens its own heading color around every inline span, so text after `**bold**` or `` `code` `` in the same heading returns to the theme's heading color.
40
+
41
+ ## Development
42
+
43
+ Requires Node 24+ and pi 1.4+.
44
+
45
+ ```bash
46
+ npm install
47
+ npm run typecheck
48
+ npm test
49
+ ```
50
+
51
+ Load it into a session without installing:
52
+
53
+ ```bash
54
+ pi -e /home/pablo/LLMs/pi-syntax-highlight/extensions/index.ts
55
+ ```
56
+
57
+ ## Known limitations
58
+
59
+ - `grep` output whose path itself contains `:N: ` or `-N- ` is inherently ambiguous; pi does not disambiguate it either.
60
+ - Highlighting relies on pi's `highlightCode`, which uses the active theme; without an initialized theme the renderers fall back to plain tool output color. The markdown styling degrades the same way: with no theme captured yet it emits no sequences.
61
+ - Only one markdown transformer is allowed per extension; the pipeline inside this one is a single pass.
62
+ - A `read` body records the line number where each source line starts. A source line longer than the terminal wraps, and the wrapped continuation does not repeat the gutter.
63
+ - The diff of an `edit` marks changed words only where one removed line is followed by one added line, the same rule pi applies; several removed lines keep the per-line colors.
64
+ - Restoring the backticks of an inline code span widens a table cell, so the columns of that table are laid out again.
65
+ - Code spans delimited by more than one backtick, setext headings, and escaped backticks are copied as they are.
66
+
67
+ ## License
68
+
69
+ MIT
@@ -0,0 +1,22 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import { activeTheme, captureTheme } from "../src/active-theme.ts";
3
+ import { createMarkdownTransformer } from "../src/markdown-fence-lang.ts";
4
+ import { resolveToolRenderers } from "../src/resolve-renderers.ts";
5
+
6
+ /**
7
+ * pi-syntax-highlight entry point.
8
+ *
9
+ * The tool resolver wraps the host chain and overrides only the result slots
10
+ * this package styles; the markdown transformer labels the fences that arrive
11
+ * without a language so the built-in highlighter has a grammar to paint with, and
12
+ * styles headings, blockquotes and inline code spans for the assistant message.
13
+ */
14
+ export default function piSyntaxHighlight(pi: ExtensionAPI): void {
15
+ pi.registerToolRenderer(resolveToolRenderers);
16
+ pi.registerMarkdownTransformer(createMarkdownTransformer({ resolveTheme: activeTheme }));
17
+ // A render slot receives the theme it paints with, but a markdown message does
18
+ // not: `session_start` is the one place the extension is handed one, and the
19
+ // reference it captures keeps resolving against the theme in effect, so no later
20
+ // session event is needed.
21
+ pi.on("session_start", (_event, ctx) => captureTheme(ctx.ui.theme));
22
+ }
package/package.json CHANGED
@@ -1,6 +1,37 @@
1
1
  {
2
2
  "name": "pi-syntax-highlight",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "description": "Syntax highlighting for pi tool output (grep, find, ls, bash, read, edit, write) and for assistant markdown (headings, blockquotes, inline code), plus language detection for unlabeled markdown fences.",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/piousp/llm-skills.git",
9
+ "directory": "pi-syntax-highlight"
10
+ },
11
+ "keywords": [
12
+ "pi-package"
13
+ ],
14
+ "pi": {
15
+ "extensions": [
16
+ "./extensions"
17
+ ]
18
+ },
19
+ "type": "module",
20
+ "scripts": {
21
+ "test": "node --experimental-strip-types --test test/unit/*.test.ts",
22
+ "typecheck": "tsc --noEmit -p tsconfig.json",
23
+ "test:types": "npm run typecheck"
24
+ },
25
+ "peerDependencies": {
26
+ "@earendil-works/pi-coding-agent": "*",
27
+ "@earendil-works/pi-tui": "*"
28
+ },
29
+ "dependencies": {
30
+ "diff": "^8.0.4"
31
+ },
32
+ "devDependencies": {
33
+ "@earendil-works/pi-coding-agent": "^1.4.0",
34
+ "@earendil-works/pi-tui": "^1.4.0",
35
+ "typescript": "^7.0.2"
36
+ }
37
+ }
@@ -0,0 +1,26 @@
1
+ import type { Theme } from "@earendil-works/pi-coding-agent";
2
+
3
+ // The `Theme` a renderer receives is a proxy of stable identity whose reads
4
+ // resolve against the theme in effect, so a theme change never swaps the object.
5
+ // Keeping the reference is therefore correct, and anything derived from it (a
6
+ // palette, a cached style, a highlight line) has to be invalidated by value:
7
+ // comparing the reference would never see the change.
8
+ let active: Theme | undefined;
9
+
10
+ // Called by every render slot this package owns, before anything is painted, so
11
+ // the last frame rendered is what the non-renderer paths see afterwards.
12
+ export function captureTheme(theme: Theme): void {
13
+ active = theme;
14
+ }
15
+
16
+ // `undefined` until the first result of a session paints, and again after
17
+ // `resetActiveTheme`; callers degrade to unstyled output instead of throwing.
18
+ export function activeTheme(): Theme | undefined {
19
+ return active;
20
+ }
21
+
22
+ // Tests only: the capture is module state on purpose, and each test starts from
23
+ // the same "no theme seen yet" state instead of inheriting the previous one.
24
+ export function resetActiveTheme(): void {
25
+ active = undefined;
26
+ }
@@ -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
+ }