Skip to content

Save and prepend fix #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/commands/showPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ async function handleMessage(
);
activeEditor.onChange((editor) => {
const workspaceLocation = editor.workspaceLocation;
const code = editor.getText();
if (!code) {
return;
}
const code = editor.getActualCode();

if (editor !== activeEditor) {
console.log(
`EXTENSION: Editor ${editor.assessmentName}_${editor.questionId} is no longer active, skipping onChange`,
Expand Down Expand Up @@ -161,6 +159,17 @@ export async function showPanel(
);

panel.iconPath = SOURCE_ACADEMY_ICON_URI;

vscode.workspace.onDidSaveTextDocument((e: vscode.TextDocument) => {
if (e.uri.toString() === activeEditor?.uri) {
const message = Messages.AssessmentAnswer(
activeEditor.questionId,
activeEditor.getActualCode()
);

sendToFrontend(panel, message);
}
})
}

// TODO: Move this to a util file
Expand Down
10 changes: 9 additions & 1 deletion src/utils/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class Editor {
onChangeCallback?: (editor: Editor) => void;
code: string | null = null;
uri: string | null = null;
num_prepend_lines: number = 0;

// For debugging purposes
replaceTime: number = 0;
Expand All @@ -38,6 +39,10 @@ export class Editor {
return this.editor?.document.getText();
}

getActualCode(): string {
return this.code ? this.code.split("\n").slice(this.num_prepend_lines).join("\n") : ""
}

// TODO: This method is too loaded, it's not obvious it also shows the editor
static async create(
workspaceLocation: VscWorkspaceLocation,
Expand Down Expand Up @@ -69,6 +74,9 @@ export class Editor {
initialCode,
].join("\n")
: initialCode;
if (prepend !== "") {
self.num_prepend_lines = prepend.split("\n").length+2
}

await vscode.workspace.fs.readFile(vscode.Uri.file(filePath)).then(
(value) => {
Expand Down Expand Up @@ -142,8 +150,8 @@ export class Editor {
return;
}
self.log(`EXTENSION: Editor's code changed!`);
self.onChangeCallback(self);
self.code = text;
self.onChangeCallback(self);
},
);
return self;
Expand Down
4 changes: 4 additions & 0 deletions src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ const Messages = createMessages({
Navigate: (route: string) => ({
route,
}),
AssessmentAnswer: (questionId: number, answer: string) => ({
questionId,
answer
}),
});

export default Messages;
Expand Down
Loading