Skip to content

Commit

Permalink
Improve VS Code file system handling (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew authored Aug 13, 2024
1 parent adfbda7 commit 16e7c68
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 111 deletions.
44 changes: 9 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/open-collaboration-protocol/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export interface EditorHandler {
export interface FileSystemHandler {
onReadFile(handler: Handler<[types.Path], types.FileData>): void;
readFile(target: MessageTarget, path: types.Path): Promise<types.FileData>;
onWriteFile(handler: Handler<[types.Path, string]>): void;
writeFile(target: MessageTarget, path: types.Path, content: string): Promise<void>;
onWriteFile(handler: Handler<[types.Path, types.FileData]>): void;
writeFile(target: MessageTarget, path: types.Path, content: types.FileData): Promise<void>;
onStat(handler: Handler<[types.Path], types.FileSystemStat>): void;
stat(target: MessageTarget, path: types.Path): Promise<types.FileSystemStat>;
onMkdir(handler: Handler<[types.Path]>): void;
Expand Down
2 changes: 1 addition & 1 deletion packages/open-collaboration-protocol/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export namespace Messages {
export const Stat = new RequestType<[types.Path], types.FileSystemStat>('fileSystem/stat');
export const Mkdir = new RequestType<[types.Path], undefined>('fileSystem/mkdir');
export const ReadFile = new RequestType<[types.Path], types.FileData>('fileSystem/readFile');
export const WriteFile = new RequestType<[types.Path, string], undefined>('fileSystem/writeFile');
export const WriteFile = new RequestType<[types.Path, types.FileData], undefined>('fileSystem/writeFile');
export const ReadDir = new RequestType<[types.Path], Record<string, types.FileType>>('fileSystem/readDir');
export const Delete = new RequestType<[types.Path], undefined>('fileSystem/delete');
export const Rename = new RequestType<[types.Path, types.Path], undefined>('fileSystem/rename');
Expand Down
8 changes: 3 additions & 5 deletions packages/open-collaboration-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,14 @@
"reflect-metadata": "^0.2.2",
"open-collaboration-yjs": "0.1.0",
"open-collaboration-protocol": "0.1.0",
"lodash.debounce": "^4.0.8",
"lodash": "^4.17.21",
"node-fetch": "^2.0.0"
},
"devDependencies": {
"@types/lodash.debounce": "^4.0.9",
"@types/lodash": "^4.17.7",
"@types/node-fetch": "^2.0.0",
"@types/path-browserify": "^1.0.2",
"@types/vscode": "^1.73.0",
"@vscode/l10n-dev": "^0.0.35",
"path-browserify": "1.0.1"
"@vscode/l10n-dev": "^0.0.35"
},
"volta": {
"node": "18.20.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ export class CollaborationFileSystemProvider implements vscode.FileSystemProvide
const stringValue = this.yjs.getText(path);
return this.encoder.encode(stringValue.toString());
} else {
// Attempt to stat the file to see if it exists on the host system
await this.stat(uri);
// Just return an empty file. It will be filled by YJS
return new Uint8Array();
const file = await this.connection.fs.readFile(this.host.id, path);
return file.content;
}
}
writeFile(_uri: vscode.Uri, _content: Uint8Array, _options: { readonly create: boolean; readonly overwrite: boolean; }): void {
// Do nothing
writeFile(uri: vscode.Uri, content: Uint8Array, _options: { readonly create: boolean; readonly overwrite: boolean; }): void {
const path = this.getHostPath(uri);
this.connection.fs.writeFile(this.host.id, path, { content });
}
delete(uri: vscode.Uri, _options: { readonly recursive: boolean; }): Promise<void> {
return this.connection.fs.delete(this.host.id, this.getHostPath(uri));
Expand All @@ -69,6 +68,8 @@ export class CollaborationFileSystemProvider implements vscode.FileSystemProvide
}

protected getHostPath(uri: vscode.Uri): string {
// When creating a URI as a guest, we always prepend it with the name of the workspace
// This just removes the workspace name from the path to get the path expected by the protocol
const path = uri.path.substring(1).split('/');
return path.slice(1).join('/');
}
Expand Down
Loading

0 comments on commit 16e7c68

Please sign in to comment.