11'use strict' ;
22import { Functions , Iterables } from '../system' ;
33import { ConfigurationChangeEvent , DecorationRangeBehavior , DecorationRenderOptions , Disposable , Event , EventEmitter , OverviewRulerLane , Progress , ProgressLocation , TextDocument , TextEditor , TextEditorDecorationType , TextEditorViewColumnChangeEvent , ThemeColor , window , workspace } from 'vscode' ;
4- import { AnnotationProviderBase , TextEditorCorrelationKey } from './annotationProvider' ;
4+ import { AnnotationProviderBase , AnnotationStatus , TextEditorCorrelationKey } from './annotationProvider' ;
55import { configuration , FileAnnotationType , IConfig , LineHighlightLocations } from '../configuration' ;
66import { CommandContext , isTextEditor , setCommandContext } from '../constants' ;
77import { Container } from '../container' ;
@@ -23,11 +23,6 @@ export enum AnnotationClearReason {
2323 DocumentClosed = 'DocumentClosed'
2424}
2525
26- enum AnnotationStatus {
27- Computing = 'computing' ,
28- Computed = 'computed'
29- }
30-
3126export const Decorations = {
3227 blameAnnotation : window . createTextEditorDecorationType ( {
3328 isWholeLine : true ,
@@ -49,6 +44,7 @@ export class AnnotationController extends Disposable {
4944 private _annotationsDisposable : Disposable | undefined ;
5045 private _annotationProviders : Map < TextEditorCorrelationKey , AnnotationProviderBase > = new Map ( ) ;
5146 private _disposable : Disposable ;
47+ private _editor : TextEditor | undefined ;
5248 private _keyboardScope : KeyboardScope | undefined = undefined ;
5349
5450 constructor ( ) {
@@ -174,6 +170,7 @@ export class AnnotationController extends Disposable {
174170 private onActiveTextEditorChanged ( editor : TextEditor | undefined ) {
175171 if ( editor !== undefined && ! isTextEditor ( editor ) ) return ;
176172
173+ this . _editor = editor ;
177174 // Logger.log('AnnotationController.onActiveTextEditorChanged', editor && editor.document.uri.fsPath);
178175
179176 const provider = this . getProvider ( editor ) ;
@@ -182,7 +179,7 @@ export class AnnotationController extends Disposable {
182179 this . detachKeyboardHook ( ) ;
183180 }
184181 else {
185- setCommandContext ( CommandContext . AnnotationStatus , AnnotationStatus . Computed ) ;
182+ setCommandContext ( CommandContext . AnnotationStatus , provider . status ) ;
186183 this . attachKeyboardHook ( ) ;
187184 }
188185 }
@@ -262,6 +259,7 @@ export class AnnotationController extends Disposable {
262259
263260 async showAnnotations ( editor : TextEditor | undefined , type : FileAnnotationType , shaOrLine ?: string | number ) : Promise < boolean > {
264261 if ( editor === undefined ) return false ; // || editor.viewColumn === undefined) return false;
262+ this . _editor = editor ;
265263
266264 const trackedDocument = await Container . tracker . getOrAdd ( editor . document ) ;
267265 if ( ! trackedDocument . isBlameable ) return false ;
@@ -272,19 +270,20 @@ export class AnnotationController extends Disposable {
272270 return true ;
273271 }
274272
275- return window . withProgress ( { location : ProgressLocation . Window } , async ( progress : Progress < { message : string } > ) => {
276- const active = editor === window . activeTextEditor ;
277- await setCommandContext ( CommandContext . AnnotationStatus , active ? AnnotationStatus . Computing : undefined ) ;
273+ const provider = await window . withProgress ( { location : ProgressLocation . Window } , async ( progress : Progress < { message : string } > ) => {
274+ await setCommandContext ( CommandContext . AnnotationStatus , AnnotationStatus . Computing ) ;
278275
279276 const computingAnnotations = this . showAnnotationsCore ( currentProvider , editor , type , shaOrLine , progress ) ;
280- const result = await computingAnnotations ;
277+ const provider = await computingAnnotations ;
281278
282- if ( active ) {
283- await setCommandContext ( CommandContext . AnnotationStatus , result ? AnnotationStatus . Computed : undefined ) ;
279+ if ( editor === this . _editor ) {
280+ await setCommandContext ( CommandContext . AnnotationStatus , provider && provider . status ) ;
284281 }
285282
286283 return computingAnnotations ;
287284 } ) ;
285+
286+ return provider !== undefined ;
288287 }
289288
290289 async toggleAnnotations ( editor : TextEditor | undefined , type : FileAnnotationType , shaOrLine ?: string | number ) : Promise < boolean > {
@@ -310,7 +309,7 @@ export class AnnotationController extends Disposable {
310309 this . _keyboardScope = await Container . keyboard . beginScope ( {
311310 escape : {
312311 onDidPressKey : async ( key : Keys ) => {
313- const e = window . activeTextEditor ;
312+ const e = this . _editor ;
314313 if ( e === undefined ) return undefined ;
315314
316315 await this . clear ( e , AnnotationClearReason . User ) ;
@@ -330,7 +329,7 @@ export class AnnotationController extends Disposable {
330329 this . _annotationProviders . delete ( key ) ;
331330 await provider . dispose ( ) ;
332331
333- if ( key === AnnotationProviderBase . getCorrelationKey ( window . activeTextEditor ) ) {
332+ if ( this . _annotationProviders . size === 0 || key === AnnotationProviderBase . getCorrelationKey ( this . _editor ) ) {
334333 await setCommandContext ( CommandContext . AnnotationStatus , undefined ) ;
335334 await this . detachKeyboardHook ( ) ;
336335 }
@@ -352,7 +351,7 @@ export class AnnotationController extends Disposable {
352351 this . _keyboardScope = undefined ;
353352 }
354353
355- private async showAnnotationsCore ( currentProvider : AnnotationProviderBase | undefined , editor : TextEditor , type : FileAnnotationType , shaOrLine ?: string | number , progress ?: Progress < { message : string } > ) : Promise < boolean > {
354+ private async showAnnotationsCore ( currentProvider : AnnotationProviderBase | undefined , editor : TextEditor , type : FileAnnotationType , shaOrLine ?: string | number , progress ?: Progress < { message : string } > ) : Promise < AnnotationProviderBase | undefined > {
356355 if ( progress !== undefined ) {
357356 let annotationsLabel = 'annotations' ;
358357 switch ( type ) {
@@ -396,7 +395,7 @@ export class AnnotationController extends Disposable {
396395 provider = new RecentChangesAnnotationProvider ( editor , trackedDocument , undefined , Decorations . recentChangesHighlight ! ) ;
397396 break ;
398397 }
399- if ( provider === undefined || ! ( await provider . validate ( ) ) ) return false ;
398+ if ( provider === undefined || ! ( await provider . validate ( ) ) ) return undefined ;
400399
401400 if ( currentProvider !== undefined ) {
402401 await this . clearCore ( currentProvider . correlationKey , AnnotationClearReason . User ) ;
@@ -408,7 +407,7 @@ export class AnnotationController extends Disposable {
408407 this . _annotationsDisposable = Disposable . from (
409408 window . onDidChangeActiveTextEditor ( Functions . debounce ( this . onActiveTextEditorChanged , 50 ) , this ) ,
410409 window . onDidChangeTextEditorViewColumn ( this . onTextEditorViewColumnChanged , this ) ,
411- window . onDidChangeVisibleTextEditors ( this . onVisibleTextEditorsChanged , this ) ,
410+ window . onDidChangeVisibleTextEditors ( Functions . debounce ( this . onVisibleTextEditorsChanged , 50 ) , this ) ,
412411 workspace . onDidCloseTextDocument ( this . onTextDocumentClosed , this ) ,
413412 Container . tracker . onDidChangeBlameState ( this . onBlameStateChanged , this ) ,
414413 Container . tracker . onDidChangeDirtyState ( this . onDirtyStateChanged , this )
@@ -418,9 +417,9 @@ export class AnnotationController extends Disposable {
418417 this . _annotationProviders . set ( provider . correlationKey , provider ) ;
419418 if ( await provider . provideAnnotation ( shaOrLine ) ) {
420419 this . _onDidToggleAnnotations . fire ( ) ;
421- return true ;
420+ return provider ;
422421 }
423422
424- return false ;
423+ return undefined ;
425424 }
426425}
0 commit comments