Skip to content

Commit adb02ed

Browse files
committed
highlight text for analyze complexity, format code as its generated
1 parent 76ea490 commit adb02ed

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/popup/popup.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ function timeout(ms: number): Promise<never> {
107107
);
108108
}
109109

110+
function formatResponseText(text: string): string {
111+
return text
112+
.replace(/time/gi, '<span style="color: lightgreen;">time complexity</span>')
113+
.replace(/space/gi, '<span style="color: lightgreen;">space complexity</span>');
114+
}
115+
110116
function processCode(
111117
chatGPTProvider: ChatGPTProvider,
112118
codeText: string,
@@ -154,21 +160,28 @@ function processCode(
154160
prompt: prompt,
155161
onEvent: (event: { type: string; data?: { text: string } }) => {
156162
if (event.type === 'answer' && event.data) {
157-
response += event.data.text;
158163
if (action === 'fix' && fixCodeResponse) {
164+
response += event.data.text;
159165
fixCodeResponse.textContent = response;
166+
(window as any).Prism.highlightAll();
167+
160168
}
161169
else if (action === 'analyze' && analyzeCodeResponse) {
162-
analyzeCodeResponse.textContent = response;
170+
response += formatResponseText(event.data.text); // Use the helper function here
171+
analyzeCodeResponse.innerHTML = response;
163172
}
164173
}
165174
if (event.type === 'done') {
166-
analyzeCodeResponse && chrome.storage.local.set({ 'analyzeCodeResponse': analyzeCodeResponse.textContent });
167-
fixCodeResponse && chrome.storage.local.set({ 'fixCodeResponse': fixCodeResponse.textContent });
175+
disableAllButtons(false);
168176
chrome.storage.local.set({ 'lastAction': action });
169177
infoMessage && (infoMessage.textContent = problemTitle);
170-
disableAllButtons(false);
171-
(window as any).Prism.highlightAll();
178+
if (action === 'fix') {
179+
fixCodeResponse && chrome.storage.local.set({ 'fixCodeResponse': fixCodeResponse.textContent });
180+
(window as any).Prism.highlightAll();
181+
}
182+
if (action === 'analyze') {
183+
analyzeCodeResponse && chrome.storage.local.set({ 'analyzeCodeResponse': analyzeCodeResponse.innerHTML });
184+
}
172185
}
173186
},
174187
}),
@@ -209,7 +222,7 @@ async function main(): Promise<void> {
209222

210223
chrome.storage.local.get('analyzeCodeResponse', function (data) {
211224
if (data.analyzeCodeResponse) {
212-
analyzeCodeResponse && (analyzeCodeResponse.textContent = data.analyzeCodeResponse);
225+
analyzeCodeResponse && (analyzeCodeResponse.innerHTML = data.analyzeCodeResponse);
213226
(window as any).Prism.highlightAll();
214227
}
215228
});

0 commit comments

Comments
 (0)