Skip to content

Commit 60da52e

Browse files
Natalie Jamesonfacebook-github-bot
authored andcommitted
Add support for handling and requesting contents of starlark:// URIs
Summary: For built-in types, we want to use the starlark:// scheme to provide a pseudo file. In order for VSCode to be able to handle this, we have to register the scheme in the vscode extension Reviewed By: ndmitchell Differential Revision: D38307727 fbshipit-source-id: b7f04c5e6614f68a57bab71db1bf163e874ebdbf
1 parent 0f0aaf1 commit 60da52e

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

vscode/client/src/extension.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,50 @@ function additionalClientSettings(): AdditionalClientSettings {
4444
};
4545
}
4646

47+
const STARLARK_FILE_CONTENTS_METHOD = 'starlark/fileContents';
48+
const STARLARK_URI_SCHEME = 'starlark';
49+
50+
class StarlarkFileContentsParams {
51+
constructor(public uri: vscode.Uri) {}
52+
}
53+
54+
class StarlarkFileContentsResponse {
55+
constructor(public contents?: string | null) {}
56+
}
57+
58+
/// Ask the server for the contents of a starlark: file
59+
class StarlarkFileHandler implements vscode.TextDocumentContentProvider {
60+
provideTextDocumentContent(
61+
uri: vscode.Uri,
62+
_token: vscode.CancellationToken,
63+
): vscode.ProviderResult<string> {
64+
if (client === undefined) {
65+
return null;
66+
} else {
67+
return client
68+
.sendRequest<StarlarkFileContentsResponse>(
69+
STARLARK_FILE_CONTENTS_METHOD,
70+
new StarlarkFileContentsParams(uri),
71+
)
72+
.then((response: StarlarkFileContentsResponse) => {
73+
if (response.contents !== undefined && response.contents !== null) {
74+
return response.contents;
75+
} else {
76+
return null;
77+
}
78+
});
79+
}
80+
}
81+
}
82+
4783
export function activate(context: ExtensionContext) {
84+
// Make sure that any starlark: URIs that come back from the LSP
85+
// are handled, and requested from the LSP.
86+
vscode.workspace.registerTextDocumentContentProvider(
87+
STARLARK_URI_SCHEME,
88+
new StarlarkFileHandler(),
89+
);
90+
4891
const path: string = requireSetting("starlark.lspPath");
4992
const args: [string] = requireSetting("starlark.lspArguments");
5093

0 commit comments

Comments
 (0)