@@ -132,40 +132,51 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
132
132
return ;
133
133
}
134
134
const api = new AtelierAPI ( uri ) ;
135
- return this . _lookupAsFile ( uri )
136
- . then ( ( ) => Promise . reject ( ) )
137
- . catch ( ( error ) => {
138
- if ( error . code === "FileNotFound" ) {
139
- return ;
135
+ return this . _lookupAsFile ( uri ) . then (
136
+ ( ) => {
137
+ // Weirdly, if the file exists on the server we don't actually write its content here.
138
+ // Instead we simply return as though we succeeded. The actual writing is done by our
139
+ // workspace.onDidSaveTextDocument handler.
140
+ return ;
141
+ } ,
142
+ ( error ) => {
143
+ if ( error . code !== "FileNotFound" || ! options . create ) {
144
+ return Promise . reject ( ) ;
140
145
}
141
- return Promise . reject ( ) ;
142
- } )
143
- . then ( ( ) => {
146
+ // File doesn't exist on the server, and we are allowed to create it.
147
+ // Create content (typically a stub).
144
148
const newContent = this . generateFileContent ( fileName , content ) ;
145
- return api . putDoc (
146
- fileName ,
147
- {
148
- ...newContent ,
149
- mtime : Date . now ( ) ,
150
- } ,
151
- false
152
- ) ;
153
- } )
154
- . catch ( ( error ) => {
155
- if ( error . error ?. result ?. status ) {
156
- throw vscode . FileSystemError . Unavailable ( error . error . result . status ) ;
157
- }
158
- throw vscode . FileSystemError . Unavailable ( error . message ) ;
159
- } )
160
- . then ( ( response ) => {
161
- if ( response && response . result . ext && response . result . ext [ 0 ] && response . result . ext [ 1 ] ) {
162
- fireOtherStudioAction ( OtherStudioAction . CreatedNewDocument , uri , response . result . ext [ 0 ] ) ;
163
- fireOtherStudioAction ( OtherStudioAction . FirstTimeDocumentSave , uri , response . result . ext [ 1 ] ) ;
164
- }
165
- this . _lookupAsFile ( uri ) . then ( ( entry ) => {
166
- this . _fireSoon ( { type : vscode . FileChangeType . Changed , uri } ) ;
167
- } ) ;
168
- } ) ;
149
+
150
+ // Write it to the server
151
+ return api
152
+ . putDoc (
153
+ fileName ,
154
+ {
155
+ ...newContent ,
156
+ mtime : Date . now ( ) ,
157
+ } ,
158
+ false
159
+ )
160
+ . catch ( ( error ) => {
161
+ // Throw all failures
162
+ if ( error . error ?. result ?. status ) {
163
+ throw vscode . FileSystemError . Unavailable ( error . error . result . status ) ;
164
+ }
165
+ throw vscode . FileSystemError . Unavailable ( error . message ) ;
166
+ } )
167
+ . then ( ( response ) => {
168
+ // New file has been written
169
+ if ( response && response . result . ext && response . result . ext [ 0 ] && response . result . ext [ 1 ] ) {
170
+ fireOtherStudioAction ( OtherStudioAction . CreatedNewDocument , uri , response . result . ext [ 0 ] ) ;
171
+ fireOtherStudioAction ( OtherStudioAction . FirstTimeDocumentSave , uri , response . result . ext [ 1 ] ) ;
172
+ }
173
+ // Sanity check that we find it there, then make client side update things
174
+ this . _lookupAsFile ( uri ) . then ( ( ) => {
175
+ this . _fireSoon ( { type : vscode . FileChangeType . Changed , uri } ) ;
176
+ } ) ;
177
+ } ) ;
178
+ }
179
+ ) ;
169
180
}
170
181
171
182
public delete ( uri : vscode . Uri , options : { recursive : boolean } ) : void | Thenable < void > {
0 commit comments