Skip to content

Commit 6cd9859

Browse files
committed
refactor: Extract and type utility functions in generateHtml
- Move renderClipboard, renderError, and getDecorations out of the closure - Minor code cleanup and improved type safety for decoration handling
1 parent d4f2f1a commit 6cd9859

File tree

1 file changed

+69
-40
lines changed

1 file changed

+69
-40
lines changed

packages/test-case-component/src/generateHtml.ts

Lines changed: 69 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createHighlighter } from "./createHighlighter";
2-
import type { BundledLanguage } from "shiki";
2+
import type { BundledLanguage, DecorationItem } from "shiki";
33
import type { StepNameType, ExtendedTestCaseSnapshot, DataFixture } from "./types";
4+
import type { Command } from "@cursorless/common";
45

56
import { createDecorations } from "./helpers";
67

@@ -14,7 +15,61 @@ export async function generateHtml(data: DataFixture) {
1415
return createHtmlGenerator(data).generateAll();
1516
}
1617

17-
const highlighter = createHighlighter();
18+
/**
19+
* Renders the clipboard HTML if clipboard content exists.
20+
*
21+
* @param {string | undefined} clipboard - The clipboard string or undefined.
22+
* @returns {string} The HTML string for the clipboard, or an empty string if clipboard is undefined.
23+
*/
24+
function renderClipboard(clipboard: string | undefined): string {
25+
if (!clipboard) {
26+
return "";
27+
}
28+
return `<pre><code>clipboard: ${clipboard}</pre></code>`;
29+
}
30+
31+
/**
32+
* Renders the error HTML if an error occurred.
33+
*
34+
* @param {number} errorLevel - The error level index.
35+
* @param {string[]} errorLevels - The array of error level descriptions.
36+
* @returns {string} The HTML string for the error, or an empty string if no error.
37+
*/
38+
function renderError(errorLevel: number, errorLevels: string[]): string {
39+
if (errorLevel === errorLevels.length - 1) {
40+
return "";
41+
}
42+
const error = errorLevels[errorLevel];
43+
return `<pre><code>Omitted due to errors: ${error}</pre></code>`;
44+
}
45+
46+
/**
47+
* Computes code decorations for a given test case state.
48+
*
49+
* @param {ExtendedTestCaseSnapshot} testCaseState - The test case state to decorate.
50+
* @param {Command} command - The command object for the test case.
51+
* @returns {Promise<DecorationItem[][]>} The computed decorations for the state.
52+
*/
53+
async function getDecorations(
54+
testCaseState: ExtendedTestCaseSnapshot,
55+
command: Command
56+
): Promise<DecorationItem[][]> {
57+
const { messages, flashes, highlights, finalStateMarkHelpers } = testCaseState;
58+
const potentialMarks = testCaseState.marks || {};
59+
const lines = testCaseState.documentContents.split("\n");
60+
const obj = {
61+
marks: potentialMarks,
62+
ide: { messages, flashes, highlights },
63+
command,
64+
lines,
65+
selections: testCaseState.selections,
66+
thatMark: testCaseState.thatMark,
67+
sourceMark: testCaseState.sourceMark,
68+
finalStateMarkHelpers
69+
};
70+
const decorations = createDecorations(obj);
71+
return decorations;
72+
}
1873

1974
/**
2075
* Closure-based HTML generator for test case data.
@@ -60,7 +115,7 @@ function createHtmlGenerator(data: DataFixture) {
60115
console.error(`Error in ${stepName} ${raw.command.spokenForm}`);
61116
return "Error";
62117
}
63-
const decorations = await getDecorations(state);
118+
const decorations = await getDecorations(state, command);
64119
const { documentContents } = state;
65120
const htmlArray: string[] = [];
66121
let codeBody;
@@ -76,7 +131,7 @@ function createHtmlGenerator(data: DataFixture) {
76131
const fallbackDecoration = decorations.slice(0, i).flat();
77132
errorLevel = i;
78133
try {
79-
const marker = await highlighter;
134+
const marker = await createHighlighter();
80135
const options = {
81136
theme: "css-variables",
82137
lang,
@@ -94,26 +149,24 @@ function createHtmlGenerator(data: DataFixture) {
94149
console.error("All fallback levels failed. Unable to generate code body.");
95150
codeBody = "";
96151
}
97-
let clipboardRendered = "";
98-
if (state.clipboard) {
99-
clipboardRendered = `<pre><code>clipboard: ${state.clipboard}</pre></code>`;
100-
if (clipboardRendered !== "") {
101-
htmlArray.push(clipboardRendered);
102-
}
152+
153+
const clipboardRendered = renderClipboard(state.clipboard);
154+
if (clipboardRendered !== "") {
155+
htmlArray.push(clipboardRendered);
103156
}
104-
let error = "";
105-
if (errorLevel !== errorLevels.length - 1) {
106-
error = errorLevels[errorLevel];
107-
const errorRendered = `<pre><code>Omitted due to errors: ${error}</pre></code>`;
157+
158+
const errorRendered = renderError(errorLevel, errorLevels);
159+
if (errorRendered !== "") {
108160
htmlArray.push(errorRendered);
109161
}
162+
110163
return { html: htmlArray.join(""), data: [decorations] };
111164
}
112165

113166
/**
114167
* Generates HTML for all test case steps (before, during, after).
115168
*
116-
* @returns {Promise<{ before: any; during: any; after: any }>} The generated HTML and decoration data for each step.
169+
* @returns {Promise<{ before: string; during: string; after: string }>} The generated HTML and decoration data for each step.
117170
*/
118171
async function generateAll() {
119172
return {
@@ -123,29 +176,5 @@ function createHtmlGenerator(data: DataFixture) {
123176
};
124177
}
125178

126-
/**
127-
* Computes code decorations for a given test case state.
128-
*
129-
* @param {ExtendedTestCaseSnapshot} testCaseState - The test case state to decorate.
130-
* @returns {Promise<Decoration[]>} The computed decorations for the state.
131-
*/
132-
async function getDecorations(testCaseState: ExtendedTestCaseSnapshot) {
133-
const { messages, flashes, highlights, finalStateMarkHelpers } = testCaseState;
134-
const potentialMarks = testCaseState.marks || {};
135-
const lines = testCaseState.documentContents.split("\n");
136-
const obj = {
137-
marks: potentialMarks,
138-
ide: { messages, flashes, highlights },
139-
command,
140-
lines,
141-
selections: testCaseState.selections,
142-
thatMark: testCaseState.thatMark,
143-
sourceMark: testCaseState.sourceMark,
144-
finalStateMarkHelpers
145-
};
146-
const decorations = createDecorations(obj);
147-
return decorations;
148-
}
149-
150-
return { generate, generateAll, getDecorations };
179+
return { generate, generateAll };
151180
}

0 commit comments

Comments
 (0)