Skip to content
10 changes: 10 additions & 0 deletions src/utils/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ export class Editor {
return this.editor.document.getText();
}

static getFilePath(assessmentName: string, questionId: number): string {
const workspaceFolder = canonicaliseLocation(config.workspaceFolder);
const filePath = path.join(
workspaceFolder,
`${assessmentName}_${questionId}.js`,
);

return filePath;
}

// TODO: This method is too loaded, it's not obvious it also shows the editor
static async create(
workspaceLocation: VscWorkspaceLocation,
Expand Down
18 changes: 18 additions & 0 deletions src/utils/messageHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ export class MessageHandler {
context.globalState.update("courseId", courseId);
treeDataProvider.refresh();
break;
case MessageTypeNames.ChangeChapter: {
const info = context.globalState.get("info") ?? {};
const uri = vscode.Uri.file(
Editor.getFilePath(message.assessmentName, message.questionId),
).toString();

_.set(info, `["${uri}"].chapter`, message.chapter ?? 1);
context.globalState.update("info", info);
client.sendRequest("source/publishInfo", info);

if (message.variant !== "default") {
vscode.window
.showInformationMessage(`The Language Server does not support any variants, the
Language Server will use Source §${message.chapter}, but it is not guaranteed to be accurate.`);
}

break;
}
case MessageTypeNames.ResetEditor:
if (this.activeEditor) {
this.activeEditor.replace(message.initialCode);
Expand Down
11 changes: 11 additions & 0 deletions src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ const Messages = createMessages({
EvalEditor: (workspaceLocation: VscWorkspaceLocation) => ({
workspaceLocation: workspaceLocation,
}),
ChangeChapter: (
assessmentName: string,
questionId: number,
chapter: number,
variant: string,
) => ({
assessmentName,
questionId,
chapter,
variant,
}),
ResetEditor: (
workspaceLocation: VscWorkspaceLocation,
initialCode: string,
Expand Down