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.
Files changed (49) hide show
  1. package/CHANGELOG.md +73 -58
  2. package/DEVELOPER.md +61 -34
  3. package/LICENSE +21 -0
  4. package/README.md +19 -18
  5. package/extensions/index.ts +5 -10
  6. package/package.json +5 -4
  7. package/src/detect-language.ts +205 -8
  8. package/src/emphasis.ts +82 -1
  9. package/src/highlight.ts +25 -9
  10. package/src/lru-cache.ts +30 -0
  11. package/src/markdown-fence-lang.ts +20 -87
  12. package/src/render-bash.ts +29 -2
  13. package/src/render-edit.ts +0 -6
  14. package/src/render-generic.ts +10 -21
  15. package/src/render-read.ts +8 -149
  16. package/src/render-utils.ts +8 -17
  17. package/src/resolve-renderers.ts +181 -240
  18. package/src/tokens.ts +1 -34
  19. package/assets/icon.png +0 -0
  20. package/assets/icon.svg +0 -36
  21. package/src/active-theme.ts +0 -26
  22. package/src/markdown-style.ts +0 -134
  23. package/src/markdown-theme.ts +0 -53
  24. package/src/render-path.ts +0 -54
  25. package/src/render-write.ts +0 -81
  26. package/test/unit/active-theme.test.ts +0 -38
  27. package/test/unit/bash-preview.test.ts +0 -57
  28. package/test/unit/detect-language.test.ts +0 -124
  29. package/test/unit/emphasis.test.ts +0 -113
  30. package/test/unit/extensions-index.test.ts +0 -116
  31. package/test/unit/grep-parse.test.ts +0 -125
  32. package/test/unit/highlight.test.ts +0 -107
  33. package/test/unit/markdown-fence-lang.test.ts +0 -131
  34. package/test/unit/markdown-pipeline.test.ts +0 -153
  35. package/test/unit/markdown-render-parity.test.ts +0 -127
  36. package/test/unit/markdown-style.test.ts +0 -152
  37. package/test/unit/markdown-theme.test.ts +0 -86
  38. package/test/unit/render-bash.test.ts +0 -194
  39. package/test/unit/render-edit.test.ts +0 -214
  40. package/test/unit/render-find.test.ts +0 -139
  41. package/test/unit/render-generic.test.ts +0 -152
  42. package/test/unit/render-grep.test.ts +0 -338
  43. package/test/unit/render-ls.test.ts +0 -132
  44. package/test/unit/render-read.test.ts +0 -375
  45. package/test/unit/render-utils.test.ts +0 -324
  46. package/test/unit/render-write.test.ts +0 -143
  47. package/test/unit/resolve-renderers.test.ts +0 -474
  48. package/test/unit/style-path.test.ts +0 -74
  49. package/tsconfig.json +0 -15
@@ -1,26 +0,0 @@
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
- }
@@ -1,134 +0,0 @@
1
- import { isClosingFence, parseOpeningFence, type OpeningFence } from "./markdown-fence-lang.ts";
2
- import type { MarkdownPalette } from "./markdown-theme.ts";
3
- import { FG_DEFAULT, ITALIC_OFF, ITALIC_ON, UNDERLINE_OFF, UNDERLINE_ON } from "./tokens.ts";
4
-
5
- // CommonMark allows up to three spaces before a block marker. The text after the
6
- // marker is required: a bare `## ` is a heading still arriving while the message
7
- // streams, and wrapping an empty span would only add sequences around nothing.
8
- const HEADING = /^( {0,3})(#{1,6})([ \t]+)(.*)$/;
9
-
10
- // The `>` run is captured whole so a nested quote keeps every marker exactly where
11
- // the renderer expects to find it before it prepends its own border.
12
- const QUOTE = /^( {0,3})((?:>[ \t]?)+)(.*)$/;
13
-
14
- // The pass writes its own escapes, and the mermaid transformer that runs before it
15
- // may have written some too. A line that already carries one is copied verbatim:
16
- // that single guard is what makes a second run over the same text a no-op, and
17
- // what keeps a diagram from being restyled as a blockquote.
18
- const ESCAPE = "\u001b";
19
-
20
- // A backtick preceded by a backslash: the escape a code span needs, and also the
21
- // escape the pass itself emits, so a line carrying one is never rewritten twice.
22
- // Rewriting half of it would turn visible text into markup, and the same guard
23
- // covers a span whose text ends in a backslash, because the tick after that
24
- // backslash is escaped too.
25
- const ESCAPED_BACKTICK = "\\`";
26
-
27
- // The delimiters the renderer drops from a code span, re-emitted escaped so
28
- // `marked` hands them back as text and they take the style of their context.
29
- const RESTORED_BACKTICK = ESCAPED_BACKTICK;
30
-
31
- // The host already paints every heading bold, so the level is told apart by one
32
- // attribute that survives the host's own prefixes: h1 and h3 take italic, h2
33
- // underline, and h4-h6 nothing, since the renderer prints their `#` prefix
34
- // visibly and that is distinction enough.
35
- const ITALIC = { on: ITALIC_ON, off: ITALIC_OFF };
36
- const UNDERLINE = { on: UNDERLINE_ON, off: UNDERLINE_OFF };
37
-
38
- // Styles the constructs the transformer alone can reach, leaving every visible
39
- // character where it was: headings and blockquote lines get their sequences inside
40
- // the line, and the delimiters of an inline code span come back as escaped text.
41
- // Fences are the one place both stages agree not to touch (see `markdown-fence-lang`).
42
- export function styleMarkdown(markdown: string, palette: MarkdownPalette): string {
43
- const lines = markdown.split("\n");
44
- const output: string[] = [];
45
- let opening: OpeningFence | undefined;
46
- for (const line of lines) {
47
- if (opening !== undefined) {
48
- // Inside a fence every line is literal body, closing line included.
49
- output.push(line);
50
- if (isClosingFence(line, opening)) opening = undefined;
51
- continue;
52
- }
53
- const fence = parseOpeningFence(line);
54
- if (fence !== undefined) {
55
- output.push(line);
56
- opening = fence;
57
- continue;
58
- }
59
- if (line.includes(ESCAPE)) {
60
- output.push(line);
61
- continue;
62
- }
63
- output.push(styleLine(restoreInlineCode(line), palette));
64
- }
65
- return output.join("\n");
66
- }
67
-
68
- // Headings and quote markers are mutually exclusive (`#` and `>` cannot start the
69
- // same line), so the order only documents which construct owns the line.
70
- function styleLine(line: string, palette: MarkdownPalette): string {
71
- const heading = HEADING.exec(line);
72
- if (heading !== null) {
73
- const text = heading[4] ?? "";
74
- if (text !== "") {
75
- const attribute = headingAttribute((heading[2] ?? "").length);
76
- const open = palette.accent + (attribute?.on ?? "");
77
- if (open === "") return line;
78
- // Closed in reverse order: the attribute first, then the color it borrowed.
79
- const close = (attribute?.off ?? "") + (palette.accent === "" ? "" : FG_DEFAULT);
80
- return `${heading[1] ?? ""}${heading[2] ?? ""}${heading[3] ?? ""}${open}${text}${close}`;
81
- }
82
- }
83
- if (palette.quoteOpen === "") return line;
84
- const quote = QUOTE.exec(line);
85
- if (quote === null) return line;
86
- const text = quote[3] ?? "";
87
- // A quote line with no text is the blank line between two quote paragraphs: the
88
- // host prints it empty, so there is nothing to color and no pair to add.
89
- if (text === "") return line;
90
- return `${quote[1] ?? ""}${quote[2] ?? ""}${palette.quoteOpen}${text}${palette.quoteClose}`;
91
- }
92
-
93
- function headingAttribute(level: number): { on: string; off: string } | undefined {
94
- if (level === 1 || level === 3) return ITALIC;
95
- if (level === 2) return UNDERLINE;
96
- return undefined;
97
- }
98
-
99
- // The renderer prints a code span without its backticks (`markdown.js` styles
100
- // `token.text` only), so the delimiters come back as escaped text. Nothing is
101
- // injected: the delimiters inherit the style of the context they sit in, which
102
- // degrades nothing inside a link, a heading or a table cell.
103
- function restoreInlineCode(line: string): string {
104
- if (line.includes(ESCAPED_BACKTICK)) return line;
105
- let output = "";
106
- let index = 0;
107
- while (index < line.length) {
108
- const open = singleBacktickAt(line, index);
109
- if (open === -1) return output + line.slice(index);
110
- const close = singleBacktickAt(line, open + 1);
111
- // Half a pair is a span still arriving: the line stays as it is until the
112
- // closing tick shows up, which is the same moment the renderer turns it into a
113
- // code span.
114
- if (close === -1) return line;
115
- const text = line.slice(open + 1, close);
116
- // A span holding a double tick is a multi-backtick delimiter this pass does not
117
- // model. An empty span cannot reach here (`singleBacktickAt` refuses a run of
118
- // two), and the check stays as the invariant the pair is written under.
119
- if (text === "" || text.includes("``")) return line;
120
- output += line.slice(index, open) + RESTORED_BACKTICK + text + RESTORED_BACKTICK;
121
- index = close + 1;
122
- }
123
- return output;
124
- }
125
-
126
- // Returns the index of the next backtick that stands alone, or -1. A run of two or
127
- // more is a different delimiter, so it is skipped rather than taken as an edge.
128
- function singleBacktickAt(line: string, from: number): number {
129
- for (let index = line.indexOf("`", from); index !== -1; index = line.indexOf("`", index + 1)) {
130
- if (line[index - 1] === "`" || line[index + 1] === "`") continue;
131
- return index;
132
- }
133
- return -1;
134
- }
@@ -1,53 +0,0 @@
1
- import type { ThemeColor } from "@earendil-works/pi-coding-agent";
2
- import { type AnsiTheme, DIM_OFF, DIM_ON, FG_DEFAULT, MD_TOKEN } from "./tokens.ts";
3
-
4
- // The heading foreground. One token for all six levels on purpose: the level is
5
- // told apart by an SGR attribute in `markdown-style.ts`, so retheming cannot make
6
- // two levels of the same document drift into two unrelated colors.
7
- const ACCENT_TOKEN: ThemeColor = "accent";
8
-
9
- // The few sequences the markdown pass injects, derived from the theme once per
10
- // render. The host's markdown theme is not extensible, so this is the palette the
11
- // pass paints with instead of `fg()`, which wraps a whole string and therefore
12
- // cannot recolor a span inside a line the host already painted.
13
- export interface MarkdownPalette {
14
- // What every heading opens with; "" when the theme gives the token no color of
15
- // its own, which is the signal to inject nothing at all.
16
- accent: string;
17
- // The quote pair has to undo what the host paints itself: `mdQuote` may be
18
- // faint, so opening the quote turns that off first and closing restores it.
19
- quoteOpen: string;
20
- quoteClose: string;
21
- // Value key of the transformer memo, derived from the sequences above and
22
- // nothing else: a theme switch that does not move them leaves the previous
23
- // render valid, which is exactly right because the bytes would be identical.
24
- signature: string;
25
- }
26
-
27
- export function markdownPalette(theme: AnsiTheme): MarkdownPalette {
28
- const accent = ownFg(theme, ACCENT_TOKEN);
29
- // The host paints blockquote text through `mdQuote`, and its faint flag is what
30
- // `\x1b[22m` closes. That is also the one sequence of the pass that closes bold
31
- // along with dim, which is safe only here: the host applies no bold to quote text.
32
- const quoteIsDim = theme.getFgAnsi("mdQuote").includes(DIM_ON);
33
- const quoteFg = ownFg(theme, MD_TOKEN.quote);
34
- // Without a color of its own the construct has nothing to say, so the pair stays
35
- // empty rather than cancelling and re-applying the host's own style.
36
- const quoteOpen = quoteFg === "" ? "" : (quoteIsDim ? DIM_OFF : "") + quoteFg;
37
- const quoteClose = quoteFg === "" ? "" : FG_DEFAULT + (quoteIsDim ? DIM_ON : "");
38
- return {
39
- accent,
40
- quoteOpen,
41
- quoteClose,
42
- signature: [accent, quoteOpen, quoteClose].join("\u0000"),
43
- };
44
- }
45
-
46
- // A token the theme maps to the terminal default ("" or `\x1b[39m` in the host's
47
- // `Theme`) is not a color this pass can add: injecting it would wrap the text in
48
- // sequences that only cancel each other, which is precisely what a monochrome
49
- // terminal must not receive.
50
- function ownFg(theme: AnsiTheme, token: ThemeColor): string {
51
- const ansi = theme.getFgAnsi(token);
52
- return ansi === "" || ansi === FG_DEFAULT ? "" : ansi;
53
- }
@@ -1,54 +0,0 @@
1
- import { homedir } from "node:os";
2
- import { isAbsolute, relative, resolve, sep } from "node:path";
3
- import { pathToFileURL } from "node:url";
4
- import { getCapabilities, hyperlink } from "@earendil-works/pi-tui";
5
- import { TOKEN, type StyleTheme } from "./tokens.ts";
6
-
7
- // The path display the built-in tool headers share. The host does not export
8
- // `renderToolPath`, `shortenPath` or `linkPath`, so a header of this package has
9
- // to reproduce them from the primitives it does export (`hyperlink`, the
10
- // terminal capabilities and the two colors below). Everything here is display:
11
- // the text is shortened for the eye while the link still targets the absolute
12
- // path, so a click opens the file the tool actually read.
13
-
14
- // Only the home directory is shortened. The built-in never makes the path
15
- // relative to the cwd, so a file outside the home directory keeps its full path.
16
- export function shortenPath(path: string): string {
17
- const home = homedir();
18
- return path.startsWith(home) ? `~${path.slice(home.length)}` : path;
19
- }
20
-
21
- // The target of the link: the argument as the tool itself resolves it, with `~`
22
- // expanded and a relative path anchored at the cwd. A path that is already
23
- // absolute passes through `resolve` untouched.
24
- export function absolutePath(rawPath: string, cwd: string | undefined): string {
25
- const expanded = rawPath === "~" || rawPath.startsWith("~/") ? `${homedir()}${rawPath.slice(1)}` : rawPath;
26
- return resolve(cwd ?? process.cwd(), expanded);
27
- }
28
-
29
- // OSC 8, gated on the terminal capability exactly like the built-in, so a
30
- // terminal without hyperlink support shows the styled text alone.
31
- export function linkPath(styledText: string, rawPath: string, cwd: string | undefined): string {
32
- if (!getCapabilities().hyperlinks) return styledText;
33
- return hyperlink(styledText, pathToFileURL(absolutePath(rawPath, cwd)).href);
34
- }
35
-
36
- // An empty path is the argument that has not arrived yet, which the built-in
37
- // renders as a placeholder instead of an empty slot.
38
- export function renderToolPath(rawPath: string, theme: StyleTheme, cwd: string | undefined): string {
39
- if (rawPath === "") return theme.fg(TOKEN.plain, "...");
40
- return linkPath(theme.fg(TOKEN.path, shortenPath(rawPath)), rawPath, cwd);
41
- }
42
-
43
- // The one cwd-relative form the built-in shows: a path inside the cwd becomes
44
- // relative, anything else stays absolute, and the separators are made posix so
45
- // the label reads the same on every platform.
46
- export function relativeToCwdOrAbsolute(filePath: string, cwd: string | undefined): string {
47
- const base = resolve(cwd ?? process.cwd());
48
- const absolute = resolve(filePath);
49
- const relativePath = relative(base, absolute);
50
- const inside =
51
- relativePath === "" ||
52
- (relativePath !== ".." && !relativePath.startsWith(`..${sep}`) && !isAbsolute(relativePath));
53
- return (inside ? relativePath || "." : absolute).split(sep).join("/");
54
- }
@@ -1,81 +0,0 @@
1
- import { getLanguageFromPath } from "@earendil-works/pi-coding-agent";
2
- import { highlightLines, type HighlightFn } from "./highlight.ts";
3
- import { renderToolPath } from "./render-path.ts";
4
- import { replaceTabs } from "./render-utils.ts";
5
- import { TOKEN, type StyleTheme } from "./tokens.ts";
6
-
7
- // The built-in write renderer shows 10 lines when collapsed; any other number is
8
- // a visible difference against stock pi.
9
- const COLLAPSED_LINES = 10;
10
-
11
- // While the argument is still streaming the tail is repainted on every frame and
12
- // tokenizing it costs more than it shows: the prefix is what the reader watches.
13
- const PARTIAL_HIGHLIGHT_LINES = 60;
14
-
15
- const INVALID_CONTENT = "[invalid content arg - expected string]";
16
-
17
- export interface WriteResultInput {
18
- output: string;
19
- isError: boolean;
20
- theme: StyleTheme;
21
- }
22
-
23
- export interface WriteCallInput {
24
- /** The argument's path, "" when the call does not carry one. */
25
- path: string;
26
- /** "" is an argument that has not arrived yet, null one that is not a string. */
27
- content: string | null;
28
- argsComplete: boolean;
29
- expanded: boolean;
30
- theme: StyleTheme;
31
- expandHint: string;
32
- highlight: HighlightFn;
33
- cwd: string | undefined;
34
- }
35
-
36
- // The tool reports a success with no text at all, so only a failure has a body
37
- // to paint; the caller drops the empty string instead of drawing a blank row.
38
- export function buildWriteResultText(input: WriteResultInput): string {
39
- if (!input.isError || input.output === "") return "";
40
- return `\n${input.theme.fg(TOKEN.broken, input.output)}`;
41
- }
42
-
43
- function trimTrailingEmptyLines(lines: string[]): string[] {
44
- let end = lines.length;
45
- while (end > 0 && lines[end - 1] === "") end -= 1;
46
- return lines.slice(0, end);
47
- }
48
-
49
- function previewLines(lines: string[], language: string | undefined, input: WriteCallInput): string[] {
50
- const fallback = (line: string): string => input.theme.fg(TOKEN.plain, line);
51
- if (language === undefined) return lines.map(fallback);
52
- const head = input.argsComplete ? lines : lines.slice(0, PARTIAL_HIGHLIGHT_LINES);
53
- const highlighted = highlightLines(head.join("\n"), language, fallback, input.highlight);
54
- return [...highlighted, ...lines.slice(head.length).map(fallback)];
55
- }
56
-
57
- // The header the built-in writes, plus the preview of what is about to be
58
- // written. The pipeline is the built-in's, with the streaming case bounded: the
59
- // trailing blank lines go before the count and the tab expansion, the content is
60
- // tokenized as one block, and the footer keeps its wording.
61
- export function buildWriteCallText(input: WriteCallInput): string {
62
- const theme = input.theme;
63
- const title = `${theme.fg("toolTitle", theme.bold("write"))} ${renderToolPath(input.path, theme, input.cwd)}`;
64
- if (input.content === null) return `${title}\n\n${theme.fg(TOKEN.broken, INVALID_CONTENT)}`;
65
- if (input.content === "") return title;
66
- // Carriage returns go before anything is measured, so a CRLF argument does not
67
- // show as an extra blank line; the three spaces of a tab are the indent the
68
- // rest of the package renders against.
69
- const content = replaceTabs(input.content.replace(/\r/g, ""));
70
- const lines = trimTrailingEmptyLines(content.split("\n"));
71
- const language = input.path === "" ? undefined : getLanguageFromPath(input.path);
72
- const styled = previewLines(lines, language, input);
73
- const total = lines.length;
74
- const limit = input.expanded ? total : COLLAPSED_LINES;
75
- let text = `${title}\n\n${styled.slice(0, limit).join("\n")}`;
76
- const hidden = total - limit;
77
- if (hidden > 0) {
78
- text += `${theme.fg("muted", `\n... (${hidden} more lines, ${total} total,`)} ${input.expandHint}${theme.fg("muted", ")")}`;
79
- }
80
- return text;
81
- }
@@ -1,38 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import type { Theme } from "@earendil-works/pi-coding-agent";
4
- import { activeTheme, captureTheme, resetActiveTheme } from "../../src/active-theme.ts";
5
-
6
- // The module stores the reference and nothing else, so a bare object stands in
7
- // for the host's `Theme`, which cannot be constructed outside a session.
8
- function fakeTheme(label: string): Theme {
9
- return { label } as unknown as Theme;
10
- }
11
-
12
- test("activeTheme: no capture yet yields undefined", () => {
13
- resetActiveTheme();
14
- assert.equal(activeTheme(), undefined);
15
- });
16
-
17
- test("activeTheme: the captured theme comes back as the same reference", () => {
18
- resetActiveTheme();
19
- const theme = fakeTheme("dark");
20
- captureTheme(theme);
21
- assert.equal(activeTheme(), theme);
22
- });
23
-
24
- test("captureTheme: a second capture replaces the first", () => {
25
- resetActiveTheme();
26
- const first = fakeTheme("dark");
27
- const second = fakeTheme("light");
28
- captureTheme(first);
29
- captureTheme(second);
30
- assert.equal(activeTheme(), second);
31
- });
32
-
33
- test("resetActiveTheme: the capture is dropped", () => {
34
- resetActiveTheme();
35
- captureTheme(fakeTheme("dark"));
36
- resetActiveTheme();
37
- assert.equal(activeTheme(), undefined);
38
- });
@@ -1,57 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import { visibleWidth } from "@earendil-works/pi-tui";
4
- import { createBashPreview } from "../../src/render-bash.ts";
5
-
6
- const fakeTheme = {
7
- fg: (c: string, t: string) => `<${c}>${t}</${c}>`,
8
- bg: (c: string, t: string) => `<bg:${c}>${t}</bg:${c}>`,
9
- bold: (t: string) => `<b>${t}</b>`,
10
- };
11
-
12
- const EXPAND_HINT = "<hint>";
13
- // Byte-identical to the built-in preview hint for five hidden visual lines.
14
- const HINT_5 = "<muted>... (5 earlier lines,</muted> <hint><muted>)</muted>";
15
-
16
- const WIDE = 200;
17
-
18
- function styledLine(index: number): string {
19
- return `<toolOutput>line${index}</toolOutput>`;
20
- }
21
-
22
- function body(count: number): string {
23
- return Array.from({ length: count }, (_, index) => styledLine(index + 1)).join("\n");
24
- }
25
-
26
- test("createBashPreview: ten lines collapse to the last five plus the hint", () => {
27
- const lines = createBashPreview(body(10), EXPAND_HINT, fakeTheme).render(WIDE).map((line) => line.trimEnd());
28
- assert.equal(lines.length, 6);
29
- assert.equal(lines[0], HINT_5);
30
- assert.deepEqual(lines.slice(1), [6, 7, 8, 9, 10].map(styledLine));
31
- });
32
-
33
- test("createBashPreview: fewer lines than the limit render without a hint", () => {
34
- const lines = createBashPreview(body(3), EXPAND_HINT, fakeTheme).render(WIDE).map((line) => line.trimEnd());
35
- assert.deepEqual(lines, [1, 2, 3].map(styledLine));
36
- });
37
-
38
- test("createBashPreview: a narrow width keeps five visual lines and truncates each to the width", () => {
39
- const long = "0123456789".repeat(10);
40
- const lines = createBashPreview(long, EXPAND_HINT, fakeTheme).render(10);
41
- assert.equal(lines.length, 6);
42
- for (const line of lines) assert.ok(visibleWidth(line) <= 10, `${line} is wider than 10`);
43
- // The tail of the wrapped line is kept, and the hint sits above it.
44
- assert.deepEqual(lines.slice(1), Array.from({ length: 5 }, () => "0123456789"));
45
- assert.ok(lines[0].startsWith("<muted>"), `${lines[0]} is not the hint`);
46
- });
47
-
48
- test("createBashPreview: lines are cached per width and dropped by invalidate", () => {
49
- const preview = createBashPreview(body(10), EXPAND_HINT, fakeTheme);
50
- const first = preview.render(WIDE);
51
- assert.equal(preview.render(WIDE), first);
52
-
53
- preview.invalidate();
54
- const afterInvalidate = preview.render(WIDE);
55
- assert.notEqual(afterInvalidate, first);
56
- assert.deepEqual(afterInvalidate, first);
57
- });
@@ -1,124 +0,0 @@
1
- import { test } from "node:test";
2
- import assert from "node:assert/strict";
3
- import { detectLanguage } from "../../src/detect-language.ts";
4
-
5
- test("detectLanguage: a JSON object and a JSON array are both json", () => {
6
- assert.equal(detectLanguage(`{\n "name": "pi",\n "version": "1.0.0"\n}`), "json");
7
- // A single line, short, with no newline: the strong signal bypasses the gate.
8
- assert.equal(detectLanguage("[1, 2, 3]"), "json");
9
- assert.equal(detectLanguage("{not json}"), undefined);
10
- });
11
-
12
- test("detectLanguage: a patch is diff", () => {
13
- const patch = `diff --git a/src/a.ts b/src/a.ts
14
- index 1111111..2222222 100644
15
- --- a/src/a.ts
16
- +++ b/src/a.ts
17
- @@ -1,3 +1,3 @@
18
- -old line
19
- +new line`;
20
- assert.equal(detectLanguage(patch), "diff");
21
- });
22
-
23
- test("detectLanguage: a shebang names its interpreter", () => {
24
- assert.equal(detectLanguage(`#!/bin/bash\nset -euo pipefail\necho hi`), "bash");
25
- assert.equal(detectLanguage(`#!/usr/bin/env python3\nprint("hi")`), "python");
26
- assert.equal(detectLanguage(`#!/usr/bin/env node\nconsole.log("hi")`), "javascript");
27
- });
28
-
29
- test("detectLanguage: SQL needs a statement keyword and a clause keyword", () => {
30
- assert.equal(detectLanguage(`SELECT id, name\nFROM users\nWHERE active = 1;`), "sql");
31
- // Prose that opens with a statement keyword but carries no clause is prose.
32
- assert.equal(detectLanguage("select the option that fits the current layout"), undefined);
33
- assert.equal(detectLanguage("select the file"), undefined);
34
- });
35
-
36
- test("detectLanguage: a def with a colon is python", () => {
37
- assert.equal(detectLanguage(`def f(x):\n if x:\n return x\n return None`), "python");
38
- });
39
-
40
- test("detectLanguage: key lines are yaml, and one colon in prose is not", () => {
41
- assert.equal(detectLanguage(`name: pi-syntax-highlight\nversion: 0.1.0\ndependencies:\n left-pad: ^1.0.0`), "yaml");
42
- assert.equal(detectLanguage("Note: the plugin adds syntax highlighting for tool output"), undefined);
43
- });
44
-
45
- test("detectLanguage: a section header plus assignments is toml", () => {
46
- assert.equal(detectLanguage(`[package]\nname = "pi-syntax-highlight"\nversion = "0.1.0"`), "toml");
47
- });
48
-
49
- test("detectLanguage: annotations are typescript, plain declarations are javascript", () => {
50
- assert.equal(detectLanguage(`interface User {\n id: number;\n name: string;\n}`), "typescript");
51
- assert.equal(detectLanguage(`const items = [1, 2, 3];\nconst doubled = items.map((item) => item * 2);`), "javascript");
52
- });
53
-
54
- test("detectLanguage: go needs a package clause, a bare fn is not go", () => {
55
- assert.equal(detectLanguage(`package main\n\nfunc main() {\n\tprintln("hi")\n}`), "go");
56
- // A single fn signal is one of Rust's two, so this stays unlabeled instead of
57
- // becoming a wrong guess.
58
- assert.equal(detectLanguage(`fn main() {\n println!("hello");\n}`), undefined);
59
- });
60
-
61
- test("detectLanguage: go and rust outrank the JavaScript signals they share", () => {
62
- // `import "fmt"` matches the JavaScript `import` signal, and `let mut` matches
63
- // its `let` one, so a wrong guess is what happens without the precedence.
64
- assert.equal(
65
- detectLanguage(`package main\n\nimport "fmt"\n\nfunc main() {\n\tfmt.Println("hi")\n}`),
66
- "go",
67
- );
68
- assert.equal(detectLanguage(`fn main() {\n let mut x = 1;\n use std::io;\n}`), "rust");
69
- });
70
-
71
- test("detectLanguage: only an XML declaration, a doctype, or an html tag are markup", () => {
72
- assert.equal(detectLanguage(`<?xml version="1.0"?>\n<root>\n <item/>\n</root>`), "xml");
73
- assert.equal(detectLanguage(`<!DOCTYPE html>\n<html lang="en">\n<body>hi</body>\n</html>`), "xml");
74
- assert.equal(detectLanguage(`<html lang="en">\n<body>hi</body>\n</html>`), "html");
75
- assert.equal(detectLanguage(`<div class="x">\n <span>hello</span>\n</div>`), undefined);
76
- });
77
-
78
- test("detectLanguage: an access modifier next to a class or void keyword is java", () => {
79
- assert.equal(
80
- detectLanguage(`public class Main {\n public static void main(String[] args) {\n System.out.println("hi");\n }\n}`),
81
- "java",
82
- );
83
- assert.equal(detectLanguage(`using System;\n\nnamespace App;\n\npublic class Program { }`), "csharp");
84
- });
85
-
86
- test("detectLanguage: a decorator alone does not make a snippet python", () => {
87
- // `@Injectable()` used to be a Python signal, which stole the block from the
88
- // type annotation that identifies it.
89
- assert.equal(detectLanguage(`@Injectable()\nexport class Api {\n base: string;\n}`), "typescript");
90
- });
91
-
92
- test("detectLanguage: interface bodies with annotation lines are typescript, not css", () => {
93
- assert.equal(detectLanguage(`interface A {\n a: string;\n}\ninterface B {\n b: string;\n}`), "typescript");
94
- });
95
-
96
- test("detectLanguage: bare key lines are yaml even when the values look like annotations", () => {
97
- assert.equal(detectLanguage(`name: string\nage: number\nenabled: boolean`), "yaml");
98
- });
99
-
100
- test("detectLanguage: a markdown table is not shell", () => {
101
- assert.equal(
102
- detectLanguage(`| Language | Runtime |\n|---|---|\n| Rust | native |\n| Go | native |`),
103
- undefined,
104
- );
105
- });
106
-
107
- test("detectLanguage: a log, prose, and empty text have no language", () => {
108
- assert.equal(
109
- detectLanguage(`2024-01-02T03:04:05Z INFO starting\n2024-01-02T03:04:06Z WARN slow\n2024-01-02T03:04:07Z INFO done`),
110
- undefined,
111
- );
112
- assert.equal(
113
- detectLanguage(
114
- "This paragraph explains why the transformer runs before rendering.\nIt keeps the fence character and the body untouched.",
115
- ),
116
- undefined,
117
- );
118
- assert.equal(detectLanguage(""), undefined);
119
- assert.equal(detectLanguage(" \n\t\n"), undefined);
120
- });
121
-
122
- test("detectLanguage: a single short line never carries a language", () => {
123
- assert.equal(detectLanguage("x = 1"), undefined);
124
- });