diff --git a/utils/index.d.ts b/utils/index.d.ts index 6471e55619..07b4e8d95f 100644 --- a/utils/index.d.ts +++ b/utils/index.d.ts @@ -1435,10 +1435,15 @@ export type AzExtItemUriParts = { query: AzExtItemQuery; }; +export interface AzExtTreeFileSystemItem { + id: string; + refresh?(context: IActionContext): Promise; +} + /** - * A virtual file system based around AzExTreeItems that only supports viewing/editing single files. + * A virtual file system based around {@link AzExtTreeFileSystemItem} that only supports viewing/editing single files. */ -export declare abstract class AzExtTreeFileSystem implements FileSystemProvider { +export declare abstract class AzExtTreeFileSystem implements FileSystemProvider { public abstract scheme: string; public constructor(tree: AzExtTreeDataProvider); diff --git a/utils/src/AzExtTreeFileSystem.ts b/utils/src/AzExtTreeFileSystem.ts index 6ec54c7fd3..a0b2663a4c 100644 --- a/utils/src/AzExtTreeFileSystem.ts +++ b/utils/src/AzExtTreeFileSystem.ts @@ -8,25 +8,20 @@ import { Disposable, Event, EventEmitter, FileChangeEvent, FileStat, FileSystemE import * as types from '../index'; import { callWithTelemetryAndErrorHandling } from "./callWithTelemetryAndErrorHandling"; import { localize } from "./localize"; -import { AzExtTreeDataProvider } from "./tree/AzExtTreeDataProvider"; -import { AzExtTreeItem } from "./tree/AzExtTreeItem"; import { nonNullProp } from "./utils/nonNull"; const unsupportedError: Error = new Error(localize('notSupported', 'This operation is not supported.')); -export abstract class AzExtTreeFileSystem implements FileSystemProvider { +export abstract class AzExtTreeFileSystem implements FileSystemProvider { + + private readonly itemCache: Map = new Map(); + public abstract scheme: string; private readonly _emitter: EventEmitter = new EventEmitter(); private readonly _bufferedEvents: FileChangeEvent[] = []; private _fireSoonHandle?: NodeJS.Timer; - private _tree: AzExtTreeDataProvider; - - public constructor(tree: AzExtTreeDataProvider) { - this._tree = tree; - } - public get onDidChangeFile(): Event { return this._emitter.event; } @@ -37,7 +32,9 @@ export abstract class AzExtTreeFileSystem implement public abstract getFilePath(item: TItem): string; public async showTextDocument(item: TItem, options?: TextDocumentShowOptions): Promise { - await window.showTextDocument(this.getUriFromItem(item), options); + const uri = this.getUriFromItem(item); + this.itemCache.set(item.id, item); + await window.showTextDocument(uri, options); } public watch(): Disposable { @@ -70,7 +67,7 @@ export abstract class AzExtTreeFileSystem implement await callWithTelemetryAndErrorHandling('writeFile', async (context) => { const item: TItem = await this.lookup(context, uri); await this.writeFileImpl(context, item, content, uri); - await item.refresh(context); + await item.refresh?.(context); }); } @@ -115,28 +112,24 @@ export abstract class AzExtTreeFileSystem implement ); } - protected getUriParts(item: TItem): types.AzExtItemUriParts { - return { + protected findItem(query: types.AzExtItemQuery): TItem | undefined { + return this.itemCache.get(query.id); + } + + private getUriFromItem(item: TItem): Uri { + const data: types.AzExtItemUriParts = { filePath: this.getFilePath(item), query: { - id: item.fullId + id: item.id } }; - } - - protected async findItem(context: types.IActionContext, query: types.AzExtItemQuery): Promise { - return await this._tree.findTreeItem(query.id, context); - } - - private getUriFromItem(item: TItem): Uri { - const data: types.AzExtItemUriParts = this.getUriParts(item); const query: string = stringifyQuery(data.query); const filePath: string = encodeURIComponent(data.filePath); return Uri.parse(`${this.scheme}:///${filePath}?${query}`); } private async lookup(context: types.IActionContext, uri: Uri): Promise { - const item: TItem | undefined = await this.findItem(context, this.getQueryFromUri(uri)); + const item: TItem | undefined = this.findItem(this.getQueryFromUri(uri)); if (!item) { context.telemetry.suppressAll = true; context.errorHandling.rethrow = true;