|
| 1 | +// TODO: Move this file to src/features/editor/editor.ts |
1 | 2 | import * as vscode from "vscode"; |
2 | 3 |
|
3 | 4 | import config from "../utils/config"; |
4 | 5 | import Messages, { VscWorkspaceLocation } from "./messages"; |
5 | 6 | import path from "path"; |
6 | 7 | import { sendToFrontendWrapped } from "../commands/showPanel"; |
7 | 8 | import { canonicaliseLocation } from "./misc"; |
| 9 | +import { codeAddPrepend, codeRemovePrepend } from "./editorUtils"; |
8 | 10 |
|
9 | 11 | export class Editor { |
10 | 12 | editor?: vscode.TextEditor; |
@@ -62,58 +64,53 @@ export class Editor { |
62 | 64 | const uri = vscode.Uri.file(filePath); |
63 | 65 | self.uri = uri.toString(); |
64 | 66 |
|
65 | | - const contents = |
66 | | - prepend !== "" |
67 | | - ? [ |
68 | | - "// PREPEND -- DO NOT EDIT", |
69 | | - prepend, |
70 | | - "// END PREPEND", |
71 | | - initialCode, |
72 | | - ].join("\n") |
73 | | - : initialCode; |
74 | | - |
75 | | - await vscode.workspace.fs.readFile(vscode.Uri.file(filePath)).then( |
76 | | - (value) => { |
77 | | - if (value.toString() !== contents) { |
78 | | - self.log( |
79 | | - "EXTENSION: Conflict detected between local and remote, prompting user to choose one", |
| 67 | + const contents = codeAddPrepend(initialCode, prepend); |
| 68 | + |
| 69 | + await vscode.workspace.fs |
| 70 | + .readFile(vscode.Uri.file(filePath)) |
| 71 | + .then((value) => value.toString()) |
| 72 | + .then( |
| 73 | + (localCode) => { |
| 74 | + if (localCode !== contents) { |
| 75 | + self.log( |
| 76 | + "EXTENSION: Conflict detected between local and remote, prompting user to choose one", |
| 77 | + ); |
| 78 | + vscode.window |
| 79 | + .showInformationMessage( |
| 80 | + [ |
| 81 | + "The local file differs from the version on the Source Academy servers.", |
| 82 | + "Discard the local file and use the one on the server?", |
| 83 | + ].join(" "), |
| 84 | + { modal: true }, |
| 85 | + "Yes", |
| 86 | + ) |
| 87 | + .then(async (answer) => { |
| 88 | + // By default the code displayed is the local one |
| 89 | + if (answer === "Yes") { |
| 90 | + self.log("EXTENSION: Saving program from server to file"); |
| 91 | + await vscode.workspace.fs.writeFile( |
| 92 | + uri, |
| 93 | + new TextEncoder().encode(contents), |
| 94 | + ); |
| 95 | + } else if (answer === undefined) { |
| 96 | + // Modal cancelled |
| 97 | + const message = Messages.Text( |
| 98 | + self.workspaceLocation, |
| 99 | + codeRemovePrepend(localCode), |
| 100 | + ); |
| 101 | + sendToFrontendWrapped(message); |
| 102 | + } |
| 103 | + }); |
| 104 | + } |
| 105 | + }, |
| 106 | + async () => { |
| 107 | + self.log(`Opening file failed, creating at ${filePath}`); |
| 108 | + await vscode.workspace.fs.writeFile( |
| 109 | + uri, |
| 110 | + new TextEncoder().encode(contents), |
80 | 111 | ); |
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 | | - }, |
109 | | - async () => { |
110 | | - self.log(`Opening file failed, creating at ${filePath}`); |
111 | | - await vscode.workspace.fs.writeFile( |
112 | | - uri, |
113 | | - new TextEncoder().encode(contents), |
114 | | - ); |
115 | | - }, |
116 | | - ); |
| 112 | + }, |
| 113 | + ); |
117 | 114 |
|
118 | 115 | const editor = await vscode.window.showTextDocument(uri, { |
119 | 116 | preview: false, |
|
0 commit comments