Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { embeddedScripts } from "../../lib/embeddedScripts";
4import { keyedLines, withEmbeddedScripts, wordsOf } from "./HighlightedTokens";
5
6function tokensOf(...contents: string[]) {
7 let offset = 0;
8 return contents.map((content) => {
9 const token = { content, offset };
10 offset += content.length;
11 return token;
12 });
13}
14
15describe("wordsOf", () => {
16 it("keeps each piece's own token so colors survive regrouping", () => {
17 const [word] = wordsOf(tokensOf('"', "$HOME", '"'));
18 expect(
19 typeof word === "string" ? null : word?.pieces.map((piece) => piece.token.content),
20 ).toEqual(['"', "$HOME", '"']);
21 });
22});
23
24describe("keyedLines", () => {
25 it.each([
26 "git status\ngit diff",
27 "Get-ChildItem\r\nWrite-Output done\r\n",
28 "mixed\r\nendings\n\nlone \r stays",
29 ])("renders the same text as the source: %j", (code) => {
30 // Shiki splits lines on \r?\n and drops the ending from the tokens.
31 const lines = code.split(/\r?\n/u).map((line) => tokensOf(line));
32 const rendered = keyedLines(code, lines)
33 .map(({ tokens, ending }) => tokens.map((token) => token.content).join("") + ending)
34 .join("");
35 expect(rendered).toBe(code);
36 });
37});
38
39describe("withEmbeddedScripts", () => {
40 function tokenize(text: string, language: string) {
41 if (language === "unknown") throw new Error("Language not found");
42 let lineStart = 0;
43 return text.split(/\r?\n/u).map((line) => {
44 const tokens = [...line.matchAll(/\S+|\s+/gu)].map((match) => ({
45 content: match[0],
46 offset: lineStart + match.index,
47 color: language,
48 }));
49 lineStart +=
50 line.length + (text.slice(lineStart + line.length).match(/^\r?\n/u)?.[0].length ?? 0);
51 return tokens;
52 });
53 }
54
55 function colorsOf(code: string, scripts = embeddedScripts(code)) {
56 const lines = withEmbeddedScripts(code, tokenize(code, "shellscript"), scripts, tokenize);
57 expect(lines.map((line) => line.map((token) => token.content).join("")).join("\n")).toBe(
58 code.replaceAll("\r\n", "\n"),
59 );
60 return lines.flat().map((token) => [token.content, token.color]);
61 }
62
63 it("colors each nested script with its own grammar, keeping the text", () => {
64 expect(colorsOf(`bash -lc "python3 -c 'print(1)'"`)).toEqual([
65 [`bash -lc "python3 -c '`, "shellscript"],
66 ["print(1)", "python"],
67 [`'"`, "shellscript"],
68 ]);
69 });
70
71 it("colors an escape sequence like the character it spells", () => {
72 expect(colorsOf(`bash -c "python3 -c 'print(\\"x\\")'"`)).toContainEqual([
73 `print(\\"x\\")`,
74 "python",
75 ]);
76 });
77
78 it("keeps the surrounding colors where a grammar is missing", () => {
79 const code = "cat > x.ts <<'EOF'\r\nbody\r\nEOF";
80 const [script] = embeddedScripts(code);
81 const characterColors = (scripts: ReturnType<typeof embeddedScripts>) =>
82 colorsOf(code, scripts).flatMap(([content, color]) => [...content!].map(() => color));
83 expect(characterColors([{ ...script!, language: "unknown" }])).toEqual(characterColors([]));
84 });
85});