1
1
import { createHighlighter } from "./createHighlighter" ;
2
- import type { BundledLanguage } from "shiki" ;
2
+ import type { BundledLanguage , DecorationItem } from "shiki" ;
3
3
import type { StepNameType , ExtendedTestCaseSnapshot , DataFixture } from "./types" ;
4
+ import type { Command } from "@cursorless/common" ;
4
5
5
6
import { createDecorations } from "./helpers" ;
6
7
@@ -14,7 +15,61 @@ export async function generateHtml(data: DataFixture) {
14
15
return createHtmlGenerator ( data ) . generateAll ( ) ;
15
16
}
16
17
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
+ }
18
73
19
74
/**
20
75
* Closure-based HTML generator for test case data.
@@ -60,7 +115,7 @@ function createHtmlGenerator(data: DataFixture) {
60
115
console . error ( `Error in ${ stepName } ${ raw . command . spokenForm } ` ) ;
61
116
return "Error" ;
62
117
}
63
- const decorations = await getDecorations ( state ) ;
118
+ const decorations = await getDecorations ( state , command ) ;
64
119
const { documentContents } = state ;
65
120
const htmlArray : string [ ] = [ ] ;
66
121
let codeBody ;
@@ -76,7 +131,7 @@ function createHtmlGenerator(data: DataFixture) {
76
131
const fallbackDecoration = decorations . slice ( 0 , i ) . flat ( ) ;
77
132
errorLevel = i ;
78
133
try {
79
- const marker = await highlighter ;
134
+ const marker = await createHighlighter ( ) ;
80
135
const options = {
81
136
theme : "css-variables" ,
82
137
lang,
@@ -94,26 +149,24 @@ function createHtmlGenerator(data: DataFixture) {
94
149
console . error ( "All fallback levels failed. Unable to generate code body." ) ;
95
150
codeBody = "" ;
96
151
}
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 ) ;
103
156
}
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 !== "" ) {
108
160
htmlArray . push ( errorRendered ) ;
109
161
}
162
+
110
163
return { html : htmlArray . join ( "" ) , data : [ decorations ] } ;
111
164
}
112
165
113
166
/**
114
167
* Generates HTML for all test case steps (before, during, after).
115
168
*
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.
117
170
*/
118
171
async function generateAll ( ) {
119
172
return {
@@ -123,29 +176,5 @@ function createHtmlGenerator(data: DataFixture) {
123
176
} ;
124
177
}
125
178
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 } ;
151
180
}
0 commit comments