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 const lines = code.split(/\r?\n/u).map((line) => tokensOf(line));
31 const rendered = keyedLines(code, lines)
32 .map(({ tokens, ending }) => tokens.map((token) => token.content).join("") + ending)
33 .join("");
34 expect(rendered).toBe(code);
35 });
36});
37
38describe("withEmbeddedScripts", () => {
39 function tokenize(text: string, language: string) {
40 if (language === "unknown") throw new Error("Language not found");
41 let lineStart = 0;
42 return text.split(/\r?\n/u).map((line) => {
43 const tokens = [...line.matchAll(/\S+|\s+/gu)].map((match) => ({
44 content: match[0],
45 offset: lineStart + match.index,
46 color: language,
47 }));
48 lineStart +=
49 line.length + (text.slice(lineStart + line.length).match(/^\r?\n/u)?.[0].length ?? 0);
50 return tokens;
51 });
52 }
53
54 function colorsOf(code: string, scripts = embeddedScripts(code)) {
55 const lines = withEmbeddedScripts(code, tokenize(code, "shellscript"), scripts, tokenize);
56 expect(lines.map((line) => line.map((token) => token.content).join("")).join("\n")).toBe(
57 code.replaceAll("\r\n", "\n"),
58 );
59 return lines.flat().map((token) => [token.content, token.color]);
60 }
61
62 it("colors each nested script with its own grammar, keeping the text", () => {
63 expect(colorsOf(`bash -lc "python3 -c 'print(1)'"`)).toEqual([
64 [`bash -lc "python3 -c '`, "shellscript"],
65 ["print(1)", "python"],
66 [`'"`, "shellscript"],
67 ]);
68 });
69
70 it("colors an escape sequence like the character it spells", () => {
71 expect(colorsOf(`bash -c "python3 -c 'print(\\"x\\")'"`)).toContainEqual([
72 `print(\\"x\\")`,
73 "python",
74 ]);
75 });
76
77 it("keeps the surrounding colors where a grammar is missing", () => {
78 const code = "cat > x.ts <<'EOF'\r\nbody\r\nEOF";
79 const [script] = embeddedScripts(code);
80 const characterColors = (scripts: ReturnType<typeof embeddedScripts>) =>
81 colorsOf(code, scripts).flatMap(([content, color]) => [...content!].map(() => color));
82 expect(characterColors([{ ...script!, language: "unknown" }])).toEqual(characterColors([]));
83 });
84});