Skip to content
This repository has been archived by the owner on Jan 20, 2023. It is now read-only.

Add FTP TextDocumentContentProvider #13

Merged
merged 2 commits into from
Nov 7, 2018
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: 1 addition & 1 deletion tree-view-sample-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@
"devDependencies": {
"@theia/plugin": "next",
"@theia/plugin-packager": "latest",
"@types/ftp": "^0.3.10",
"rimraf": "2.6.2",
"typescript-formatter": "7.2.2",
"typescript": "2.9.2"
},
"dependencies": {
"@types/ftp": "^0.3.10",
"ftp": "^0.3.10"
},
"scripts": {
Expand Down
32 changes: 20 additions & 12 deletions tree-view-sample-plugin/src/ftp-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export class FtpModel {

client.end();

return c(this.sort(list.map(entry => ({ resource: theia.Uri.parse(`ftp://${this.host}///${entry.name}`), isDirectory: entry.type === 'd' }))));
return c(this.sort(list.map(entry => ({
resource: theia.Uri.parse(`ftp://${this.host}/${entry.name}`),
isDirectory: entry.type === 'd'
}))));
});
});
});
Expand All @@ -65,7 +68,10 @@ export class FtpModel {

client.end();

return c(this.sort(list.map(entry => ({ resource: theia.Uri.parse(`${node.resource.fsPath}/${entry.name}`), isDirectory: entry.type === 'd' }))));
return c(this.sort(list.map(entry => ({
resource: theia.Uri.parse(`ftp://${this.host}${node.resource.fsPath}/${entry.name}`),
isDirectory: entry.type === 'd'
}))));
});
});
});
Expand All @@ -88,7 +94,7 @@ export class FtpModel {
public getContent(resource: theia.Uri): Promise<string> {
return this.connect().then(client => {
return new Promise<string>((c, e) => {
client.get(resource.path.substr(2), (err, stream) => {
client.get(resource.path, (err, stream) => {
if (err) {
return e(err);
}
Expand All @@ -112,8 +118,7 @@ export class FtpModel {

}

// Implement theia.TextDocumentContentProvider intreface and uncomment provideTextDocumentContent
export class FtpTreeDataProvider implements theia.TreeDataProvider<FtpNode> {
export class FtpTreeDataProvider implements theia.TreeDataProvider<FtpNode>, theia.TextDocumentContentProvider {

private _onDidChangeTreeData: theia.EventEmitter<any> = new theia.EventEmitter<any>();
readonly onDidChangeTreeData: theia.Event<any> = this._onDidChangeTreeData.event;
Expand Down Expand Up @@ -151,9 +156,9 @@ export class FtpTreeDataProvider implements theia.TreeDataProvider<FtpNode> {

}

// public provideTextDocumentContent(uri: theia.Uri, token: theia.CancellationToken): theia.ProviderResult<string> {
// return this.model.getContent(uri).then(content => content);
// }
public provideTextDocumentContent(uri: theia.Uri, token: theia.CancellationToken): theia.ProviderResult<string> {
return this.model.getContent(uri).then(content => content);
}
}

const ftpExplorerRefresh: theia.Command = {
Expand All @@ -180,20 +185,23 @@ export class FtpExplorer {
constructor(context: theia.PluginContext) {
this.ftpModel = new FtpModel('mirror.switch.ch', 'anonymous', 'anonymous@anonymous.de');
const treeDataProvider = new FtpTreeDataProvider(this.ftpModel);
// theia.workspace.registerTextDocumentContentProvider('ftp', treeDataProvider)
theia.workspace.registerTextDocumentContentProvider('ftp', treeDataProvider)

this.ftpViewer = theia.window.createTreeView('ftpExplorer', { treeDataProvider });

context.subscriptions.push(
theia.commands.registerCommand(ftpExplorerRefresh, () => treeDataProvider.refresh()));
context.subscriptions.push(
theia.commands.registerCommand(ftpExplorerOpenFtpResource, resource => this.openResource(resource)));
theia.commands.registerCommand(ftpExplorerOpenFtpResource, args => this.openResource(args)));
context.subscriptions.push(
theia.commands.registerCommand(ftpExplorerRevealResource, () => this.reveal()));
}

private openResource(resource: theia.Uri): void {
theia.workspace.openTextDocument(resource);
private openResource(args: any[]): void {
if (args && args.length > 0) {
const resource: theia.Uri = args[0] as theia.Uri;
theia.workspace.openTextDocument(resource);
}
}

private async reveal(): Promise<void> {
Expand Down