mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-26 02:05:31 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f2b7ac1f2b | ||
|
|
8b8f405d2e |
@@ -2,11 +2,23 @@ import { highlightCode, type HighlightToken } from "@getpaseo/highlight";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildSourceLines } from "./source-lines";
|
||||
|
||||
const records = Array.from({ length: 900 }, (_, i) => ({
|
||||
t: `comment number ${i} with some text`,
|
||||
l: i % 7,
|
||||
r: i % 3,
|
||||
u: `user${i}`,
|
||||
}));
|
||||
|
||||
const large = Array.from({ length: 14000 }, (_, i) => `export const item${i} = ${i};`).join("\n");
|
||||
|
||||
/** Collects every report until `count` have arrived. */
|
||||
function build(
|
||||
input: { presentation: "highlighted" | "plain"; content?: string; firstLines?: number },
|
||||
input: {
|
||||
presentation: "highlighted" | "plain";
|
||||
content?: string;
|
||||
filename?: string;
|
||||
firstLines?: number;
|
||||
},
|
||||
count = 1,
|
||||
) {
|
||||
return new Promise<HighlightToken[][][]>((resolve) => {
|
||||
@@ -14,7 +26,7 @@ function build(
|
||||
buildSourceLines(
|
||||
{
|
||||
content: input.content ?? large,
|
||||
filename: "large.ts",
|
||||
filename: input.filename ?? "large.ts",
|
||||
presentation: input.presentation,
|
||||
firstLines: input.firstLines ?? 0,
|
||||
},
|
||||
@@ -50,6 +62,63 @@ describe("buildSourceLines", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("reports the supplied single-line JSON as one complete plain token", async () => {
|
||||
const content = JSON.stringify(records);
|
||||
const filename = "one-line-repro.json";
|
||||
const [[line]] = await build({ content, filename, presentation: "highlighted" });
|
||||
|
||||
expect(new TextEncoder().encode(content)).toHaveLength(60981);
|
||||
expect(line).toHaveLength(1);
|
||||
expect(line[0]).toEqual({ text: content, style: null });
|
||||
});
|
||||
|
||||
it("keeps the pretty-printed control highlighted", async () => {
|
||||
const content = JSON.stringify(records, null, 2);
|
||||
const filename = "pretty-repro.json";
|
||||
const [lines] = await build({ content, filename, presentation: "highlighted" });
|
||||
|
||||
expect(lines).toHaveLength(5402);
|
||||
expect(lines).toEqual(highlightCode(content, filename));
|
||||
expect(lines.flat().some((token) => token.style !== null)).toBe(true);
|
||||
});
|
||||
|
||||
it("falls back for a dense row in the preview and full result", async () => {
|
||||
const filename = "data.json";
|
||||
const dense = `${JSON.stringify(Array.from({ length: 25 }, (_, i) => i))} `;
|
||||
const content = `${dense}\n{"ok":true}\n{"ok":false}\n`;
|
||||
const original = highlightCode(content, filename);
|
||||
expect(original[0]).toHaveLength(52);
|
||||
const [preview, full] = await build(
|
||||
{ content, filename, presentation: "highlighted", firstLines: 1 },
|
||||
2,
|
||||
);
|
||||
|
||||
expect(preview[0]).toEqual([{ text: dense, style: null }]);
|
||||
expect(full).toEqual([[{ text: dense, style: null }], ...original.slice(1)]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["data.json", JSON.stringify(Array.from({ length: 25 }, (_, i) => i)), 51],
|
||||
[
|
||||
"ordinary.ts",
|
||||
'const values = [{ id: 0, name: "item0" }, { id: 1, name: "item1" }, { id: 2, name: "item2" }];',
|
||||
50,
|
||||
],
|
||||
])("keeps %s highlighted within the span budget", async (filename, content, tokenCount) => {
|
||||
const original = highlightCode(content, filename);
|
||||
const [lines] = await build({ content, filename, presentation: "highlighted" });
|
||||
|
||||
expect(original[0]).toHaveLength(tokenCount);
|
||||
expect(lines[0]).toHaveLength(tokenCount);
|
||||
expect(lines).toEqual(original);
|
||||
});
|
||||
|
||||
it("preserves every line for unknown languages", async () => {
|
||||
const content = "hello\r\n\nworld\n";
|
||||
const [lines] = await build({ content, filename: "data.unknown", presentation: "highlighted" });
|
||||
expect(lines).toEqual(content.split("\n").map((text) => [{ text, style: null }]));
|
||||
});
|
||||
|
||||
it("never reports lines once cancelled", async () => {
|
||||
let reported = false;
|
||||
const cancel = buildSourceLines(
|
||||
|
||||
@@ -3,6 +3,10 @@ import { highlightCodeInSteps, type HighlightToken } from "@getpaseo/highlight";
|
||||
/** JavaScript time per slice; the rest of the frame stays free for input and rendering. */
|
||||
const SLICE_BUDGET_MS = 12;
|
||||
const PLAIN_LINES_PER_UNIT = 2000;
|
||||
// Android has 256 span-priority slots. A colored source fragment uses five:
|
||||
// color, size, font family, line height, and the fragment marker. At most 51
|
||||
// tokens fit even when every token has a color.
|
||||
const MAX_HIGHLIGHTED_TOKENS_PER_LINE = 51;
|
||||
|
||||
/**
|
||||
* Splits `content` into source lines, highlighted or plain, a time slice per task so a large file
|
||||
@@ -28,16 +32,21 @@ export function buildSourceLines(
|
||||
do {
|
||||
const step = steps.next();
|
||||
if (step.done) {
|
||||
onLines(step.value);
|
||||
onLines(step.value.map(limitRowTokens));
|
||||
return;
|
||||
}
|
||||
if (step.value) onLines(step.value);
|
||||
if (step.value) onLines(step.value.map(limitRowTokens));
|
||||
} while (performance.now() < deadline);
|
||||
cancelSlice = scheduleSlice(runSlice);
|
||||
}
|
||||
return () => cancelSlice();
|
||||
}
|
||||
|
||||
function limitRowTokens(tokens: HighlightToken[]): HighlightToken[] {
|
||||
if (tokens.length <= MAX_HIGHLIGHTED_TOKENS_PER_LINE) return tokens;
|
||||
return [{ text: tokens.map((token) => token.text).join(""), style: null }];
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs `slice` once the JavaScript thread is idle. React Native runs idle callbacks after pending
|
||||
* events and renders without waiting for a frame; a zero timeout waits for the next frame.
|
||||
|
||||
Reference in New Issue
Block a user