Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vscode): Implements readFile/writeFile for workspace.fs #6980

Merged
merged 1 commit into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/plugin-ext/src/common/plugin-api-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,8 @@ export interface FileSystemExt {
}

export interface FileSystemMain {
$readFile(uri: UriComponents): Promise<string>;
$writeFile(uri: UriComponents, content: string): Promise<void>;
$registerFileSystemProvider(handle: number, scheme: string): void;
$unregisterProvider(handle: number): void;
}
Expand Down
20 changes: 19 additions & 1 deletion packages/plugin-ext/src/main/browser/file-system-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@
import { interfaces, injectable } from 'inversify';
import Uri from 'vscode-uri';
import { Disposable, ResourceResolver, DisposableCollection } from '@theia/core';
import { Resource } from '@theia/core/lib/common/resource';
import { Resource, ResourceProvider } from '@theia/core/lib/common/resource';
import URI from '@theia/core/lib/common/uri';
import { MAIN_RPC_CONTEXT, FileSystemMain, FileSystemExt } from '../../common/plugin-api-rpc';
import { RPCProtocol } from '../../common/rpc-protocol';
import { UriComponents } from '../../common/uri-components';

export class FileSystemMainImpl implements FileSystemMain, Disposable {

private readonly proxy: FileSystemExt;
private readonly resourceResolver: FSResourceResolver;
private readonly resourceProvider: ResourceProvider;
private readonly providers = new Map<number, Disposable>();
private readonly toDispose = new DisposableCollection();

constructor(rpc: RPCProtocol, container: interfaces.Container) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.FILE_SYSTEM_EXT);
this.resourceResolver = container.get(FSResourceResolver);
this.resourceProvider = container.get(ResourceProvider);
}

dispose(): void {
Expand All @@ -54,6 +57,21 @@ export class FileSystemMainImpl implements FileSystemMain, Disposable {
}
}

async $readFile(uriComponents: UriComponents): Promise<string> {
const uri = Uri.revive(uriComponents);
const resource = await this.resourceProvider(new URI(uri));
return resource.readContents();
}

async $writeFile(uriComponents: UriComponents, content: string): Promise<void> {
const uri = Uri.revive(uriComponents);
const resource = await this.resourceProvider(new URI(uri));
if (!resource.saveContents) {
throw new Error(`'No write operation available on the resource for URI ${uriComponents}`);
}
return resource.saveContents(content);
}

}

@injectable()
Expand Down
8 changes: 8 additions & 0 deletions packages/plugin-ext/src/plugin/file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import { PLUGIN_RPC_CONTEXT, FileSystemExt, FileSystemMain } from '../common/plu
import { RPCProtocol } from '../common/rpc-protocol';
import { UriComponents, Schemes } from '../common/uri-components';
import { Disposable } from './types-impl';
import { InPluginFileSystemProxy } from './in-plugin-filesystem-proxy';

export class FileSystemExtImpl implements FileSystemExt {

private readonly proxy: FileSystemMain;
private readonly usedSchemes = new Set<string>();
private readonly fsProviders = new Map<number, theia.FileSystemProvider>();
private fileSystem: InPluginFileSystemProxy;

private handlePool: number = 0;

Expand All @@ -41,6 +43,12 @@ export class FileSystemExtImpl implements FileSystemExt {
this.usedSchemes.add(Schemes.MAILTO);
this.usedSchemes.add(Schemes.DATA);
this.usedSchemes.add(Schemes.COMMAND);
this.fileSystem = new InPluginFileSystemProxy(this.proxy);

}

get fs(): theia.FileSystem {
return this.fileSystem;
}

registerFileSystemProvider(scheme: string, provider: theia.FileSystemProvider): theia.Disposable {
Expand Down
62 changes: 62 additions & 0 deletions packages/plugin-ext/src/plugin/in-plugin-filesystem-proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/********************************************************************************
* Copyright (C) 2020 Red Hat, Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

import * as theia from '@theia/plugin';
import { TextEncoder, TextDecoder } from 'util';
import { FileSystemMain } from '../common/plugin-api-rpc';
import { UriComponents } from '../common/uri-components';
import { FileSystemError } from './types-impl';

/**
* This class is managing FileSystem proxy
*/
export class InPluginFileSystemProxy implements theia.FileSystem {

private proxy: FileSystemMain;

constructor(proxy: FileSystemMain) {
this.proxy = proxy;
}

async readFile(uri: UriComponents): Promise<Uint8Array> {
try {
const val = await this.proxy.$readFile(uri);
return new TextEncoder().encode(val);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried about this line: will it provide the exact byte contents of the file in every case? Reading the file as a string will use a certain text encoding. How can we be sure we're using the same encoding on this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsmaeder because it is using utf-8 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you don't know that: resources are a point of extension. Say a resource reads a file in EBDIC and translates that correctly to a string. When you encode that using utf-8, you will not have the original byte contents in your byte array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it was like temporary issue (for non utf-8 resources) until being able to use filesystem from workspace.fs (and then hook FileStat etc in theia core)
For now we only have resources allowing the file access + custom FileSystemResourceProvider

} catch (error) {
throw this.handleError(error);
}
}
async writeFile(uri: UriComponents, content: Uint8Array): Promise<void> {
const encoded = new TextDecoder().decode(content);

try {
await this.proxy.$writeFile(uri, encoded);
} catch (error) {
throw this.handleError(error);
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
handleError(error: any): Error {
if (!(error instanceof Error)) {
return new FileSystemError(String(error));
}

// file system error
return new FileSystemError(error.message, error.name);
}

}
5 changes: 5 additions & 0 deletions packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ export function createAPIFactory(
};

const workspace: typeof theia.workspace = {

get fs(): theia.FileSystem {
return fileSystemExt.fs;
},

get rootPath(): string | undefined {
return workspaceExt.rootPath;
},
Expand Down
35 changes: 35 additions & 0 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4505,6 +4505,33 @@ declare module '@theia/plugin' {
copy?(source: Uri, destination: Uri, options: { overwrite: boolean }): void | PromiseLike<void>;
}

/**
* The file system interface exposes the editor's built-in and contributed
* [file system providers](#FileSystemProvider). It allows extensions to work
* with files from the local disk as well as files from remote places, like the
* remote extension host or ftp-servers.
*
* *Note* that an instance of this interface is avaiable as [`workspace.fs`](#workspace.fs).
*/
export interface FileSystem {

/**
* Read the entire contents of a file.
*
* @param uri The uri of the file.
* @return An array of bytes or a PromiseLike that resolves to such.
*/
readFile(uri: Uri): PromiseLike<Uint8Array>;

/**
* Write data to a file, replacing its entire contents.
*
* @param uri The uri of the file.
* @param content The new content of the file.
*/
writeFile(uri: Uri, content: Uint8Array): PromiseLike<void>;
}

/**
* Namespace for dealing with the current workspace. A workspace is the representation
* of the folder that has been opened. There is no workspace when just a file but not a
Expand All @@ -4516,6 +4543,14 @@ declare module '@theia/plugin' {
*/
export namespace workspace {

/**
* A [file system](#FileSystem) instance that allows to interact with local and remote
* files, e.g. `workspace.fs.readDirectory(someUri)` allows to retrieve all entries
* of a directory or `workspace.fs.stat(anotherUri)` returns the meta data for a
* file.
*/
export const fs: FileSystem;

/**
* ~~The folder that is open in the editor. `undefined` when no folder
* has been opened.~~
Expand Down