1
1
import * as vscode from "vscode" ;
2
- import * as os from "os" ;
3
2
4
3
import config from "../utils/config" ;
5
- import { VscWorkspaceLocation } from "./messages" ;
4
+ import Messages , { VscWorkspaceLocation } from "./messages" ;
6
5
import path from "path" ;
6
+ import { sendToFrontendWrapped } from "../commands/showPanel" ;
7
7
import { canonicaliseLocation } from "./misc" ;
8
8
9
9
export class Editor {
@@ -73,7 +73,39 @@ export class Editor {
73
73
: initialCode ;
74
74
75
75
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
+ } ,
77
109
async ( ) => {
78
110
self . log ( `Opening file failed, creating at ${ filePath } ` ) ;
79
111
await vscode . workspace . fs . writeFile (
@@ -95,25 +127,27 @@ export class Editor {
95
127
vscode . commands . executeCommand ( "editor.fold" ) ;
96
128
97
129
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
+ ) ;
117
151
return self ;
118
152
}
119
153
0 commit comments