11'use strict' ;
2- import { Disposable , ExtensionContext , TextDocument , TextEditor , TextEditorDecorationType , TextEditorSelectionChangeEvent , window , workspace } from 'vscode' ;
2+ import { DecorationOptions , Disposable , ExtensionContext , TextDocument , TextEditor , TextEditorDecorationType , TextEditorSelectionChangeEvent , Uri , window , workspace } from 'vscode' ;
33import { FileAnnotationType } from '../annotations/annotationController' ;
44import { TextDocumentComparer } from '../comparers' ;
55import { ExtensionKey , IConfig } from '../configuration' ;
66
7- export abstract class AnnotationProviderBase extends Disposable {
7+ export type TextEditorCorrelationKey = string ;
8+
9+ export abstract class AnnotationProviderBase extends Disposable {
10+
11+ static getCorrelationKey ( editor : TextEditor | undefined ) : TextEditorCorrelationKey {
12+ return editor !== undefined ? ( editor as any ) . id : '' ;
13+ }
814
915 public annotationType : FileAnnotationType ;
16+ public correlationKey : TextEditorCorrelationKey ;
1017 public document : TextDocument ;
1118
1219 protected _config : IConfig ;
20+ protected _decorations : DecorationOptions [ ] | undefined ;
1321 protected _disposable : Disposable ;
1422
1523 constructor (
@@ -20,14 +28,14 @@ import { ExtensionKey, IConfig } from '../configuration';
2028 ) {
2129 super ( ( ) => this . dispose ( ) ) ;
2230
31+ this . correlationKey = AnnotationProviderBase . getCorrelationKey ( this . editor ) ;
2332 this . document = this . editor . document ;
2433
2534 this . _config = workspace . getConfiguration ( ) . get < IConfig > ( ExtensionKey ) ! ;
2635
27- const subscriptions : Disposable [ ] = [ ] ;
28-
29- subscriptions . push ( window . onDidChangeTextEditorSelection ( this . _onTextEditorSelectionChanged , this ) ) ;
30-
36+ const subscriptions : Disposable [ ] = [
37+ window . onDidChangeTextEditorSelection ( this . _onTextEditorSelectionChanged , this )
38+ ] ;
3139 this . _disposable = Disposable . from ( ...subscriptions ) ;
3240 }
3341
@@ -43,6 +51,16 @@ import { ExtensionKey, IConfig } from '../configuration';
4351 return this . selection ( e . selections [ 0 ] . active . line ) ;
4452 }
4553
54+ get editorId ( ) : string {
55+ if ( this . editor === undefined || this . editor . document === undefined ) return '' ;
56+ return ( this . editor as any ) . id ;
57+ }
58+
59+ get editorUri ( ) : Uri | undefined {
60+ if ( this . editor === undefined || this . editor . document === undefined ) return undefined ;
61+ return this . editor . document . uri ;
62+ }
63+
4664 async clear ( ) {
4765 if ( this . editor !== undefined ) {
4866 try {
@@ -68,6 +86,20 @@ import { ExtensionKey, IConfig } from '../configuration';
6886 await this . provideAnnotation ( this . editor === undefined ? undefined : this . editor . selection . active . line ) ;
6987 }
7088
89+ restore ( editor : TextEditor , force : boolean = false ) {
90+ // If the editor isn't disposed then we don't need to do anything
91+ // Explicitly check for `false`
92+ if ( ! force && ( this . editor as any ) . _disposed === false ) return ;
93+
94+ this . editor = editor ;
95+ this . correlationKey = AnnotationProviderBase . getCorrelationKey ( editor ) ;
96+ this . document = editor . document ;
97+
98+ if ( this . _decorations !== undefined && this . _decorations . length ) {
99+ this . editor . setDecorations ( this . decoration ! , this . _decorations ) ;
100+ }
101+ }
102+
71103 abstract async provideAnnotation ( shaOrLine ?: string | number ) : Promise < boolean > ;
72104 abstract async selection ( shaOrLine ?: string | number ) : Promise < void > ;
73105 abstract async validate ( ) : Promise < boolean > ;
0 commit comments