Skip to content

Commit

Permalink
Don't use findTreeItem in AzExtTreeFileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
alexweininger committed Dec 8, 2022
1 parent 1043e7d commit 49904b4
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions utils/src/AzExtTreeFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,26 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { randomUUID } from "crypto";
import { parse as parseQuery, ParsedUrlQuery, stringify as stringifyQuery } from "querystring";
import { Disposable, Event, EventEmitter, FileChangeEvent, FileStat, FileSystemError, FileSystemProvider, FileType, TextDocumentShowOptions, Uri, window } from "vscode";
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<TItem extends AzExtTreeItem> implements FileSystemProvider {
export abstract class AzExtTreeFileSystem<TItem> implements FileSystemProvider {

private readonly itemCache: Map<string, TItem> = new Map<string, TItem>();

public abstract scheme: string;

private readonly _emitter: EventEmitter<FileChangeEvent[]> = new EventEmitter<FileChangeEvent[]>();
private readonly _bufferedEvents: FileChangeEvent[] = [];
private _fireSoonHandle?: NodeJS.Timer;

private _tree: AzExtTreeDataProvider;

public constructor(tree: AzExtTreeDataProvider) {
this._tree = tree;
}

public get onDidChangeFile(): Event<FileChangeEvent[]> {
return this._emitter.event;
}
Expand All @@ -37,7 +33,10 @@ export abstract class AzExtTreeFileSystem<TItem extends AzExtTreeItem> implement
public abstract getFilePath(item: TItem): string;

public async showTextDocument(item: TItem, options?: TextDocumentShowOptions): Promise<void> {
await window.showTextDocument(this.getUriFromItem(item), options);
const id = randomUUID();
const uri = this.getUriFromItem(item, id);
this.itemCache.set(id, item);
await window.showTextDocument(uri, options);
}

public watch(): Disposable {
Expand Down Expand Up @@ -70,7 +69,7 @@ export abstract class AzExtTreeFileSystem<TItem extends AzExtTreeItem> 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);
});
}

Expand Down Expand Up @@ -98,7 +97,7 @@ export abstract class AzExtTreeFileSystem<TItem extends AzExtTreeItem> implement
this._bufferedEvents.push(...events.map(e => {
return {
type: e.type,
uri: this.getUriFromItem(e.item)
uri: this.getUriFromItem(e.item, '')
};
}));

Expand All @@ -115,28 +114,24 @@ export abstract class AzExtTreeFileSystem<TItem extends AzExtTreeItem> 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, id: string): Uri {
const data: types.AzExtItemUriParts = {
filePath: this.getFilePath(item),
query: {
id: item.fullId
id
}
};
}

protected async findItem(context: types.IActionContext, query: types.AzExtItemQuery): Promise<TItem | undefined> {
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<TItem> {
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;
Expand Down

0 comments on commit 49904b4

Please sign in to comment.