Skip to content

Commit 6866548

Browse files
committed
open document as file in editor window
1 parent da0861f commit 6866548

File tree

8 files changed

+98
-76
lines changed

8 files changed

+98
-76
lines changed

src/api/database.repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ export default class DatabaseRepository {
1919
public async remove(database: Database): Promise<void> {
2020
const couch = await this.connection.instance();
2121

22-
await couch.db.destroy(database.label);
22+
await couch.db.destroy(database.id!);
2323
}
2424
}

src/api/document.repository.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,15 @@ export default class DocumentRepository {
3939
};
4040
}
4141

42-
public async get(document: Document): Promise<DocumentGetResponse> {
42+
public async get(document: {
43+
source: string;
44+
_id: string;
45+
}): Promise<DocumentGetResponse> {
4346
const couch = await this.connection.instance();
4447

4548
const db = couch.use(document.source);
4649

47-
return db.get(document._id, {});
50+
return await db.get(document._id, {});
4851
}
4952

5053
public async remove(document: Document): Promise<void> {

src/controller/document.controller.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ export default class DocumentController {
5353

5454
public async openDocument(document: Document): Promise<void> {
5555
try {
56-
const data = await this.documentStore.get(document);
57-
5856
this.documentProvider.refresh(this.documentView);
5957

60-
document.setRev(data._rev);
61-
document.setContent(JSON.stringify(data, null, '\t'));
62-
6358
// open the document in a new editor
64-
vscode.workspace.openTextDocument(document.uri);
59+
const doc = await vscode.workspace.openTextDocument(document.uri);
60+
61+
// set json language mode
62+
await vscode.languages.setTextDocumentLanguage(doc, 'json');
63+
64+
vscode.window.showTextDocument(doc);
6565
} catch (error) {
6666
vscode.window.showErrorMessage('Document was removed.');
6767

src/core/document.store.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import CouchItem from '../provider/couch.item';
77
import { DocumentGetResponse } from 'nano';
88

99
export default class DocumentStore extends DataStore<CouchItem> {
10+
public active?: DocumentGetResponse;
11+
1012
private total = 0;
1113

1214
constructor(private readonly documentRepository: DocumentRepository) {
@@ -29,8 +31,31 @@ export default class DocumentStore extends DataStore<CouchItem> {
2931
return data;
3032
}
3133

32-
public async get(document: Document): Promise<DocumentGetResponse> {
33-
return await this.documentRepository.get(document);
34+
public async get(document: {
35+
source: string;
36+
_id: string;
37+
}): Promise<Document> {
38+
const data = await this.documentRepository.get(document);
39+
40+
let doc = this.data.find((d) => (d as Document)._id! === document._id);
41+
if (!doc) {
42+
doc = new Document(
43+
{
44+
id: data._id,
45+
value: {
46+
rev: data._rev,
47+
},
48+
},
49+
document.source
50+
);
51+
52+
this.data.push(doc);
53+
}
54+
55+
(doc as Document).mtime = Date.now();
56+
(doc as Document).setContent(JSON.stringify(data, null, '\t'));
57+
58+
return doc as Document;
3459
}
3560

3661
public findByURI(uri: vscode.Uri): Document | undefined {

src/provider/couch.collection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class Document extends CouchItem implements vscode.FileStat {
7272

7373
this.contextValue = 'document';
7474

75-
this.ctime = 0;
75+
this.ctime = Date.now();
7676
this.mtime = 0;
7777
}
7878

src/provider/couch.editor.provider.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,102 @@
11
import * as vscode from 'vscode';
2+
import DocumentStore from '../core/document.store';
3+
import { TextEncoder } from 'util';
4+
import { Document } from './couch.collection';
25

36
export class CouchFileSystemProvider implements vscode.FileSystemProvider {
47
public static scheme = 'couchdb';
58

69
onDidChangeFile: vscode.Event<vscode.FileChangeEvent[]>;
710

8-
constructor() {
11+
constructor(private readonly documentStore: DocumentStore) {
912
this.onDidChangeFile = new vscode.EventEmitter<
1013
vscode.FileChangeEvent[]
1114
>().event;
1215
}
1316

14-
watch(
17+
public watch(
1518
uri: vscode.Uri,
1619
options: { readonly recursive: boolean; readonly excludes: readonly string[] }
1720
): vscode.Disposable {
18-
throw new Error('Method not implemented.');
21+
console.log('watch', uri, options);
22+
23+
return new vscode.Disposable(() => {});
1924
}
2025

21-
stat(uri: vscode.Uri): vscode.FileStat | Thenable<vscode.FileStat> {
22-
throw new Error('Method not implemented.');
26+
public stat(uri: vscode.Uri): vscode.FileStat {
27+
const doc = this.documentStore.findByURI(uri);
28+
29+
return {
30+
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,
34+
};
2335
}
2436

25-
readDirectory(
37+
public readDirectory(
2638
uri: vscode.Uri
2739
): [string, vscode.FileType][] | Thenable<[string, vscode.FileType][]> {
28-
throw new Error('Method not implemented.');
40+
console.log('readDirectory', uri);
41+
42+
return Promise.reject();
2943
}
3044

31-
createDirectory(uri: vscode.Uri): void | Thenable<void> {
32-
throw new Error('Method not implemented.');
45+
public createDirectory(uri: vscode.Uri): void | Thenable<void> {
46+
console.log('createDirectory', uri);
47+
48+
return Promise.reject();
3349
}
3450

35-
readFile(uri: vscode.Uri): Uint8Array | Thenable<Uint8Array> {
36-
throw new Error('Method not implemented.');
51+
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+
57+
if (!document) {
58+
throw new Error('Document not found');
59+
}
60+
61+
return new TextEncoder().encode(document.content);
3762
}
3863

39-
writeFile(
64+
public writeFile(
4065
uri: vscode.Uri,
4166
content: Uint8Array,
4267
options: { readonly create: boolean; readonly overwrite: boolean }
4368
): void | Thenable<void> {
44-
throw new Error('Method not implemented.');
69+
console.log('writeFile', uri, content, options);
70+
71+
return Promise.reject();
4572
}
4673

47-
delete(
74+
public delete(
4875
uri: vscode.Uri,
4976
options: { readonly recursive: boolean }
5077
): void | Thenable<void> {
51-
throw new Error('Method not implemented.');
78+
console.log('delete', uri, options);
79+
80+
return Promise.reject();
5281
}
5382

54-
rename(
83+
public rename(
5584
oldUri: vscode.Uri,
5685
newUri: vscode.Uri,
5786
options: { readonly overwrite: boolean }
5887
): void | Thenable<void> {
59-
throw new Error('Method not implemented.');
88+
console.log('rename', oldUri, newUri, options);
89+
90+
return Promise.reject();
6091
}
6192

62-
copy?(
93+
public copy?(
6394
source: vscode.Uri,
6495
destination: vscode.Uri,
6596
options: { readonly overwrite: boolean }
6697
): void | Thenable<void> {
67-
throw new Error('Method not implemented.');
98+
console.log('copy', source, destination, options);
99+
100+
return Promise.reject();
68101
}
69102
}

src/service/editor.service.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import * as vscode from 'vscode';
22
import { CouchFileSystemProvider } from '../provider/filesystem.provider';
3+
import DocumentStore from '../core/document.store';
34

45
export default class EditorService {
56
private provider: CouchFileSystemProvider;
67

7-
constructor(private readonly context: vscode.ExtensionContext) {
8-
this.provider = new CouchFileSystemProvider();
8+
constructor(
9+
private readonly context: vscode.ExtensionContext,
10+
private readonly documentStore: DocumentStore
11+
) {
12+
this.provider = new CouchFileSystemProvider(documentStore);
913

1014
const providerRegistration = vscode.Disposable.from(
1115
vscode.workspace.registerFileSystemProvider(

0 commit comments

Comments
 (0)