Skip to content

Commit 384a055

Browse files
committed
refactor: Separate out functions from generateHtml
- getDecorations - renderClipboard - renderClipboardAndError - renderError
1 parent 6cd9859 commit 384a055

File tree

7 files changed

+66
-60
lines changed

7 files changed

+66
-60
lines changed

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

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { createHighlighter } from "./createHighlighter";
2-
import type { BundledLanguage, DecorationItem } from "shiki";
3-
import type { StepNameType, ExtendedTestCaseSnapshot, DataFixture } from "./types";
4-
import type { Command } from "@cursorless/common";
2+
import type { BundledLanguage } from "shiki";
3+
import type { StepNameType, DataFixture } from "./types";
54

6-
import { createDecorations } from "./helpers";
5+
import { renderClipboard, renderError } from "./renderHtml";
6+
import { getDecorations } from "./helpers/decorations";
77

88
/**
99
* Generates HTML content based on the provided state, language, command, and ide.
@@ -15,62 +15,6 @@ export async function generateHtml(data: DataFixture) {
1515
return createHtmlGenerator(data).generateAll();
1616
}
1717

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-
}
73-
7418
/**
7519
* Closure-based HTML generator for test case data.
7620
*
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Computes code decorations for a given test case state.
3+
*
4+
* @param {ExtendedTestCaseSnapshot} testCaseState - The test case state to decorate.
5+
* @param {Command} command - The command object for the test case.
6+
* @returns {Promise<DecorationItem[][]>} The computed decorations for the state.
7+
*/
8+
import type { ExtendedTestCaseSnapshot } from "../../types";
9+
import type { Command } from "@cursorless/common";
10+
import type { DecorationItem } from "shiki";
11+
import { createDecorations } from "../index";
12+
13+
export async function getDecorations(
14+
testCaseState: ExtendedTestCaseSnapshot,
15+
command: Command
16+
): Promise<DecorationItem[][]> {
17+
const { messages, flashes, highlights, finalStateMarkHelpers } = testCaseState;
18+
const potentialMarks = testCaseState.marks || {};
19+
const lines = testCaseState.documentContents.split("\n");
20+
const obj = {
21+
marks: potentialMarks,
22+
ide: { messages, flashes, highlights },
23+
command,
24+
lines,
25+
selections: testCaseState.selections,
26+
thatMark: testCaseState.thatMark,
27+
sourceMark: testCaseState.sourceMark,
28+
finalStateMarkHelpers
29+
};
30+
const decorations = createDecorations(obj);
31+
return decorations;
32+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./getDecorations";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./renderClipboard";
2+
export * from "./renderError";
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Renders the clipboard HTML if clipboard content exists.
3+
*
4+
* @param {string | undefined} clipboard - The clipboard string or undefined.
5+
* @returns {string} The HTML string for the clipboard, or an empty string if clipboard is undefined.
6+
*/
7+
export function renderClipboard(clipboard: string | undefined): string {
8+
if (!clipboard) {
9+
return "";
10+
}
11+
return `<pre><code>clipboard: ${clipboard}</pre></code>`;
12+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Remove the old renderClipboardAndError.ts file since the functions are now in their own files
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Renders the error HTML if an error occurred.
3+
*
4+
* @param {number} errorLevel - The error level index.
5+
* @param {string[]} errorLevels - The array of error level descriptions.
6+
* @returns {string} The HTML string for the error, or an empty string if no error.
7+
*/
8+
export function renderError(errorLevel: number, errorLevels: string[]): string {
9+
if (errorLevel === errorLevels.length - 1) {
10+
return "";
11+
}
12+
const error = errorLevels[errorLevel];
13+
return `<pre><code>Omitted due to errors: ${error}</pre></code>`;
14+
}

0 commit comments

Comments
 (0)