diff --git a/utils/index.d.ts b/utils/index.d.ts index a079f5cca9..6e939c37b4 100644 --- a/utils/index.d.ts +++ b/utils/index.d.ts @@ -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; +/** + * 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. */ diff --git a/utils/package-lock.json b/utils/package-lock.json index 792fec2abf..dce2e72360 100644 --- a/utils/package-lock.json +++ b/utils/package-lock.json @@ -1,12 +1,12 @@ { "name": "@microsoft/vscode-azext-utils", - "version": "2.5.4", + "version": "2.5.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@microsoft/vscode-azext-utils", - "version": "2.5.4", + "version": "2.5.5", "license": "MIT", "dependencies": { "@microsoft/vscode-azureresources-api": "^2.3.1", diff --git a/utils/package.json b/utils/package.json index 92b6cdcc8d..181219b1ae 100644 --- a/utils/package.json +++ b/utils/package.json @@ -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", diff --git a/utils/src/openReadOnlyContent.ts b/utils/src/openReadOnlyContent.ts index e6397345da..64e36ddae4 100644 --- a/utils/src/openReadOnlyContent.ts +++ b/utils/src/openReadOnlyContent.ts @@ -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 { const contentProvider = getContentProvider(); return await contentProvider.openReadOnlyContent(node, content, fileExtension, options); @@ -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 { + 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 { + 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 { const readOnlyContent = await this.stashReadOnlyContent(node, content, fileExtension); await window.showTextDocument(readOnlyContent.uri, options);