Skip to content

Commit ce4d256

Browse files
committed
feat: update code on reset
1 parent 1481d68 commit ce4d256

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/utils/editor.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class Editor {
1717
private onChangeCallback?: (editor: Editor) => void;
1818

1919
// Data associated with TextEditor
20+
prepend: string;
2021
uri: string;
2122

2223
// Metadata relating to this question
@@ -26,12 +27,14 @@ export class Editor {
2627

2728
private constructor(
2829
editor: vscode.TextEditor,
30+
prepend: string,
2931
uri: string,
3032
workspaceLocation: VscWorkspaceLocation,
3133
assessmentName: string,
3234
questionId: number,
3335
) {
3436
this.editor = editor;
37+
this.prepend = prepend;
3538
this.uri = uri;
3639
this.workspaceLocation = workspaceLocation;
3740
this.assessmentName = assessmentName;
@@ -123,6 +126,7 @@ export class Editor {
123126
// Create wrapper
124127
const self = new Editor(
125128
editor,
129+
prepend,
126130
uri.toString(),
127131
workspaceLocation,
128132
assessmentName,
@@ -146,16 +150,18 @@ export class Editor {
146150
return self;
147151
}
148152

149-
async replace(code: string, tag: string = "") {
153+
async replace(code: string) {
150154
const editor = this.editor;
151-
// Don't replace if the code is the same
155+
const contents = codeAddPrepend(code, this.prepend);
156+
157+
// In some sense, simulate a select all and paste
152158
editor.edit((editBuilder) => {
153159
editBuilder.replace(
154160
new vscode.Range(
155161
editor.document.positionAt(0),
156162
editor.document.positionAt(editor.document.getText().length),
157163
),
158-
code,
164+
contents,
159165
);
160166
});
161167
}

src/utils/messageHandler.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ export class MessageHandler {
167167
context.globalState.update("courseId", courseId);
168168
treeDataProvider.refresh();
169169
break;
170+
case MessageTypeNames.ResetEditor:
171+
if (this.activeEditor) {
172+
this.activeEditor.replace(message.initialCode);
173+
this.panel?.reveal(vscode.ViewColumn.Two);
174+
}
175+
break;
170176
}
171177
console.log(`${Date.now()} Finish handleMessage: ${message.type}`);
172178
}

src/utils/messages.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ const Messages = createMessages({
4848
EvalEditor: (workspaceLocation: VscWorkspaceLocation) => ({
4949
workspaceLocation: workspaceLocation,
5050
}),
51+
ResetEditor: (
52+
workspaceLocation: VscWorkspaceLocation,
53+
initialCode: string,
54+
) => ({
55+
workspaceLocation,
56+
initialCode,
57+
}),
5158
NotifyAssessmentsOverview: (
5259
assessmentOverviews: VscAssessmentOverview[],
5360
courseId: number,

0 commit comments

Comments
 (0)