Skip to content

Commit 76dcf5e

Browse files
committed
feat: remove prepend when sending to frontend
1 parent 57a06b1 commit 76dcf5e

File tree

3 files changed

+65
-45
lines changed

3 files changed

+65
-45
lines changed

src/commands/showPanel.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { FRONTEND_ELEMENT_ID } from "../constants";
1515
import { client, SOURCE_ACADEMY_ICON_URI } from "../extension";
1616
import _ from "lodash";
1717
import { treeDataProvider } from "../treeview";
18-
import { getNumPrependLines } from "../utils/editorUtils";
18+
import { codeRemovePrepend, getNumPrependLines } from "../utils/editorUtils";
1919

2020
let panel: vscode.WebviewPanel | null = null;
2121
// This needs to be a reference to active
@@ -78,8 +78,10 @@ async function handleMessage(
7878
`EXTENSION: Editor ${editor.assessmentName}_${editor.questionId} is no longer active, skipping onChange`,
7979
);
8080
}
81-
const message = Messages.Text(workspaceLocation, code);
82-
console.log(`Sending message: ${JSON.stringify(message)}`);
81+
const message = Messages.Text(
82+
workspaceLocation,
83+
codeRemovePrepend(code),
84+
);
8385
sendToFrontend(panel, message);
8486
});
8587
break;

src/utils/editor.ts

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Messages, { VscWorkspaceLocation } from "./messages";
66
import path from "path";
77
import { sendToFrontendWrapped } from "../commands/showPanel";
88
import { canonicaliseLocation } from "./misc";
9-
import { codeAddPrepend } from "./editorUtils";
9+
import { codeAddPrepend, codeRemovePrepend } from "./editorUtils";
1010

1111
export class Editor {
1212
editor?: vscode.TextEditor;
@@ -64,48 +64,51 @@ export class Editor {
6464

6565
const contents = codeAddPrepend(initialCode, prepend);
6666

67-
await vscode.workspace.fs.readFile(vscode.Uri.file(filePath)).then(
68-
(value) => {
69-
if (value.toString() !== contents) {
70-
self.log(
71-
"EXTENSION: Conflict detected between local and remote, prompting user to choose one",
67+
await vscode.workspace.fs
68+
.readFile(vscode.Uri.file(filePath))
69+
.then((value) => value.toString())
70+
.then(
71+
(localCode) => {
72+
if (localCode !== contents) {
73+
self.log(
74+
"EXTENSION: Conflict detected between local and remote, prompting user to choose one",
75+
);
76+
vscode.window
77+
.showInformationMessage(
78+
[
79+
"The local file differs from the version on the Source Academy servers.",
80+
"Discard the local file and use the one on the server?",
81+
].join(" "),
82+
{ modal: true },
83+
"Yes",
84+
)
85+
.then(async (answer) => {
86+
// By default the code displayed is the local one
87+
if (answer === "Yes") {
88+
self.log("EXTENSION: Saving program from server to file");
89+
await vscode.workspace.fs.writeFile(
90+
uri,
91+
new TextEncoder().encode(contents),
92+
);
93+
} else if (answer === undefined) {
94+
// Modal cancelled
95+
const message = Messages.Text(
96+
self.workspaceLocation,
97+
codeRemovePrepend(localCode),
98+
);
99+
sendToFrontendWrapped(message);
100+
}
101+
});
102+
}
103+
},
104+
async () => {
105+
self.log(`Opening file failed, creating at ${filePath}`);
106+
await vscode.workspace.fs.writeFile(
107+
uri,
108+
new TextEncoder().encode(contents),
72109
);
73-
vscode.window
74-
.showInformationMessage(
75-
[
76-
"The local file differs from the version on the Source Academy servers.",
77-
"Discard the local file and use the one on the server?",
78-
].join(" "),
79-
{ modal: true },
80-
"Yes",
81-
)
82-
.then(async (answer) => {
83-
// By default the code displayed is the local one
84-
if (answer === "Yes") {
85-
self.log("EXTENSION: Saving program from server to file");
86-
await vscode.workspace.fs.writeFile(
87-
uri,
88-
new TextEncoder().encode(contents),
89-
);
90-
} else if (answer === undefined) {
91-
// Modal cancelled
92-
const message = Messages.Text(
93-
self.workspaceLocation,
94-
value.toString(),
95-
);
96-
sendToFrontendWrapped(message);
97-
}
98-
});
99-
}
100-
},
101-
async () => {
102-
self.log(`Opening file failed, creating at ${filePath}`);
103-
await vscode.workspace.fs.writeFile(
104-
uri,
105-
new TextEncoder().encode(contents),
106-
);
107-
},
108-
);
110+
},
111+
);
109112

110113
const editor = await vscode.window.showTextDocument(uri, {
111114
preview: false,

src/utils/editorUtils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,18 @@ export function getNumPrependLines(prepend: string) {
2727
}
2828
return prepend.split("\n").length + 3; // account for start/end markers
2929
}
30+
31+
export function codeRemovePrepend(code: string) {
32+
const lines = code.split("\n");
33+
const start = lines.indexOf(PREPEND_MARKER_START);
34+
const end = lines.indexOf(PREPEND_MARKER_END);
35+
36+
if (start === -1 || end === -1 || end < start) {
37+
return code;
38+
}
39+
40+
// If line spacing between prepend and code accidentally removed, do not delete code!
41+
const skip = end + (lines[end + 1] === "" ? 1 : 0);
42+
43+
return lines.slice(skip + 1).join("\n");
44+
}

0 commit comments

Comments
 (0)