From dd931a3c0572b79e27198df631036ef8d8164e70 Mon Sep 17 00:00:00 2001 From: Jeff Peterson Date: Thu, 29 Nov 2018 14:50:43 -0800 Subject: [PATCH] Add external uri handler --- package-lock.json | 8 ++++---- package.json | 9 +++++---- src/HypermergeUriHandler.ts | 15 +++++++++++++++ src/extension.ts | 20 ++++++++++++++------ src/treeview.ts | 14 ++++++++++++-- 5 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 src/HypermergeUriHandler.ts diff --git a/package-lock.json b/package-lock.json index ae8d535..2eb7643 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2867,7 +2867,7 @@ }, "os-homedir": { "version": "1.0.2", - "resolved": "http://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz", + "resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz", "integrity": "sha1-/7xJiDNuDoM94MFox+8VISGqf7M=", "dev": true }, @@ -2882,7 +2882,7 @@ }, "os-tmpdir": { "version": "1.0.2", - "resolved": "http://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", + "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", "integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=", "dev": true }, @@ -2956,7 +2956,7 @@ }, "path-is-absolute": { "version": "1.0.1", - "resolved": "http://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", "dev": true }, @@ -3713,7 +3713,7 @@ }, "tar": { "version": "2.2.1", - "resolved": "http://registry.npmjs.org/tar/-/tar-2.2.1.tgz", + "resolved": "https://registry.npmjs.org/tar/-/tar-2.2.1.tgz", "integrity": "sha1-jk0qJWwOIYXGsYrWlK7JaLg8sdE=", "dev": true, "requires": { diff --git a/package.json b/package.json index 27d6486..8d1bce5 100644 --- a/package.json +++ b/package.json @@ -9,14 +9,15 @@ "type": "git" }, "engines": { - "vscode": "^1.23.0" + "vscode": "^1.29.1" }, "categories": [ "Other" ], "activationEvents": [ - "onFileSystem:hypermergefs", - "onView:hypermergeExplorer" + "onFileSystem:hypermerge", + "onView:hypermergeExplorer", + "onUri" ], "main": "./out/src/extension", "contributes": { @@ -138,7 +139,7 @@ "@types/node": "^10.2.0", "electron-rebuild": "^1.8.2", "typescript": "^3.1.4", - "vscode": "^1.1.21" + "vscode": "^1.1.22" }, "dependencies": { "@hyperswarm/network": "0.0.4", diff --git a/src/HypermergeUriHandler.ts b/src/HypermergeUriHandler.ts new file mode 100644 index 0000000..a6522ab --- /dev/null +++ b/src/HypermergeUriHandler.ts @@ -0,0 +1,15 @@ +import { UriHandler, Uri, commands, OutputChannel } from 'vscode' + +export default class HypermergeUriHandler implements UriHandler { + output: OutputChannel + constructor(output: OutputChannel) { + this.output = output + output.appendLine("HypermergeUriHandler registered") + } + + handleUri(uri: Uri) { + const uriString = uri.path.replace(/^\//, "") + this.output.appendLine(uriString) + commands.executeCommand("hypermergeExplorer.open", uriString) + } +} diff --git a/src/extension.ts b/src/extension.ts index b3f857d..dd7f183 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -4,11 +4,14 @@ import { HypermergeFS } from "./fileSystemProvider"; import { HypermergeExplorer } from "./treeview"; import { HypermergeViewContainer } from "./details"; import { HypermergeWrapper } from "./fauxmerge"; +import HypermergeUriHandler from "./HypermergeUriHandler"; export function activate(context: vscode.ExtensionContext) { console.log("HypermergeFS activated"); const hypermergeWrapper = new HypermergeWrapper(); + const output = vscode.window.createOutputChannel("Hypermerge") + const hypermergeFs = new HypermergeFS(hypermergeWrapper); context.subscriptions.push( vscode.workspace.registerFileSystemProvider("hypermerge", hypermergeFs, { @@ -16,13 +19,18 @@ export function activate(context: vscode.ExtensionContext) { }) ); - vscode.workspace.onDidOpenTextDocument(document => { - if (document.uri.scheme === "hypermerge") { - if (JSON.parse(document.getText())) { - (vscode.languages as any).setTextDocumentLanguage(document, "json"); + context.subscriptions.push( + vscode.workspace.onDidOpenTextDocument(document => { + if (document.uri.scheme === "hypermerge") { + if (JSON.parse(document.getText())) { + (vscode.languages as any).setTextDocumentLanguage(document, "json"); + } } - } - }); + })); + + context.subscriptions.push( + vscode.window.registerUriHandler(new HypermergeUriHandler(output)) + ) // self-registers new HypermergeExplorer(context, hypermergeWrapper); diff --git a/src/treeview.ts b/src/treeview.ts index 65aa731..71bcc66 100644 --- a/src/treeview.ts +++ b/src/treeview.ts @@ -15,10 +15,10 @@ export class HypermergeTreeDataProvider implements vscode.TreeDataProvider { private _onDidChangeTreeData: vscode.EventEmitter< HypermergeNodeKey | undefined - > = new vscode.EventEmitter(); + > = new vscode.EventEmitter(); readonly onDidChangeTreeData: vscode.Event< HypermergeNodeKey | undefined - > = this._onDidChangeTreeData.event; + > = this._onDidChangeTreeData.event; constructor(private readonly hypermergeWrapper: HypermergeWrapper) { this.hypermergeWrapper = hypermergeWrapper; @@ -72,6 +72,8 @@ export class HypermergeTreeDataProvider } public addRoot(uriString: string) { + if (this.roots().has(uriString)) return + const inspectRoots = vscode.workspace .getConfiguration("hypermergefs") .inspect("roots"); @@ -188,6 +190,14 @@ export class HypermergeExplorer { treeDataProvider.refresh() ); + vscode.commands.registerCommand("hypermergeExplorer.open", (uriString: string) => { + if (!this.validateURL(uriString)) { + treeDataProvider.addRoot(uriString); + treeDataProvider.refresh(); + vscode.workspace.openTextDocument(vscode.Uri.parse(uriString)) + } + }); + context.subscriptions.push( vscode.workspace.onDidChangeConfiguration(e => { if (e.affectsConfiguration("hypermergefs.roots")) {