Skip to content

Commit fea8f7d

Browse files
authored
Merge branch 'main' into mcq-ui
2 parents 9708311 + 1194a9a commit fea8f7d

File tree

1 file changed

+56
-22
lines changed

1 file changed

+56
-22
lines changed

src/utils/editor.ts

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as vscode from "vscode";
2-
import * as os from "os";
32

43
import config from "../utils/config";
5-
import { VscWorkspaceLocation } from "./messages";
4+
import Messages, { VscWorkspaceLocation } from "./messages";
65
import path from "path";
6+
import { sendToFrontendWrapped } from "../commands/showPanel";
77
import { canonicaliseLocation } from "./misc";
88

99
export class Editor {
@@ -73,7 +73,39 @@ export class Editor {
7373
: initialCode;
7474

7575
await vscode.workspace.fs.readFile(vscode.Uri.file(filePath)).then(
76-
() => null,
76+
(value) => {
77+
if (value.toString() !== contents) {
78+
self.log(
79+
"EXTENSION: Conflict detected between local and remote, prompting user to choose one",
80+
);
81+
vscode.window
82+
.showInformationMessage(
83+
[
84+
"The local file differs from the version on the Source Academy servers.",
85+
"Discard the local file and use the one on the server?",
86+
].join(" "),
87+
{ modal: true },
88+
"Yes",
89+
)
90+
.then(async (answer) => {
91+
// By default the code displayed is the local one
92+
if (answer === "Yes") {
93+
self.log("EXTENSION: Saving program from server to file");
94+
await vscode.workspace.fs.writeFile(
95+
uri,
96+
new TextEncoder().encode(contents),
97+
);
98+
} else if (answer === undefined) {
99+
// Modal cancelled
100+
const message = Messages.Text(
101+
self.workspaceLocation,
102+
value.toString(),
103+
);
104+
sendToFrontendWrapped(message);
105+
}
106+
});
107+
}
108+
},
77109
async () => {
78110
self.log(`Opening file failed, creating at ${filePath}`);
79111
await vscode.workspace.fs.writeFile(
@@ -95,25 +127,27 @@ export class Editor {
95127
vscode.commands.executeCommand("editor.fold");
96128

97129
self.editor = editor;
98-
vscode.workspace.onDidChangeTextDocument(() => {
99-
if (!self.onChangeCallback) {
100-
return;
101-
}
102-
const text = editor.document.getText();
103-
if (self.code === text) {
104-
self.log(`EXTENSION: Editor's code did not change, ignoring`);
105-
return;
106-
}
107-
if (Date.now() - self.replaceTime < 1000) {
108-
self.log(
109-
`EXTENSION: Ignoring change event, ${Date.now() - self.replaceTime}ms since last replace`,
110-
);
111-
return;
112-
}
113-
self.log(`EXTENSION: Editor's code changed! ${text}`);
114-
self.onChangeCallback(self);
115-
self.code = text;
116-
});
130+
vscode.workspace.onDidChangeTextDocument(
131+
(e: vscode.TextDocumentChangeEvent) => {
132+
if (!self.onChangeCallback) {
133+
return;
134+
}
135+
const text = editor.document.getText();
136+
if (e.contentChanges.length === 0) {
137+
self.log(`EXTENSION: Editor's code did not change, ignoring`);
138+
return;
139+
}
140+
if (Date.now() - self.replaceTime < 1000) {
141+
self.log(
142+
`EXTENSION: Ignoring change event, ${Date.now() - self.replaceTime}ms since last replace`,
143+
);
144+
return;
145+
}
146+
self.log(`EXTENSION: Editor's code changed!`);
147+
self.onChangeCallback(self);
148+
self.code = text;
149+
},
150+
);
117151
return self;
118152
}
119153

0 commit comments

Comments
 (0)