From 964cfd363e42248f297a9d44e98f87f49156ba75 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 6 Feb 2017 15:56:39 -0500 Subject: [PATCH] Fixes updating CodeLens after file save Fixes updating active line annotations after file save --- src/blameStatusBarController.ts | 6 ++++++ src/gitCodeLensProvider.ts | 12 ++++++++++- src/gitProvider.ts | 46 +++++++++++++++++++++++++++-------------- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/blameStatusBarController.ts b/src/blameStatusBarController.ts index cb8e8af..6f8a6f2 100644 --- a/src/blameStatusBarController.ts +++ b/src/blameStatusBarController.ts @@ -34,6 +34,7 @@ export default class BlameStatusBarController extends Disposable { const subscriptions: Disposable[] = []; subscriptions.push(workspace.onDidChangeConfiguration(this._onConfigure, this)); + subscriptions.push(git.onDidRemoveCacheEntry(this._onRemoveCacheEntry, this)); this._disposable = Disposable.from(...subscriptions); } @@ -98,6 +99,11 @@ export default class BlameStatusBarController extends Disposable { this._onActiveTextEditorChanged(window.activeTextEditor); } + private _onRemoveCacheEntry() { + this._blame = undefined; + this._onActiveTextEditorChanged(window.activeTextEditor); + } + private async _onActiveTextEditorChanged(e: TextEditor): Promise { const previousEditor = this._editor; previousEditor && previousEditor.setDecorations(activeLineDecoration, []); diff --git a/src/gitCodeLensProvider.ts b/src/gitCodeLensProvider.ts index 2e0eced..9d89210 100644 --- a/src/gitCodeLensProvider.ts +++ b/src/gitCodeLensProvider.ts @@ -1,6 +1,6 @@ 'use strict'; import { Functions, Iterables, Strings } from './system'; -import { CancellationToken, CodeLens, CodeLensProvider, commands, DocumentSelector, ExtensionContext, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri, workspace } from 'vscode'; +import { CancellationToken, CodeLens, CodeLensProvider, commands, DocumentSelector, Event, EventEmitter, ExtensionContext, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri, workspace } from 'vscode'; import { BuiltInCommands, Commands, DocumentSchemes } from './constants'; import { CodeLensCommand, CodeLensLocation, IConfig, ICodeLensLanguageLocation } from './configuration'; import GitProvider, { GitCommit, GitUri, IGitBlame, IGitBlameLines } from './gitProvider'; @@ -31,6 +31,11 @@ export class GitAuthorsCodeLens extends CodeLens { export default class GitCodeLensProvider implements CodeLensProvider { + private _onDidChangeCodeLensesEmitter = new EventEmitter(); + public get onDidChangeCodeLenses(): Event { + return this._onDidChangeCodeLensesEmitter.event; + } + static selector: DocumentSelector = { scheme: DocumentSchemes.File }; private _config: IConfig; @@ -39,6 +44,11 @@ export default class GitCodeLensProvider implements CodeLensProvider { this._config = workspace.getConfiguration('').get('gitlens'); } + reset() { + Logger.log('Triggering a reset of the git CodeLens provider'); + this._onDidChangeCodeLensesEmitter.fire(); + } + async provideCodeLenses(document: TextDocument, token: CancellationToken): Promise { let languageLocations = this._config.codeLens.languageLocations.find(_ => _.language.toLowerCase() === document.languageId); if (languageLocations == null) { diff --git a/src/gitProvider.ts b/src/gitProvider.ts index b5bc229..e2ea2b0 100644 --- a/src/gitProvider.ts +++ b/src/gitProvider.ts @@ -1,6 +1,6 @@ 'use strict'; -import { Functions, Iterables, Objects } from './system'; -import { Disposable, ExtensionContext, languages, Location, Position, Range, TextDocument, TextEditor, Uri, workspace } from 'vscode'; +import { /*Functions,*/ Iterables, Objects } from './system'; +import { Disposable, Event, EventEmitter, ExtensionContext, languages, Location, Position, Range, TextDocument, TextEditor, Uri, workspace } from 'vscode'; import { CodeLensVisibility, IConfig } from './configuration'; import { DocumentSchemes, WorkspaceState } from './constants'; import Git, { GitBlameParserEnricher, GitBlameFormat, GitCommit, GitLogParserEnricher, IGitAuthor, IGitBlame, IGitBlameLine, IGitBlameLines, IGitLog } from './git/git'; @@ -41,20 +41,25 @@ interface ICachedLog extends ICachedItem { } enum RemoveCacheReason { DocumentClosed, - DocumentSaved, - DocumentChanged + DocumentSaved } export default class GitProvider extends Disposable { + private _onDidRemoveCacheEntryEmitter = new EventEmitter(); + get onDidRemoveCacheEntry(): Event { + return this._onDidRemoveCacheEntryEmitter.event; + } + private _gitCache: Map | undefined; private _cacheDisposable: Disposable | undefined; private _repoPath: string; private _uriCache: Map | undefined; config: IConfig; - private _disposable: Disposable; + private _codeLensProvider: GitCodeLensProvider | undefined; private _codeLensProviderDisposable: Disposable | undefined; + private _disposable: Disposable; private _gitignore: Promise; static EmptyPromise: Promise = Promise.resolve(undefined); @@ -76,7 +81,11 @@ export default class GitProvider extends Disposable { dispose() { this._disposable && this._disposable.dispose(); + this._codeLensProviderDisposable && this._codeLensProviderDisposable.dispose(); + this._codeLensProviderDisposable = undefined; + this._codeLensProvider = undefined; + this._cacheDisposable && this._cacheDisposable.dispose(); this._uriCache && this._uriCache.clear(); this._gitCache && this._gitCache.clear(); @@ -98,12 +107,20 @@ export default class GitProvider extends Disposable { if (codeLensChanged || advancedChanged) { Logger.log('CodeLens config changed; resetting CodeLens provider'); - this._codeLensProviderDisposable && this._codeLensProviderDisposable.dispose(); + // this._codeLensProviderDisposable && this._codeLensProviderDisposable.dispose(); if (config.codeLens.visibility === CodeLensVisibility.Auto && (config.codeLens.recentChange.enabled || config.codeLens.authors.enabled)) { - this._codeLensProviderDisposable = languages.registerCodeLensProvider(GitCodeLensProvider.selector, new GitCodeLensProvider(this.context, this)); + if (this._codeLensProvider) { + this._codeLensProvider.reset(); + } + else { + this._codeLensProvider = new GitCodeLensProvider(this.context, this); + this._codeLensProviderDisposable = languages.registerCodeLensProvider(GitCodeLensProvider.selector, this._codeLensProvider); + } } else { + this._codeLensProviderDisposable && this._codeLensProviderDisposable.dispose(); this._codeLensProviderDisposable = undefined; + this._codeLensProvider = undefined; } } @@ -117,10 +134,7 @@ export default class GitProvider extends Disposable { // TODO: Maybe stop clearing on close and instead limit to a certain number of recent blames disposables.push(workspace.onDidCloseTextDocument(d => this._removeCachedEntry(d, RemoveCacheReason.DocumentClosed))); - - const removeCachedEntryFn = Functions.debounce(this._removeCachedEntry.bind(this), 2500); - disposables.push(workspace.onDidSaveTextDocument(d => removeCachedEntryFn(d, RemoveCacheReason.DocumentSaved))); - disposables.push(workspace.onDidChangeTextDocument(e => removeCachedEntryFn(e.document, RemoveCacheReason.DocumentChanged))); + disposables.push(workspace.onDidSaveTextDocument(d => this._removeCachedEntry(d, RemoveCacheReason.DocumentSaved))); this._cacheDisposable = Disposable.from(...disposables); } @@ -188,10 +202,12 @@ export default class GitProvider extends Disposable { if (this._gitCache.delete(cacheKey)) { Logger.log(`Clear cache entry for '${cacheKey}', reason=${RemoveCacheReason[reason]}`); - // if (reason === RemoveCacheReason.DocumentSaved) { - // // TODO: Killing the code lens provider is too drastic -- makes the editor jump around, need to figure out how to trigger a refresh - // this._registerCodeLensProvider(); - // } + if (reason === RemoveCacheReason.DocumentSaved) { + this._onDidRemoveCacheEntryEmitter.fire(); + + // Refresh the codelenses with the update blame + this._codeLensProvider && this._codeLensProvider.reset(); + } } }