pi-syntax-highlight 0.1.0 → 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 +81 -40
- package/DEVELOPER.md +100 -0
- package/LICENSE +21 -0
- package/README.md +42 -44
- package/extensions/index.ts +5 -10
- package/package.json +20 -5
- 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/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
|
@@ -1,474 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { Box, Container, Text, imageFallback, setCapabilityOverrides, type Component } from "@earendil-works/pi-tui";
|
|
4
|
-
import type { ThemeBg, ThemeColor, ToolRenderers, ToolRenderResultOptions } from "@earendil-works/pi-coding-agent";
|
|
5
|
-
import { resolveToolRenderers } from "../../src/resolve-renderers.ts";
|
|
6
|
-
|
|
7
|
-
// Only the slots are under test, so the components themselves are opaque.
|
|
8
|
-
function baseRenderers(renderShell: ToolRenderers["renderShell"] = "default"): ToolRenderers {
|
|
9
|
-
return {
|
|
10
|
-
renderShell,
|
|
11
|
-
renderCall: () => new Text("base call", 0, 0),
|
|
12
|
-
renderResult: () => new Text("base result", 0, 0),
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// A tool that brings no result renderer of its own: the case the generic
|
|
17
|
-
// overlay exists for.
|
|
18
|
-
function baseRenderersWithoutResult(renderShell: ToolRenderers["renderShell"] = "default"): ToolRenderers {
|
|
19
|
-
return {
|
|
20
|
-
renderShell,
|
|
21
|
-
renderCall: () => new Text("base call", 0, 0),
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
type ResultRenderer = NonNullable<ToolRenderers["renderResult"]>;
|
|
26
|
-
type HostResult = Parameters<ResultRenderer>[0];
|
|
27
|
-
type HostContext = Parameters<ResultRenderer>[3];
|
|
28
|
-
type HostTheme = Parameters<ResultRenderer>[2];
|
|
29
|
-
|
|
30
|
-
// The delegation path never reads a color, so the two members this package uses
|
|
31
|
-
// stand in for the host's richer `Theme`.
|
|
32
|
-
const fakeTheme = {
|
|
33
|
-
fg: (color: ThemeColor, text: string) => `<${color}>${text}</${color}>`,
|
|
34
|
-
bg: (color: ThemeBg, text: string) => `<bg:${color}>${text}</bg:${color}>`,
|
|
35
|
-
bold: (text: string) => `<b>${text}</b>`,
|
|
36
|
-
} as HostTheme;
|
|
37
|
-
|
|
38
|
-
function shellResult(text: string): HostResult {
|
|
39
|
-
return { content: [{ type: "text", text }], details: undefined };
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// The host's `ToolRenderContext` is not exported, so the fake is
|
|
43
|
-
// typed from the renderer signature and carries every field the renderers read.
|
|
44
|
-
function hostContext(overrides: Partial<HostContext> = {}): HostContext {
|
|
45
|
-
return {
|
|
46
|
-
args: {},
|
|
47
|
-
toolCallId: "call-1",
|
|
48
|
-
invalidate: () => {},
|
|
49
|
-
lastComponent: undefined,
|
|
50
|
-
state: {},
|
|
51
|
-
cwd: "/tmp",
|
|
52
|
-
executionStarted: true,
|
|
53
|
-
argsComplete: true,
|
|
54
|
-
isPartial: true,
|
|
55
|
-
expanded: false,
|
|
56
|
-
showImages: false,
|
|
57
|
-
isError: false,
|
|
58
|
-
durationMs: undefined,
|
|
59
|
-
outputPad: 0,
|
|
60
|
-
...overrides,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function firstLine(component: Component | undefined): string | undefined {
|
|
65
|
-
return component?.render(40)[0]?.trimEnd();
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Wide enough that the fake theme's own markup (which counts as visible text) does
|
|
69
|
-
// not push a line into a wrap: these tests assert content, not layout, and the
|
|
70
|
-
// components under test are `Text`, so a narrow width would split assertions.
|
|
71
|
-
const RENDER_WIDTH = 200;
|
|
72
|
-
|
|
73
|
-
function renderedLines(component: Component | undefined): string[] {
|
|
74
|
-
return (component?.render(RENDER_WIDTH) ?? []).map((line) => line.trimEnd());
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
test("resolveToolRenderers: grep, find, ls, bash and powershell replace renderResult and keep the other slots", () => {
|
|
78
|
-
for (const toolName of ["grep", "find", "ls", "bash", "powershell"]) {
|
|
79
|
-
const base = baseRenderers();
|
|
80
|
-
const resolved = resolveToolRenderers(toolName, () => base);
|
|
81
|
-
assert.notEqual(resolved, base);
|
|
82
|
-
assert.equal(resolved?.renderCall, base.renderCall);
|
|
83
|
-
assert.equal(resolved?.renderShell, base.renderShell);
|
|
84
|
-
assert.notEqual(resolved?.renderResult, base.renderResult);
|
|
85
|
-
assert.equal(typeof resolved?.renderResult, "function");
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
test("resolveToolRenderers: a tool with no overlay keeps every slot", () => {
|
|
90
|
-
const base = baseRenderers();
|
|
91
|
-
for (const toolName of ["mcp__docs__search", "some_unknown_tool"]) {
|
|
92
|
-
const resolved = resolveToolRenderers(toolName, () => base);
|
|
93
|
-
assert.notEqual(resolved, base);
|
|
94
|
-
assert.equal(resolved?.renderCall, base.renderCall);
|
|
95
|
-
assert.equal(resolved?.renderResult, base.renderResult);
|
|
96
|
-
assert.equal(resolved?.renderShell, base.renderShell);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
test("resolveToolRenderers: read, write and edit replace the result slot and keep the base chain", () => {
|
|
101
|
-
for (const toolName of ["read", "write", "edit"]) {
|
|
102
|
-
const base = baseRenderers();
|
|
103
|
-
const resolved = resolveToolRenderers(toolName, () => base);
|
|
104
|
-
assert.notEqual(resolved, base);
|
|
105
|
-
assert.equal(resolved?.renderShell, base.renderShell);
|
|
106
|
-
assert.notEqual(resolved?.renderResult, base.renderResult);
|
|
107
|
-
assert.equal(typeof resolved?.renderResult, "function");
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
test("resolveToolRenderers: read and write replace the header, edit keeps the built-in preview", () => {
|
|
112
|
-
for (const toolName of ["read", "write"]) {
|
|
113
|
-
const base = baseRenderers();
|
|
114
|
-
const resolved = resolveToolRenderers(toolName, () => base);
|
|
115
|
-
assert.notEqual(resolved?.renderCall, base.renderCall);
|
|
116
|
-
assert.equal(typeof resolved?.renderCall, "function");
|
|
117
|
-
}
|
|
118
|
-
// The preview of edit comes from a helper the host does not export, so the
|
|
119
|
-
// built-in `renderCall` stays in place and only its box body is replaced.
|
|
120
|
-
const base = baseRenderers();
|
|
121
|
-
assert.equal(resolveToolRenderers("edit", () => base)?.renderCall, base.renderCall);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
test("resolveToolRenderers: an unknown tool without a renderResult gains only the generic overlay", () => {
|
|
125
|
-
for (const toolName of ["mcp__docs__search", "codemode"]) {
|
|
126
|
-
const base = baseRenderersWithoutResult();
|
|
127
|
-
const resolved = resolveToolRenderers(toolName, () => base);
|
|
128
|
-
assert.notEqual(resolved, base);
|
|
129
|
-
assert.equal(resolved?.renderCall, base.renderCall);
|
|
130
|
-
assert.equal(resolved?.renderShell, base.renderShell);
|
|
131
|
-
assert.equal(typeof resolved?.renderResult, "function");
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
test("resolveToolRenderers: the generic overlay renders the result body", () => {
|
|
136
|
-
const resolved = resolveToolRenderers("codemode", () => baseRenderersWithoutResult());
|
|
137
|
-
const component = resolved?.renderResult?.(
|
|
138
|
-
shellResult("plain output"),
|
|
139
|
-
{ expanded: false, isPartial: false },
|
|
140
|
-
fakeTheme,
|
|
141
|
-
hostContext({ isPartial: false }),
|
|
142
|
-
);
|
|
143
|
-
// `wrapResultRenderer` falls back to the unstyled body when the module-global
|
|
144
|
-
// theme is not initialized, which is the case outside a pi session, so the
|
|
145
|
-
// assertion is on the output rather than on the exact prefix.
|
|
146
|
-
assert.match(renderedLines(component).join("\n"), /plain output/);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
test("resolveToolRenderers: find and ls paint the body flat when the context reports the error", () => {
|
|
150
|
-
const cases = [
|
|
151
|
-
{ toolName: "find", line: "src/a.ts" },
|
|
152
|
-
{ toolName: "ls", line: "src/" },
|
|
153
|
-
];
|
|
154
|
-
for (const { toolName, line } of cases) {
|
|
155
|
-
const resolved = resolveToolRenderers(toolName, () => baseRenderers());
|
|
156
|
-
const render = (isError: boolean): string =>
|
|
157
|
-
renderedLines(
|
|
158
|
-
resolved?.renderResult?.(
|
|
159
|
-
shellResult(line),
|
|
160
|
-
{ expanded: false, isPartial: false },
|
|
161
|
-
fakeTheme,
|
|
162
|
-
hostContext({ isPartial: false, isError }),
|
|
163
|
-
),
|
|
164
|
-
).join("\n");
|
|
165
|
-
// The failure is painted flat in the content color: no bold, no file-family
|
|
166
|
-
// color, which is the rule both list renderers share.
|
|
167
|
-
assert.equal(render(true), `\n<text>${line}</text>`);
|
|
168
|
-
assert.notEqual(render(false), `\n<text>${line}</text>`);
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
test("resolveToolRenderers: the generic overlay takes the failure from the context, not from the result", () => {
|
|
173
|
-
const resolved = resolveToolRenderers("codemode", () => baseRenderersWithoutResult());
|
|
174
|
-
const diff = "diff --git a/x.ts b/x.ts\n+const b = 2;";
|
|
175
|
-
const render = (result: HostResult, isError: boolean): string =>
|
|
176
|
-
renderedLines(
|
|
177
|
-
resolved?.renderResult?.(
|
|
178
|
-
result,
|
|
179
|
-
{ expanded: false, isPartial: false },
|
|
180
|
-
fakeTheme,
|
|
181
|
-
hostContext({ isPartial: false, isError }),
|
|
182
|
-
),
|
|
183
|
-
).join("\n");
|
|
184
|
-
// A diff body painted by kind would pass for a normal result, which is the case
|
|
185
|
-
// the flat rule exists for.
|
|
186
|
-
const failed = render(shellResult(diff), true);
|
|
187
|
-
const painted = render(shellResult(diff), false);
|
|
188
|
-
assert.match(failed, /<text>\+const b = 2;<\/text>/);
|
|
189
|
-
assert.equal(failed.includes("<toolDiffAdded>"), false);
|
|
190
|
-
assert.equal(painted.includes("<toolDiffAdded>"), true);
|
|
191
|
-
// The result the TUI hands to a renderer carries no `isError`; a copy that does
|
|
192
|
-
// is not read, because the context is the only source.
|
|
193
|
-
const flagged = Object.assign(shellResult(diff), { isError: true });
|
|
194
|
-
assert.equal(render(flagged, false), painted);
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
test("resolveToolRenderers: renderShell \"self\" survives the spread", () => {
|
|
198
|
-
const resolved = resolveToolRenderers("edit", () => baseRenderers("self"));
|
|
199
|
-
assert.equal(resolved?.renderShell, "self");
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
test("resolveToolRenderers: next() is called exactly once", () => {
|
|
203
|
-
let calls = 0;
|
|
204
|
-
resolveToolRenderers("grep", () => {
|
|
205
|
-
calls += 1;
|
|
206
|
-
return baseRenderers();
|
|
207
|
-
});
|
|
208
|
-
assert.equal(calls, 1);
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
test("resolveToolRenderers: a base of undefined yields an object instead of ending the chain", () => {
|
|
212
|
-
for (const toolName of ["grep", "find", "ls", "bash", "powershell"]) {
|
|
213
|
-
const styled = resolveToolRenderers(toolName, () => undefined);
|
|
214
|
-
assert.notEqual(styled, undefined);
|
|
215
|
-
assert.equal(styled?.renderCall, undefined);
|
|
216
|
-
assert.equal(typeof styled?.renderResult, "function");
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
// The generic overlay applies to any name the host resolves, so an unknown
|
|
220
|
-
// tool with no base at all still comes back renderable instead of `undefined`.
|
|
221
|
-
const other = resolveToolRenderers("some_unknown_tool", () => undefined);
|
|
222
|
-
assert.notEqual(other, undefined);
|
|
223
|
-
assert.equal(other?.renderCall, undefined);
|
|
224
|
-
assert.equal(other?.renderShell, undefined);
|
|
225
|
-
assert.equal(typeof other?.renderResult, "function");
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
test("resolveToolRenderers: a partial shell result delegates to the built-in renderResult", () => {
|
|
229
|
-
for (const toolName of ["bash", "powershell"]) {
|
|
230
|
-
const seen: ToolRenderResultOptions[] = [];
|
|
231
|
-
const base: ToolRenderers = {
|
|
232
|
-
...baseRenderers(),
|
|
233
|
-
renderResult: (_result, options) => {
|
|
234
|
-
seen.push(options);
|
|
235
|
-
return new Text("built-in partial", 0, 0);
|
|
236
|
-
},
|
|
237
|
-
};
|
|
238
|
-
const resolved = resolveToolRenderers(toolName, () => base);
|
|
239
|
-
const component = resolved?.renderResult?.(
|
|
240
|
-
shellResult("streaming output"),
|
|
241
|
-
{ expanded: false, isPartial: true },
|
|
242
|
-
fakeTheme,
|
|
243
|
-
hostContext(),
|
|
244
|
-
);
|
|
245
|
-
assert.equal(firstLine(component), "built-in partial");
|
|
246
|
-
assert.deepEqual(seen, [{ expanded: false, isPartial: true }]);
|
|
247
|
-
}
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
test("resolveToolRenderers: a final shell result stops the pending interval and stamps the end time", () => {
|
|
251
|
-
const cleared: unknown[] = [];
|
|
252
|
-
const realClearInterval = globalThis.clearInterval;
|
|
253
|
-
// The renderer resolves `clearInterval` from the global object at call time, so
|
|
254
|
-
// replacing it here is what makes the cancellation observable.
|
|
255
|
-
Object.assign(globalThis, {
|
|
256
|
-
clearInterval: (handle: ReturnType<typeof setInterval>) => {
|
|
257
|
-
cleared.push(handle);
|
|
258
|
-
},
|
|
259
|
-
});
|
|
260
|
-
try {
|
|
261
|
-
// A plain object stands in for the host's `Timeout`: only `clearInterval`
|
|
262
|
-
// ever reads it, and that is the function replaced above.
|
|
263
|
-
const interval = { unref: () => {} };
|
|
264
|
-
const state: Record<string, unknown> = { startedAt: Date.now() - 5000, interval };
|
|
265
|
-
const resolved = resolveToolRenderers("bash", () => baseRenderers());
|
|
266
|
-
resolved?.renderResult?.(
|
|
267
|
-
shellResult("done"),
|
|
268
|
-
{ expanded: false, isPartial: false },
|
|
269
|
-
fakeTheme,
|
|
270
|
-
hostContext({ isPartial: false, durationMs: 4150, state }),
|
|
271
|
-
);
|
|
272
|
-
assert.deepEqual(cleared, [interval]);
|
|
273
|
-
assert.equal(Object.hasOwn(state, "interval"), false);
|
|
274
|
-
assert.equal(typeof state.endedAt, "number");
|
|
275
|
-
} finally {
|
|
276
|
-
Object.assign(globalThis, { clearInterval: realClearInterval });
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
test("resolveToolRenderers: an unrecorded duration falls back to the row's own clock", () => {
|
|
281
|
-
const state: Record<string, unknown> = { startedAt: Date.now() - 1500 };
|
|
282
|
-
const resolved = resolveToolRenderers("bash", () => baseRenderers());
|
|
283
|
-
const component = resolved?.renderResult?.(
|
|
284
|
-
shellResult("done"),
|
|
285
|
-
{ expanded: false, isPartial: false },
|
|
286
|
-
fakeTheme,
|
|
287
|
-
hostContext({ isPartial: false, durationMs: undefined, state }),
|
|
288
|
-
);
|
|
289
|
-
// The timer is the last child of the component tree, after body and warnings.
|
|
290
|
-
assert.match(renderedLines(component).join("\n"), /<muted>Took \d+\.\ds<\/muted>/);
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
test("resolveToolRenderers: the image fallback follows context.showImages", () => {
|
|
294
|
-
// Both directions are only observable when the terminal can show images, so
|
|
295
|
-
// the capability is pinned for the duration of the test.
|
|
296
|
-
setCapabilityOverrides({ images: "kitty" });
|
|
297
|
-
try {
|
|
298
|
-
const result: HostResult = {
|
|
299
|
-
content: [
|
|
300
|
-
{ type: "text", text: "a screenshot" },
|
|
301
|
-
{ type: "image", data: "not-an-image", mimeType: "image/png" },
|
|
302
|
-
],
|
|
303
|
-
details: undefined,
|
|
304
|
-
};
|
|
305
|
-
const render = (showImages: boolean): string => {
|
|
306
|
-
const resolved = resolveToolRenderers("codemode", () => baseRenderersWithoutResult());
|
|
307
|
-
const component = resolved?.renderResult?.(
|
|
308
|
-
result,
|
|
309
|
-
{ expanded: false, isPartial: false },
|
|
310
|
-
fakeTheme,
|
|
311
|
-
hostContext({ isPartial: false, showImages }),
|
|
312
|
-
);
|
|
313
|
-
return renderedLines(component).join("\n");
|
|
314
|
-
};
|
|
315
|
-
// The exact wording belongs to the package; comparing against its own
|
|
316
|
-
// formatter keeps this test from pinning a format the host may change.
|
|
317
|
-
assert.equal(render(true).includes(imageFallback("image/png")), false);
|
|
318
|
-
assert.equal(render(false).includes(imageFallback("image/png")), true);
|
|
319
|
-
} finally {
|
|
320
|
-
setCapabilityOverrides({});
|
|
321
|
-
}
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
// The read and edit tools carry their own details; the renderers are the only
|
|
325
|
-
// consumers of each shape.
|
|
326
|
-
function readResult(text: string, details?: unknown): HostResult {
|
|
327
|
-
return { content: [{ type: "text", text }], details };
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
function editResult(diff: string): HostResult {
|
|
331
|
-
return { content: [{ type: "text", text: "" }], details: { diff } };
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
// The header paints the path through the link helper, which is only wrapped in
|
|
335
|
-
// OSC 8 when the terminal supports it, so the assertions look for the colored
|
|
336
|
-
// text instead of pinning the whole line.
|
|
337
|
-
function headerText(toolName: string, args: unknown): string {
|
|
338
|
-
const resolved = resolveToolRenderers(toolName, () => baseRenderers());
|
|
339
|
-
return renderedLines(resolved?.renderCall?.(args, fakeTheme, hostContext({ args }))).join("\n");
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
test("resolveToolRenderers: the read header names the tool and the path", () => {
|
|
343
|
-
const header = headerText("read", { path: "src/a.ts" });
|
|
344
|
-
assert.match(header, /<toolTitle><b>read<\/b><\/toolTitle>/);
|
|
345
|
-
assert.match(header, /<accent>src\/a\.ts<\/accent>/);
|
|
346
|
-
});
|
|
347
|
-
|
|
348
|
-
test("resolveToolRenderers: the write header previews the content and reports an invalid argument", () => {
|
|
349
|
-
const header = headerText("write", { path: "a.ts", content: "const a = 1;" });
|
|
350
|
-
assert.match(header, /<b>write<\/b>/);
|
|
351
|
-
assert.match(header, /const a = 1;/);
|
|
352
|
-
const invalid = headerText("write", { path: "a.ts", content: 42 });
|
|
353
|
-
assert.match(invalid, /\[invalid content arg - expected string\]/);
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
test("resolveToolRenderers: an expanded read numbers the body from the recorded offset", () => {
|
|
357
|
-
const resolved = resolveToolRenderers("read", () => baseRenderers());
|
|
358
|
-
const render = (args: unknown): string =>
|
|
359
|
-
renderedLines(
|
|
360
|
-
resolved?.renderResult?.(
|
|
361
|
-
readResult("const a = 1;\nconst b = 2;"),
|
|
362
|
-
{ expanded: true, isPartial: false },
|
|
363
|
-
fakeTheme,
|
|
364
|
-
hostContext({ isPartial: false, args }),
|
|
365
|
-
),
|
|
366
|
-
).join("\n");
|
|
367
|
-
const text = render({ path: "a.ts" });
|
|
368
|
-
assert.match(text, /<syntaxNumber>1<\/syntaxNumber><dim> \|<\/dim>/);
|
|
369
|
-
assert.match(text, /const a = 1;/);
|
|
370
|
-
// The offset moves the numbering itself, not only the header's range.
|
|
371
|
-
assert.match(render({ path: "a.ts", offset: 98 }), /<syntaxNumber>98<\/syntaxNumber>/);
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
test("resolveToolRenderers: a collapsed read with no failure renders no rows", () => {
|
|
375
|
-
const resolved = resolveToolRenderers("read", () => baseRenderers());
|
|
376
|
-
const component = resolved?.renderResult?.(
|
|
377
|
-
readResult("const a = 1;"),
|
|
378
|
-
{ expanded: false, isPartial: false },
|
|
379
|
-
fakeTheme,
|
|
380
|
-
hostContext({ isPartial: false, args: { path: "a.ts" } }),
|
|
381
|
-
);
|
|
382
|
-
assert.deepEqual(component?.render(40), []);
|
|
383
|
-
});
|
|
384
|
-
|
|
385
|
-
test("resolveToolRenderers: a read failure is painted flat even when collapsed", () => {
|
|
386
|
-
const resolved = resolveToolRenderers("read", () => baseRenderers());
|
|
387
|
-
const component = resolved?.renderResult?.(
|
|
388
|
-
readResult("denied"),
|
|
389
|
-
{ expanded: false, isPartial: false },
|
|
390
|
-
fakeTheme,
|
|
391
|
-
hostContext({ isPartial: false, args: { path: "a.ts" }, isError: true }),
|
|
392
|
-
);
|
|
393
|
-
const text = renderedLines(component).join("\n");
|
|
394
|
-
assert.match(text, /<toolOutput>denied<\/toolOutput>/);
|
|
395
|
-
assert.equal(text.includes("<syntaxNumber>"), false);
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
test("resolveToolRenderers: a write failure renders text and a success an empty container", () => {
|
|
399
|
-
const resolved = resolveToolRenderers("write", () => baseRenderers());
|
|
400
|
-
const render = (isError: boolean): Component | undefined =>
|
|
401
|
-
resolved?.renderResult?.(
|
|
402
|
-
readResult("permission denied"),
|
|
403
|
-
{ expanded: false, isPartial: false },
|
|
404
|
-
fakeTheme,
|
|
405
|
-
hostContext({ isPartial: false, isError }),
|
|
406
|
-
);
|
|
407
|
-
assert.match(renderedLines(render(true)).join("\n"), /<error>permission denied<\/error>/);
|
|
408
|
-
const ok = render(false);
|
|
409
|
-
assert.equal(ok instanceof Container, true);
|
|
410
|
-
const container = ok as Container;
|
|
411
|
-
assert.deepEqual(container.children, []);
|
|
412
|
-
assert.deepEqual(container.render(40), []);
|
|
413
|
-
});
|
|
414
|
-
|
|
415
|
-
test("resolveToolRenderers: edit fills the built-in box with the diff and leaves the result slot empty", () => {
|
|
416
|
-
// The built-in `renderCall` of edit builds this box and keeps it in the state;
|
|
417
|
-
// the header is its first child.
|
|
418
|
-
const box = new Box(1, 1, () => "");
|
|
419
|
-
box.addChild(new Text("edit src/a.ts", 0, 0));
|
|
420
|
-
const resolved = resolveToolRenderers("edit", () => baseRenderers());
|
|
421
|
-
const component = resolved?.renderResult?.(
|
|
422
|
-
editResult("-1 const a = 1;\n+1 const b = 2;"),
|
|
423
|
-
{ expanded: false, isPartial: false },
|
|
424
|
-
fakeTheme,
|
|
425
|
-
hostContext({ args: { path: "a.ts" }, isPartial: false, state: { callComponent: box } }),
|
|
426
|
-
);
|
|
427
|
-
assert.deepEqual(component?.render(40), []);
|
|
428
|
-
// The header survives, the gap and our body follow it. The bodies are matched
|
|
429
|
-
// by their markers: the word diff of a modification puts real inverse
|
|
430
|
-
// sequences inside the content, so the text is no longer contiguous there.
|
|
431
|
-
assert.equal(box.children.length, 3);
|
|
432
|
-
const rendered = box.render(40).join("\n");
|
|
433
|
-
assert.match(rendered, /edit src\/a\.ts/);
|
|
434
|
-
assert.match(rendered, /<toolDiffRemoved>-<\/toolDiffRemoved>/);
|
|
435
|
-
assert.match(rendered, /<toolDiffAdded>\+<\/toolDiffAdded>/);
|
|
436
|
-
// A settled success brings the box's own success background back.
|
|
437
|
-
assert.match(rendered, /<bg:toolSuccessBg>/);
|
|
438
|
-
});
|
|
439
|
-
|
|
440
|
-
test("resolveToolRenderers: an edit failure paints the box background red and the body in the error color", () => {
|
|
441
|
-
const box = new Box(1, 1, () => "");
|
|
442
|
-
box.addChild(new Text("edit src/a.ts", 0, 0));
|
|
443
|
-
const resolved = resolveToolRenderers("edit", () => baseRenderers());
|
|
444
|
-
const component = resolved?.renderResult?.(
|
|
445
|
-
readResult("no such file"),
|
|
446
|
-
{ expanded: false, isPartial: false },
|
|
447
|
-
fakeTheme,
|
|
448
|
-
hostContext({
|
|
449
|
-
args: { path: "a.ts" },
|
|
450
|
-
isPartial: false,
|
|
451
|
-
isError: true,
|
|
452
|
-
state: { callComponent: box },
|
|
453
|
-
}),
|
|
454
|
-
);
|
|
455
|
-
assert.deepEqual(component?.render(40), []);
|
|
456
|
-
const rendered = box.render(40).join("\n");
|
|
457
|
-
assert.match(rendered, /<bg:toolErrorBg>/);
|
|
458
|
-
assert.match(rendered, /<error>no such file<\/error>/);
|
|
459
|
-
});
|
|
460
|
-
|
|
461
|
-
test("resolveToolRenderers: edit without a box renders the diff below the header", () => {
|
|
462
|
-
const resolved = resolveToolRenderers("edit", () => baseRenderers());
|
|
463
|
-
const component = resolved?.renderResult?.(
|
|
464
|
-
editResult("-1 const a = 1;"),
|
|
465
|
-
{ expanded: false, isPartial: false },
|
|
466
|
-
fakeTheme,
|
|
467
|
-
hostContext({ args: { path: "a.ts" }, isPartial: false, outputPad: 2 }),
|
|
468
|
-
);
|
|
469
|
-
assert.equal(component instanceof Container, true);
|
|
470
|
-
const rendered = renderedLines(component as Container);
|
|
471
|
-
assert.equal(rendered.length, 2);
|
|
472
|
-
// The body is padded by the value the row's own renderers use.
|
|
473
|
-
assert.match(rendered[1], /^ .*const a = 1;/);
|
|
474
|
-
});
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { test } from "node:test";
|
|
2
|
-
import assert from "node:assert/strict";
|
|
3
|
-
import { classifyPathLine, stylePath } from "../../src/style-path.ts";
|
|
4
|
-
import { splitExtension } from "../../src/tokens.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
|
-
test("stylePath: every segment but the last is dimmed, the last takes the type color in bold", () => {
|
|
13
|
-
assert.equal(
|
|
14
|
-
stylePath("src/deep/a.ts", fakeTheme),
|
|
15
|
-
"<muted>src</muted>/<muted>deep</muted>/<b><syntaxType>a.ts</syntaxType></b>",
|
|
16
|
-
);
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test("stylePath: a document name is colored like tool output", () => {
|
|
20
|
-
assert.equal(stylePath("README.md", fakeTheme), "<b><toolOutput>README.md</toolOutput></b>");
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test("stylePath: build files are colored as functions, dotfiles have no extension", () => {
|
|
24
|
-
assert.equal(stylePath("Makefile", fakeTheme), "<b><syntaxFunction>Makefile</syntaxFunction></b>");
|
|
25
|
-
assert.equal(stylePath("src/build.mk", fakeTheme), "<muted>src</muted>/<b><syntaxFunction>build.mk</syntaxFunction></b>");
|
|
26
|
-
assert.equal(stylePath(".gitignore", fakeTheme), "<b><toolOutput>.gitignore</toolOutput></b>");
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test("stylePath: only the extension after the last dot counts", () => {
|
|
30
|
-
assert.equal(splitExtension("a.b.c.ts"), "ts");
|
|
31
|
-
assert.equal(splitExtension(".gitignore"), undefined);
|
|
32
|
-
assert.equal(splitExtension("Makefile"), undefined);
|
|
33
|
-
assert.equal(stylePath("a.b.c.ts", fakeTheme), "<b><syntaxType>a.b.c.ts</syntaxType></b>");
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test("stylePath: spaces in a directory segment are preserved verbatim", () => {
|
|
37
|
-
assert.equal(
|
|
38
|
-
stylePath("my docs/notes.md", fakeTheme),
|
|
39
|
-
"<muted>my docs</muted>/<b><toolOutput>notes.md</toolOutput></b>",
|
|
40
|
-
);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test("stylePath: a directory's own name is dimmed and bold, and keeps its slash", () => {
|
|
44
|
-
assert.equal(stylePath("dir/", fakeTheme), "<b><dim>dir</dim></b>/");
|
|
45
|
-
assert.equal(stylePath("src/sub/", fakeTheme), "<muted>src</muted>/<b><dim>sub</dim></b>/");
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
test("stylePath: an entry name ls prints carries the whole weight, and a directory is dimmed and bold", () => {
|
|
49
|
-
assert.equal(stylePath("a.ts", fakeTheme), "<b><syntaxType>a.ts</syntaxType></b>");
|
|
50
|
-
assert.equal(stylePath("notes", fakeTheme), "<b><toolOutput>notes</toolOutput></b>");
|
|
51
|
-
assert.equal(stylePath("dir/", fakeTheme), "<b><dim>dir</dim></b>/");
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test("classifyPathLine: sentinels and blank lines are plain", () => {
|
|
55
|
-
for (const line of [
|
|
56
|
-
"No matches found",
|
|
57
|
-
"No files found matching pattern",
|
|
58
|
-
"(empty directory)",
|
|
59
|
-
"",
|
|
60
|
-
" ",
|
|
61
|
-
]) {
|
|
62
|
-
assert.deepEqual(classifyPathLine(line), { kind: "plain", text: line });
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test("classifyPathLine: a bracketed notice line is its own kind", () => {
|
|
67
|
-
const notice = "[50 entries limit reached. Use limit=100 for more]";
|
|
68
|
-
assert.deepEqual(classifyPathLine(notice), { kind: "notice", text: notice });
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test("classifyPathLine: a trailing slash is a directory, anything else a file", () => {
|
|
72
|
-
assert.deepEqual(classifyPathLine("src/a.ts"), { kind: "file", path: "src/a.ts" });
|
|
73
|
-
assert.deepEqual(classifyPathLine("src/"), { kind: "dir", path: "src/" });
|
|
74
|
-
});
|
package/tsconfig.json
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "NodeNext",
|
|
5
|
-
"moduleResolution": "NodeNext",
|
|
6
|
-
"esModuleInterop": true,
|
|
7
|
-
"allowImportingTsExtensions": true,
|
|
8
|
-
"strict": true,
|
|
9
|
-
"skipLibCheck": true,
|
|
10
|
-
"outDir": "./dist",
|
|
11
|
-
"declaration": true,
|
|
12
|
-
"noEmit": true
|
|
13
|
-
},
|
|
14
|
-
"include": ["src/**/*", "extensions/**/*", "test/**/*"]
|
|
15
|
-
}
|