Skip to content

Commit

Permalink
Use experimental API to write remote files.
Browse files Browse the repository at this point in the history
This is built on top of microsoft/vscode#30337.

Note that I had to follow the instructions at
microsoft/vscode#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.
  • Loading branch information
bolinfest committed Jul 11, 2017
1 parent eeda7a9 commit 4e7647b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"args": ["/Users/mbolin/src/vscode", "--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false
},
{
Expand Down
34 changes: 29 additions & 5 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs');
const vscode = require('vscode');
const {Server: WebSocketServer} = require('ws');
const {createConnection} = require('./connection');
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"onCommand:extension.bigDigRemoteFileOpenerDemo"
],
"main": "./extension",
"enableProposedApi": true,
"contributes": {
"commands": [
{
Expand Down

0 comments on commit 4e7647b

Please sign in to comment.