From 4e7647b8c821011c0af37d0912e0f3d6ebfbdc52 Mon Sep 17 00:00:00 2001 From: Michael Bolin Date: Tue, 11 Jul 2017 01:20:00 -0700 Subject: [PATCH] Use experimental API to write remote files. This is built on top of https://github.com/Microsoft/vscode/pull/30337. Note that I had to follow the instructions at https://github.com/Microsoft/vscode/issues/13394 to be able to run this extension from the OSS version of VS Code. Unfortunately, that means I made a personal modification to `.vscode/launch.json`, so hopefully I can find some way to avoid that. This is pretty exciting in that I had to make minimal changes to my existing extension to get this to work! The only issue that I'm running into right now is that `resolveContents()` appears to be called more often than I expect. It is called twice when I open the file for the first time. I also opened a remote `.md` file using this mechanism. I was able to use `Markdown: Open Preview` on this remote file, but when I closed the preview, `resolveContents()` was called again and I'm not sure why. Because loading remote file contents could be expensive, we would ideally fix things so this is called sparingly, or we'll have to maintain a local cache to avoid extra roundtrips. Note that I had to launch the OSS version (built from source) using `./scripts/code.sh --remote nuclide`. There is a TODO in the PR to eliminate the need to pass this flag. --- .vscode/launch.json | 2 +- extension.js | 34 +++++++++++++++++++++++++++++----- package.json | 1 + 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 2131f2c..8e576d2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,7 +7,7 @@ "type": "extensionHost", "request": "launch", "runtimeExecutable": "${execPath}", - "args": ["--extensionDevelopmentPath=${workspaceRoot}" ], + "args": ["/Users/mbolin/src/vscode", "--extensionDevelopmentPath=${workspaceRoot}" ], "stopOnEntry": false }, { diff --git a/extension.js b/extension.js index b19001b..c8cf789 100644 --- a/extension.js +++ b/extension.js @@ -1,3 +1,4 @@ +const fs = require('fs'); const vscode = require('vscode'); const {Server: WebSocketServer} = require('ws'); const {createConnection} = require('./connection'); @@ -55,11 +56,33 @@ function onDidWebSocketServerStartListening(server, context) { context.subscriptions.push(connectionWrapper); context.subscriptions.push({dispose() {connection.close()}}); - simpleContentProvider = new SimpleTextDocumentContentProvider(connectionWrapper); + // simpleContentProvider = new SimpleTextDocumentContentProvider(connectionWrapper); + // context.subscriptions.push( + // vscode.workspace.registerTextDocumentContentProvider( + // 'nuclide', + // simpleContentProvider) + // ); context.subscriptions.push( - vscode.workspace.registerTextDocumentContentProvider( - 'nuclide', - simpleContentProvider) + vscode.workspace.registerFileSystemProvider('nuclide', { + onDidChange: new vscode.EventEmitter().event, + resolveContents(resource) { + // TODO(jrieken): This method appears to get invoked more often than I would expect! + + // Strip the leading slash from resource.path. + const path = resource.path.substring(1); + return connectionWrapper.makeRpc( + 'get-file-contents', + {path} + ).then(response => response.contents); + }, + writeContents(resource, value) { + // Strip the leading slash from resource.path. + const path = resource.path.substring(1); + return new Promise((resolve, reject) => { + fs.writeFile(path, value, err => err ? reject(err) : resolve()); + }); + } + }) ); ws.send(JSON.stringify({ @@ -88,7 +111,8 @@ function onDidWebSocketServerStartListening(server, context) { const {file} = params; const address = connection.getAddress(); const remotePath = `${searchDirectory}/${file}`; - const uri = `${address.replace(/^wss?:/, 'nuclide:')}${remotePath}`; + // const uri = `${address.replace(/^wss?:/, 'nuclide:')}${remotePath}`; + const uri = `file://nuclide/${remotePath}`; vscode.workspace.openTextDocument(vscode.Uri.parse(uri)).then( textDocument => vscode.window.showTextDocument(textDocument, vscode.ViewColumn.Two, /* preserveFocus */ true), error => console.error(`Failed to open text document for uri '${uri}'`, error)); diff --git a/package.json b/package.json index 04878b5..4d73fd7 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "onCommand:extension.bigDigRemoteFileOpenerDemo" ], "main": "./extension", + "enableProposedApi": true, "contributes": { "commands": [ {