From e7b523a75e6f14dd55bbeb491a8842cf94a27ff8 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Fri, 29 Jul 2022 03:11:47 -0400 Subject: [PATCH] Reworks document tracker to only track by object Removes unused key property --- src/env/node/git/localGitProvider.ts | 14 ++++++------ src/plus/github/githubGitProvider.ts | 4 ++-- src/system/utils.ts | 5 +++++ src/trackers/documentTracker.ts | 43 +++++++++++++++--------------------- src/trackers/gitDocumentTracker.ts | 2 -- src/trackers/trackedDocument.ts | 6 ++--- 6 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/env/node/git/localGitProvider.ts b/src/env/node/git/localGitProvider.ts index 82bec1d..abf18c3 100644 --- a/src/env/node/git/localGitProvider.ts +++ b/src/env/node/git/localGitProvider.ts @@ -1084,7 +1084,7 @@ export class LocalGitProvider implements GitProvider, Disposable { key += `:${uri.sha}`; } - const doc = await this.container.tracker.getOrAdd(uri); + const doc = await this.container.tracker.getOrAdd(document ?? uri); if (this.useCaching) { if (doc.state != null) { const cachedBlame = doc.state.getBlame(key); @@ -1097,7 +1097,7 @@ export class LocalGitProvider implements GitProvider, Disposable { Logger.debug(cc, `Cache miss: '${key}'`); if (doc.state == null) { - doc.state = new GitDocumentState(doc.key); + doc.state = new GitDocumentState(); } } @@ -1176,7 +1176,7 @@ export class LocalGitProvider implements GitProvider, Disposable { Logger.debug(cc, `Cache miss: ${key}`); if (doc.state == null) { - doc.state = new GitDocumentState(doc.key); + doc.state = new GitDocumentState(); } } @@ -1248,7 +1248,7 @@ export class LocalGitProvider implements GitProvider, Disposable { if (document?.isDirty) return this.getBlameForLineContents(uri, editorLine, document.getText(), options); if (!options?.forceSingleLine && this.useCaching) { - const blame = await this.getBlame(uri); + const blame = await this.getBlame(uri, document); if (blame == null) return undefined; let blameLine = blame.lines[editorLine]; @@ -1841,7 +1841,7 @@ export class LocalGitProvider implements GitProvider, Disposable { Logger.debug(cc, `Cache miss: '${key}'`); if (doc.state == null) { - doc.state = new GitDocumentState(doc.key); + doc.state = new GitDocumentState(); } } @@ -1929,7 +1929,7 @@ export class LocalGitProvider implements GitProvider, Disposable { Logger.debug(cc, `Cache miss: ${key}`); if (doc.state == null) { - doc.state = new GitDocumentState(doc.key); + doc.state = new GitDocumentState(); } } @@ -2546,7 +2546,7 @@ export class LocalGitProvider implements GitProvider, Disposable { Logger.debug(cc, `Cache miss: '${key}'`); if (doc.state == null) { - doc.state = new GitDocumentState(doc.key); + doc.state = new GitDocumentState(); } } diff --git a/src/plus/github/githubGitProvider.ts b/src/plus/github/githubGitProvider.ts index d7d6dee..396da7f 100644 --- a/src/plus/github/githubGitProvider.ts +++ b/src/plus/github/githubGitProvider.ts @@ -433,7 +433,7 @@ export class GitHubGitProvider implements GitProvider, Disposable { Logger.debug(cc, `Cache miss: '${key}'`); if (doc.state == null) { - doc.state = new GitDocumentState(doc.key); + doc.state = new GitDocumentState(); } const promise = this.getBlameCore(uri, doc, key, cc); @@ -1716,7 +1716,7 @@ export class GitHubGitProvider implements GitProvider, Disposable { Logger.debug(cc, `Cache miss: '${key}'`); if (doc.state == null) { - doc.state = new GitDocumentState(doc.key); + doc.state = new GitDocumentState(); } } diff --git a/src/system/utils.ts b/src/system/utils.ts index ef37cd4..97b54fb 100644 --- a/src/system/utils.ts +++ b/src/system/utils.ts @@ -6,6 +6,11 @@ import { Logger } from '../logger'; import { executeCoreCommand } from './command'; import { extname } from './path'; +export function findTextDocument(uri: Uri): TextDocument | undefined { + const normalizedUri = uri.toString(); + return workspace.textDocuments.find(d => d.uri.toString() === normalizedUri); +} + export function findEditor(uri: Uri): TextEditor | undefined { const active = window.activeTextEditor; const normalizedUri = uri.toString(); diff --git a/src/trackers/documentTracker.ts b/src/trackers/documentTracker.ts index ae6c281..88fe5dd 100644 --- a/src/trackers/documentTracker.ts +++ b/src/trackers/documentTracker.ts @@ -26,8 +26,7 @@ import { debug } from '../system/decorators/log'; import { once } from '../system/event'; import { debounce, Deferrable } from '../system/function'; import { filter, join, map } from '../system/iterable'; -import { getBestPath } from '../system/path'; -import { isActiveDocument, isTextEditor } from '../system/utils'; +import { findTextDocument, isActiveDocument, isTextEditor } from '../system/utils'; import { DocumentBlameStateChangeEvent, TrackedDocument } from './trackedDocument'; export * from './trackedDocument'; @@ -72,8 +71,7 @@ export class DocumentTracker implements Disposable { private _dirtyIdleTriggerDelay: number; private readonly _disposable: Disposable; - // TODO@eamodio: replace with a trie? - protected readonly _documentMap = new Map>>(); + protected readonly _documentMap = new Map>>(); constructor(protected readonly container: Container) { this._disposable = Disposable.from( @@ -273,12 +271,9 @@ export class DocumentTracker implements Disposable { } private async addCore(document: TextDocument): Promise> { - const key = getUriKey(document.uri); - - // Always start out false, so we will fire the event if needed const doc = TrackedDocument.create( document, - key, + // Always start out false, so we will fire the event if needed false, { onDidBlameStateChange: (e: DocumentBlameStateChangeEvent) => this._onDidChangeBlameState.fire(e), @@ -287,7 +282,6 @@ export class DocumentTracker implements Disposable { ); this._documentMap.set(document, doc); - this._documentMap.set(key, doc); return doc; } @@ -304,22 +298,22 @@ export class DocumentTracker implements Disposable { get(uri: Uri): Promise> | undefined; get(documentOrUri: TextDocument | Uri): Promise> | undefined; get(documentOrUri: TextDocument | Uri): Promise> | undefined { - let key; - if (GitUri.is(documentOrUri)) { - key = getUriKey(documentOrUri.documentUri()); - } else if (documentOrUri instanceof Uri) { - key = getUriKey(documentOrUri); - } else { - key = documentOrUri; + if (documentOrUri instanceof Uri) { + const document = findTextDocument(documentOrUri); + if (document == null) return undefined; + + documentOrUri = document; } - const doc = this._documentMap.get(key); + const doc = this._documentMap.get(documentOrUri); return doc; } - getOrAdd(document: TextDocument): Promise>; - getOrAdd(uri: Uri): Promise>; async getOrAdd(documentOrUri: TextDocument | Uri): Promise> { + if (documentOrUri instanceof Uri) { + documentOrUri = findTextDocument(documentOrUri) ?? documentOrUri; + } + const doc = this.get(documentOrUri) ?? this.add(documentOrUri); return doc; } @@ -328,8 +322,12 @@ export class DocumentTracker implements Disposable { has(uri: Uri): boolean; has(documentOrUri: TextDocument | Uri): boolean { if (documentOrUri instanceof Uri) { - return this._documentMap.has(getUriKey(documentOrUri)); + const document = findTextDocument(documentOrUri); + if (document == null) return false; + + documentOrUri = document; } + return this._documentMap.has(documentOrUri); } @@ -340,7 +338,6 @@ export class DocumentTracker implements Disposable { } this._documentMap.delete(document); - this._documentMap.delete(getUriKey(document.uri)); (tracked ?? (await promise))?.dispose(); } @@ -475,7 +472,3 @@ class EmptyTextDocument implements TextDocument { class BinaryTextDocument extends EmptyTextDocument {} class MissingRevisionTextDocument extends EmptyTextDocument {} - -function getUriKey(pathOrUri: string | Uri): string { - return getBestPath(pathOrUri); -} diff --git a/src/trackers/gitDocumentTracker.ts b/src/trackers/gitDocumentTracker.ts index cc92cdf..89a2244 100644 --- a/src/trackers/gitDocumentTracker.ts +++ b/src/trackers/gitDocumentTracker.ts @@ -18,8 +18,6 @@ export class GitDocumentState { private readonly diffCache = new Map(); private readonly logCache = new Map(); - constructor(public readonly key: string) {} - clearBlame(key?: string): void { if (key == null) { this.blameCache.clear(); diff --git a/src/trackers/trackedDocument.ts b/src/trackers/trackedDocument.ts index e3fe26f..cb02190 100644 --- a/src/trackers/trackedDocument.ts +++ b/src/trackers/trackedDocument.ts @@ -17,12 +17,11 @@ export interface DocumentBlameStateChangeEvent { export class TrackedDocument implements Disposable { static async create( document: TextDocument, - key: string, dirty: boolean, eventDelegates: { onDidBlameStateChange(e: DocumentBlameStateChangeEvent): void }, container: Container, ) { - const doc = new TrackedDocument(document, key, dirty, eventDelegates, container); + const doc = new TrackedDocument(document, dirty, eventDelegates, container); await doc.initialize(); return doc; } @@ -40,7 +39,6 @@ export class TrackedDocument implements Disposable { private constructor( readonly document: TextDocument, - public readonly key: string, public dirty: boolean, private _eventDelegates: { onDidBlameStateChange(e: DocumentBlameStateChangeEvent): void }, private readonly container: Container, @@ -126,7 +124,7 @@ export class TrackedDocument implements Disposable { if (this.state != null) { this.state = undefined; - Logger.log(`Reset state for '${this.key}', reason=${reason}`); + Logger.log(`Reset state for '${this.document.uri.toString(true)}', reason=${reason}`); } if (reason === 'repository' && isActiveDocument(this.document)) {