Skip to content

Commit

Permalink
utils: Add stashReadOnlyContentSync function (#1769)
Browse files Browse the repository at this point in the history
* Add stashReadOnlyContentSync function

* Remove unused parameter

* Export the function for external use

* Bump package version
  • Loading branch information
JasonYeMSFT authored Aug 14, 2024
1 parent 76f11f4 commit b010f00
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
8 changes: 8 additions & 0 deletions utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,14 @@ export declare function openReadOnlyContent(node: { label: string, fullId: strin
*/
export declare function stashReadOnlyContent(node: { label: string, fullId: string }, content: string, fileExtension: string): Promise<ReadOnlyContent>;

/**
* Stash a read-only editor so it can be opened by its uri later.
* @param node Typically (but not strictly) an `AzExtTreeItem`. `label` is used for the file name displayed in VS Code and a random id will be generated to uniquely identify this file
* @param content The content to display
* @param fileExtension The file extension
*/
export declare function stashReadOnlyContentSync(node: { label: string }, content: string, fileExtension: string): ReadOnlyContent;

/**
* Disposes all the read-only contents stashed in memory.
*/
Expand Down
4 changes: 2 additions & 2 deletions utils/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@microsoft/vscode-azext-utils",
"author": "Microsoft Corporation",
"version": "2.5.4",
"version": "2.5.5",
"description": "Common UI tools for developing Azure extensions for VS Code",
"tags": [
"azure",
Expand Down
22 changes: 18 additions & 4 deletions utils/src/openReadOnlyContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export async function stashReadOnlyContent(node: { label: string, fullId: string
return await contentProvider.stashReadOnlyContent(node, content, fileExtension);
}

export function stashReadOnlyContentSync(node: { label: string }, content: string, fileExtension: string): ReadOnlyContent {
const contentProvider = getContentProvider();
return contentProvider.stashReadOnlyContentSync(node, content, fileExtension);
}

export async function openReadOnlyContent(node: { label: string, fullId: string }, content: string, fileExtension: string, options?: TextDocumentShowOptions): Promise<ReadOnlyContent> {
const contentProvider = getContentProvider();
return await contentProvider.openReadOnlyContent(node, content, fileExtension, options);
Expand Down Expand Up @@ -98,17 +103,26 @@ class ReadOnlyContentProvider implements TextDocumentContentProvider {
return this._onDidChangeEmitter.event;
}

public async stashReadOnlyContent(node: { label: string, fullId: string }, content: string, fileExtension: string): Promise<ReadOnlyContent> {
private stashReadOnlyContentCore(label: string, fileId: string, fileExtension: string, content: string): ReadOnlyContent {
const scheme = getScheme();
const idHash: string = await randomUtils.getPseudononymousStringHash(node.fullId);
// Remove special characters which may prove troublesome when parsing the uri. We'll allow the same set as `encodeUriComponent`
const fileName = node.label.replace(/[^a-z0-9\-\_\.\!\~\*\'\(\)]/gi, '_');
const uri: Uri = Uri.parse(`${scheme}:///${idHash}/${fileName}${fileExtension}`);
const fileName = label.replace(/[^a-z0-9\-\_\.\!\~\*\'\(\)]/gi, '_');
const uri: Uri = Uri.parse(`${scheme}:///${fileId}/${fileName}${fileExtension}`);
const readOnlyContent: ReadOnlyContent = new ReadOnlyContent(uri, this._onDidChangeEmitter, content);
this._contentMap.set(uri.toString(), readOnlyContent);
return readOnlyContent;
}

public async stashReadOnlyContent(node: { label: string, fullId: string }, content: string, fileExtension: string): Promise<ReadOnlyContent> {
const idHash: string = await randomUtils.getPseudononymousStringHash(node.fullId);
return this.stashReadOnlyContentCore(node.label, idHash, fileExtension, content);
}

public stashReadOnlyContentSync(node: { label: string }, content: string, fileExtension: string): ReadOnlyContent {
const randomId: string = randomUtils.getRandomHexString(16);
return this.stashReadOnlyContentCore(node.label, randomId, fileExtension, content);
}

public async openReadOnlyContent(node: { label: string, fullId: string }, content: string, fileExtension: string, options?: TextDocumentShowOptions): Promise<ReadOnlyContent> {
const readOnlyContent = await this.stashReadOnlyContent(node, content, fileExtension);
await window.showTextDocument(readOnlyContent.uri, options);
Expand Down

0 comments on commit b010f00

Please sign in to comment.