11import * as vscode from 'vscode' ;
22import DocumentStore from '../core/document.store' ;
3- import { TextEncoder } from 'util' ;
4- import { Document } from './couch.collection' ;
3+ import { FileSystemError } from 'vscode' ;
54
65export class CouchFileSystemProvider implements vscode . FileSystemProvider {
76 public static scheme = 'couchdb' ;
87
9- onDidChangeFile : vscode . Event < vscode . FileChangeEvent [ ] > ;
8+ private readonly _emitter = new vscode . EventEmitter < vscode . FileChangeEvent [ ] > ( ) ;
9+
10+ readonly onDidChangeFile : vscode . Event < vscode . FileChangeEvent [ ] > = this . _emitter . event ;
11+ private readonly watchers = new Map < string , Set < Symbol > > ;
1012
1113 constructor ( private readonly documentStore : DocumentStore ) {
12- this . onDidChangeFile = new vscode . EventEmitter <
13- vscode . FileChangeEvent [ ]
14- > ( ) . event ;
14+ this . _emitter = new vscode . EventEmitter < vscode . FileChangeEvent [ ] > ( ) ;
15+ this . onDidChangeFile = this . _emitter . event ;
1516 }
1617
17- public watch (
18- uri : vscode . Uri ,
19- options : { readonly recursive : boolean ; readonly excludes : readonly string [ ] }
20- ) : vscode . Disposable {
21- console . log ( 'watch' , uri , options ) ;
22-
23- return new vscode . Disposable ( ( ) => { } ) ;
18+ watch ( resource : vscode . Uri ) : vscode . Disposable {
19+ return new vscode . Disposable ( ( ) => undefined ) ;
2420 }
2521
26- public stat ( uri : vscode . Uri ) : vscode . FileStat {
27- const doc = this . documentStore . findByURI ( uri ) ;
22+ public async stat ( uri : vscode . Uri ) : Promise < vscode . FileStat > {
23+ const document = this . documentStore . findByURI ( uri ) ;
24+ if ( ! document ) {
25+ throw FileSystemError . FileNotFound ;
26+ }
2827
2928 return {
3029 type : vscode . FileType . File ,
31- ctime : ( doc as Document ) . ctime ,
32- mtime : ( doc as Document ) . mtime ,
33- size : new TextEncoder ( ) . encode ( ( doc as Document ) . content || '' ) . length ,
30+ ctime : 0 ,
31+ mtime : document . mtime ,
32+ size : document . size ,
3433 } ;
3534 }
3635
3736 public readDirectory (
3837 uri : vscode . Uri
3938 ) : [ string , vscode . FileType ] [ ] | Thenable < [ string , vscode . FileType ] [ ] > {
40- console . log ( 'readDirectory' , uri ) ;
41-
4239 return Promise . reject ( ) ;
4340 }
4441
4542 public createDirectory ( uri : vscode . Uri ) : void | Thenable < void > {
46- console . log ( 'createDirectory' , uri ) ;
47-
4843 return Promise . reject ( ) ;
4944 }
5045
5146 public async readFile ( uri : vscode . Uri ) : Promise < Uint8Array > {
52- const document = await this . documentStore . get ( {
53- source : uri . authority ,
54- _id : uri . path . slice ( 1 ) ,
55- } ) ;
56-
47+ const document = await this . documentStore . get ( uri ) ;
5748 if ( ! document ) {
58- throw new Error ( 'Document not found' ) ;
49+ throw FileSystemError . FileNotFound ;
5950 }
6051
61- return new TextEncoder ( ) . encode ( document . content ) ;
52+ try {
53+ return document . getContent ( ) ;
54+ } catch ( error ) {
55+ throw new Error ( ) ;
56+ }
6257 }
6358
64- public writeFile (
59+ public async writeFile (
6560 uri : vscode . Uri ,
6661 content : Uint8Array ,
6762 options : { readonly create : boolean ; readonly overwrite : boolean }
68- ) : void | Thenable < void > {
69- console . log ( 'writeFile' , uri , content , options ) ;
70-
71- return Promise . reject ( ) ;
63+ ) : Promise < void > {
64+ try {
65+ await this . documentStore . put ( uri , content ) ;
66+
67+ this . _emitter . fire ( [ { type : vscode . FileChangeType . Changed , uri } ] ) ;
68+
69+ return Promise . resolve ( ) ;
70+ } catch ( error ) {
71+ return Promise . reject ( error ) ;
72+ }
7273 }
7374
7475 public delete (
7576 uri : vscode . Uri ,
7677 options : { readonly recursive : boolean }
7778 ) : void | Thenable < void > {
78- console . log ( 'delete' , uri , options ) ;
79-
8079 return Promise . reject ( ) ;
8180 }
8281
@@ -85,8 +84,6 @@ export class CouchFileSystemProvider implements vscode.FileSystemProvider {
8584 newUri : vscode . Uri ,
8685 options : { readonly overwrite : boolean }
8786 ) : void | Thenable < void > {
88- console . log ( 'rename' , oldUri , newUri , options ) ;
89-
9087 return Promise . reject ( ) ;
9188 }
9289
@@ -95,8 +92,6 @@ export class CouchFileSystemProvider implements vscode.FileSystemProvider {
9592 destination : vscode . Uri ,
9693 options : { readonly overwrite : boolean }
9794 ) : void | Thenable < void > {
98- console . log ( 'copy' , source , destination , options ) ;
99-
10095 return Promise . reject ( ) ;
10196 }
10297}
0 commit comments