@@ -26,7 +26,7 @@ import {
2626 Uri ,
2727 workspace ,
2828} from 'vscode' ;
29- import { IChangeStore , ContextStore } from './stores' ;
29+ import { ContextStore , IWritableChangeStore } from './stores' ;
3030import { GitHubApiContext } from './github/api' ;
3131
3232const emptyDisposable = { dispose : ( ) => { /* noop */ } } ;
@@ -44,7 +44,7 @@ export class VirtualFS implements FileSystemProvider, FileSearchProvider, TextSe
4444 readonly scheme : string ,
4545 private readonly originalScheme : string ,
4646 contextStore : ContextStore < GitHubApiContext > ,
47- private readonly changeStore : IChangeStore ,
47+ private readonly changeStore : IWritableChangeStore ,
4848 private readonly fs : FileSystemProvider & FileSearchProvider & TextSearchProvider
4949 ) {
5050 // TODO@eamodio listen for workspace folder changes
@@ -100,21 +100,19 @@ export class VirtualFS implements FileSystemProvider, FileSearchProvider, TextSe
100100 return stat ;
101101 }
102102
103- if ( uri . path === '' || uri . path . lastIndexOf ( '/' ) === 0 ) {
104- return { type : FileType . Directory , size : 0 , ctime : 0 , mtime : 0 } ;
105- }
106-
107103 stat = await this . fs . stat ( this . getOriginalResource ( uri ) ) ;
108104 return stat ;
109105 }
110106
111107 async readDirectory ( uri : Uri ) : Promise < [ string , FileType ] [ ] > {
112- const entries = await this . fs . readDirectory ( this . getOriginalResource ( uri ) ) ;
108+ let entries = await this . fs . readDirectory ( this . getOriginalResource ( uri ) ) ;
109+ entries = this . changeStore . updateDirectoryEntries ( uri , entries ) ;
113110 return entries ;
114111 }
115112
116113 createDirectory ( _uri : Uri ) : void | Thenable < void > {
117- throw FileSystemError . NoPermissions ;
114+ // TODO@eamodio only support files for now
115+ throw FileSystemError . NoPermissions ( ) ;
118116 }
119117
120118 async readFile ( uri : Uri ) : Promise < Uint8Array > {
@@ -127,20 +125,60 @@ export class VirtualFS implements FileSystemProvider, FileSearchProvider, TextSe
127125 return data ;
128126 }
129127
130- async writeFile ( uri : Uri , content : Uint8Array , _options : { create : boolean , overwrite : boolean } ) : Promise < void > {
131- await this . changeStore . recordFileChange ( uri , content , ( ) => this . fs . readFile ( this . getOriginalResource ( uri ) ) ) ;
128+ async writeFile ( uri : Uri , content : Uint8Array , options : { create : boolean , overwrite : boolean } ) : Promise < void > {
129+ let stat ;
130+ try {
131+ stat = await this . stat ( uri ) ;
132+ if ( ! options . overwrite ) {
133+ throw FileSystemError . FileExists ( ) ;
134+ }
135+ } catch ( ex ) {
136+ if ( ex instanceof FileSystemError && ex . code === 'FileNotFound' ) {
137+ if ( ! options . create ) {
138+ throw FileSystemError . FileNotFound ( ) ;
139+ }
140+ } else {
141+ throw ex ;
142+ }
143+ }
144+
145+ if ( stat === undefined ) {
146+ await this . changeStore . onFileCreated ( uri , content ) ;
147+ } else {
148+ await this . changeStore . onFileChanged ( uri , content , ( ) => this . fs . readFile ( this . getOriginalResource ( uri ) ) ) ;
149+ }
132150 }
133151
134- delete ( _uri : Uri , _options : { recursive : boolean } ) : void | Thenable < void > {
135- throw FileSystemError . NoPermissions ;
152+ async delete ( uri : Uri , _options : { recursive : boolean } ) : Promise < void > {
153+ const stat = await this . stat ( uri ) ;
154+ if ( stat . type !== FileType . File ) {
155+ throw FileSystemError . NoPermissions ( ) ;
156+ }
157+
158+ await this . changeStore . onFileDeleted ( uri ) ;
136159 }
137160
138- rename ( _oldUri : Uri , _newUri : Uri , _options : { overwrite : boolean } ) : void | Thenable < void > {
139- throw FileSystemError . NoPermissions ;
161+ async rename ( oldUri : Uri , newUri : Uri , options : { overwrite : boolean } ) : Promise < void > {
162+ const stat = await this . stat ( oldUri ) ;
163+ // TODO@eamodio only support files for now
164+ if ( stat . type !== FileType . File ) {
165+ throw FileSystemError . NoPermissions ( ) ;
166+ }
167+
168+ const content = await this . readFile ( oldUri ) ;
169+ await this . writeFile ( newUri , content , { create : true , overwrite : options . overwrite } ) ;
170+ await this . delete ( oldUri , { recursive : false } ) ;
140171 }
141172
142- copy ( _source : Uri , _destination : Uri , _options : { overwrite : boolean } ) : void | Thenable < void > {
143- throw FileSystemError . NoPermissions ;
173+ async copy ( source : Uri , destination : Uri , options : { overwrite : boolean } ) : Promise < void > {
174+ const stat = await this . stat ( source ) ;
175+ // TODO@eamodio only support files for now
176+ if ( stat . type !== FileType . File ) {
177+ throw FileSystemError . NoPermissions ( ) ;
178+ }
179+
180+ const content = await this . readFile ( source ) ;
181+ await this . writeFile ( destination , content , { create : true , overwrite : options . overwrite } ) ;
144182 }
145183
146184 //#endregion
0 commit comments