@@ -92,6 +92,28 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
92
92
return this . _lookupAsFile ( uri ) . then ( ( file : File ) => file . data ) ;
93
93
}
94
94
95
+ private generateFileContent ( fileName : string , content : Buffer ) : { content : string [ ] ; enc : boolean } {
96
+ const fileExt = fileName . split ( "." ) . pop ( ) . toLowerCase ( ) ;
97
+ if ( fileExt === "cls" ) {
98
+ const className = fileName . split ( "." ) . slice ( 0 , - 1 ) . join ( "." ) ;
99
+ return {
100
+ content : [ `Class ${ className } {}` ] ,
101
+ enc : false ,
102
+ } ;
103
+ } else if ( [ "int" , "inc" , "mac" ] . includes ( fileExt ) ) {
104
+ const routineName = fileName . split ( "." ) . slice ( 0 , - 1 ) . join ( "." ) ;
105
+ const routineType = `[ type = ${ fileExt } ]` ;
106
+ return {
107
+ content : [ `ROUTINE ${ routineName } ${ routineType } ` ] ,
108
+ enc : false ,
109
+ } ;
110
+ }
111
+ return {
112
+ content : [ content . toString ( "base64" ) ] ,
113
+ enc : true ,
114
+ } ;
115
+ }
116
+
95
117
public writeFile (
96
118
uri : vscode . Uri ,
97
119
content : Buffer ,
@@ -111,66 +133,38 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
111
133
}
112
134
const api = new AtelierAPI ( uri ) ;
113
135
return this . _lookupAsFile ( uri )
114
- . then ( ( file ) => ( file . data = content ) )
136
+ . then ( ( ) => Promise . reject ( ) )
137
+ . catch ( ( error ) => {
138
+ if ( error . code === "FileNotFound" ) {
139
+ return ;
140
+ }
141
+ return Promise . reject ( ) ;
142
+ } )
115
143
. then ( ( ) => {
116
- api
117
- . actionIndex ( [ fileName ] )
118
- . then ( ( data ) => data . result . content [ 0 ] )
119
- . then ( ( info ) => {
120
- if ( info . status === "" ) {
121
- /// file found, everything is Ok
122
- return ;
123
- }
124
- if ( options . create ) {
125
- if ( csp ) {
126
- return api . putDoc (
127
- fileName ,
128
- {
129
- content : [ content . toString ( "base64" ) ] ,
130
- enc : true ,
131
- mtime : Date . now ( ) ,
132
- } ,
133
- false
134
- ) ;
135
- }
136
- const fileExt = fileName . split ( "." ) . pop ( ) . toLowerCase ( ) ;
137
- if ( fileExt === "cls" ) {
138
- const className = fileName . split ( "." ) . slice ( 0 , - 1 ) . join ( "." ) ;
139
- return api . putDoc (
140
- fileName ,
141
- {
142
- content : [ `Class ${ className } {}` ] ,
143
- enc : false ,
144
- mtime : Date . now ( ) ,
145
- } ,
146
- false
147
- ) ;
148
- } else if ( [ "int" , "inc" , "mac" ] . includes ( fileExt ) ) {
149
- const api = new AtelierAPI ( uri ) ;
150
- const routineName = fileName . split ( "." ) . slice ( 0 , - 1 ) . join ( "." ) ;
151
- const routineType = `[ type = ${ fileExt } ]` ;
152
- return api . putDoc (
153
- fileName ,
154
- {
155
- content : [ `ROUTINE ${ routineName } ${ routineType } ` ] ,
156
- enc : false ,
157
- mtime : Date . now ( ) ,
158
- } ,
159
- false
160
- ) ;
161
- }
162
- throw new Error ( "Not implemented" ) ;
163
- }
164
- } )
165
- . then ( ( response ) => {
166
- if ( response && response . result . ext && response . result . ext [ 0 ] && response . result . ext [ 1 ] ) {
167
- fireOtherStudioAction ( OtherStudioAction . CreatedNewDocument , uri , response . result . ext [ 0 ] ) ;
168
- fireOtherStudioAction ( OtherStudioAction . FirstTimeDocumentSave , uri , response . result . ext [ 1 ] ) ;
169
- }
170
- this . _lookupAsFile ( uri ) . then ( ( entry ) => {
171
- this . _fireSoon ( { type : vscode . FileChangeType . Changed , uri } ) ;
172
- } ) ;
173
- } ) ;
144
+ 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
+ } ) ;
174
168
} ) ;
175
169
}
176
170
0 commit comments