@@ -6,7 +6,7 @@ import Messages, { VscWorkspaceLocation } from "./messages";
6
6
import path from "path" ;
7
7
import { sendToFrontendWrapped } from "../commands/showPanel" ;
8
8
import { canonicaliseLocation } from "./misc" ;
9
- import { codeAddPrepend } from "./editorUtils" ;
9
+ import { codeAddPrepend , codeRemovePrepend } from "./editorUtils" ;
10
10
11
11
export class Editor {
12
12
editor ?: vscode . TextEditor ;
@@ -64,48 +64,51 @@ export class Editor {
64
64
65
65
const contents = codeAddPrepend ( initialCode , prepend ) ;
66
66
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 ) ,
72
109
) ;
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
+ ) ;
109
112
110
113
const editor = await vscode . window . showTextDocument ( uri , {
111
114
preview : false ,
0 commit comments