Skip to content

Commit 4e7647b

Browse files
committed
Use experimental API to write remote files.
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.
1 parent eeda7a9 commit 4e7647b

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"type": "extensionHost",
88
"request": "launch",
99
"runtimeExecutable": "${execPath}",
10-
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
10+
"args": ["/Users/mbolin/src/vscode", "--extensionDevelopmentPath=${workspaceRoot}" ],
1111
"stopOnEntry": false
1212
},
1313
{

extension.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const fs = require('fs');
12
const vscode = require('vscode');
23
const {Server: WebSocketServer} = require('ws');
34
const {createConnection} = require('./connection');
@@ -55,11 +56,33 @@ function onDidWebSocketServerStartListening(server, context) {
5556
context.subscriptions.push(connectionWrapper);
5657
context.subscriptions.push({dispose() {connection.close()}});
5758

58-
simpleContentProvider = new SimpleTextDocumentContentProvider(connectionWrapper);
59+
// simpleContentProvider = new SimpleTextDocumentContentProvider(connectionWrapper);
60+
// context.subscriptions.push(
61+
// vscode.workspace.registerTextDocumentContentProvider(
62+
// 'nuclide',
63+
// simpleContentProvider)
64+
// );
5965
context.subscriptions.push(
60-
vscode.workspace.registerTextDocumentContentProvider(
61-
'nuclide',
62-
simpleContentProvider)
66+
vscode.workspace.registerFileSystemProvider('nuclide', {
67+
onDidChange: new vscode.EventEmitter().event,
68+
resolveContents(resource) {
69+
// TODO(jrieken): This method appears to get invoked more often than I would expect!
70+
71+
// Strip the leading slash from resource.path.
72+
const path = resource.path.substring(1);
73+
return connectionWrapper.makeRpc(
74+
'get-file-contents',
75+
{path}
76+
).then(response => response.contents);
77+
},
78+
writeContents(resource, value) {
79+
// Strip the leading slash from resource.path.
80+
const path = resource.path.substring(1);
81+
return new Promise((resolve, reject) => {
82+
fs.writeFile(path, value, err => err ? reject(err) : resolve());
83+
});
84+
}
85+
})
6386
);
6487

6588
ws.send(JSON.stringify({
@@ -88,7 +111,8 @@ function onDidWebSocketServerStartListening(server, context) {
88111
const {file} = params;
89112
const address = connection.getAddress();
90113
const remotePath = `${searchDirectory}/${file}`;
91-
const uri = `${address.replace(/^wss?:/, 'nuclide:')}${remotePath}`;
114+
// const uri = `${address.replace(/^wss?:/, 'nuclide:')}${remotePath}`;
115+
const uri = `file://nuclide/${remotePath}`;
92116
vscode.workspace.openTextDocument(vscode.Uri.parse(uri)).then(
93117
textDocument => vscode.window.showTextDocument(textDocument, vscode.ViewColumn.Two, /* preserveFocus */ true),
94118
error => console.error(`Failed to open text document for uri '${uri}'`, error));

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"onCommand:extension.bigDigRemoteFileOpenerDemo"
1515
],
1616
"main": "./extension",
17+
"enableProposedApi": true,
1718
"contributes": {
1819
"commands": [
1920
{

0 commit comments

Comments
 (0)