11'use strict' ;
2- import { Functions } from '../system' ;
2+ import { Functions , IDeferred } from '../system' ;
33import { ConfigurationChangeEvent , Disposable , Event , EventEmitter , TextDocumentChangeEvent , TextEditor , window , workspace } from 'vscode' ;
44import { TextDocumentComparer } from '../comparers' ;
55import { configuration } from '../configuration' ;
@@ -44,16 +44,19 @@ export class GitContextTracker extends Disposable {
4444 private readonly _context : Context = { state : { dirty : false } } ;
4545 private readonly _disposable : Disposable ;
4646 private _listenersDisposable : Disposable | undefined ;
47+ private _onDirtyStateChangedDebounced : ( ( dirty : boolean ) => void ) & IDeferred ;
4748
4849 constructor (
4950 private readonly git : GitService
5051 ) {
5152 super ( ( ) => this . dispose ( ) ) ;
5253
54+ this . _onDirtyStateChangedDebounced = Functions . debounce ( this . onDirtyStateChanged , 250 ) ;
55+
5356 this . _disposable = Disposable . from (
5457 workspace . onDidChangeConfiguration ( this . onConfigurationChanged , this )
5558 ) ;
56- this . onConfigurationChanged ( configuration . initializingChangeEvent ) ;
59+ this . onConfigurationChanged ( configuration . initializingChangeEvent ) ;
5760 }
5861
5962 dispose ( ) {
@@ -75,7 +78,7 @@ export class GitContextTracker extends Disposable {
7578 if ( enabled ) {
7679 this . _listenersDisposable = Disposable . from (
7780 window . onDidChangeActiveTextEditor ( Functions . debounce ( this . onActiveTextEditorChanged , 50 ) , this ) ,
78- workspace . onDidChangeTextDocument ( Functions . debounce ( this . onTextDocumentChanged , 50 ) , this ) ,
81+ workspace . onDidChangeTextDocument ( this . onTextDocumentChanged , this ) ,
7982 this . git . onDidBlameFail ( this . onBlameFailed , this ) ,
8083 this . git . onDidChange ( this . onGitChanged , this )
8184 ) ;
@@ -102,6 +105,11 @@ export class GitContextTracker extends Disposable {
102105 this . updateBlameability ( BlameabilityChangeReason . BlameFailed , false ) ;
103106 }
104107
108+ private onDirtyStateChanged ( dirty : boolean ) {
109+ this . _context . state . dirty = dirty ;
110+ this . updateBlameability ( BlameabilityChangeReason . DocumentChanged ) ;
111+ }
112+
105113 private onGitChanged ( e : GitChangeEvent ) {
106114 if ( e . reason !== GitChangeReason . Repositories ) return ;
107115
@@ -116,13 +124,25 @@ export class GitContextTracker extends Disposable {
116124 private onTextDocumentChanged ( e : TextDocumentChangeEvent ) {
117125 if ( this . _context . editor === undefined || ! TextDocumentComparer . equals ( this . _context . editor . document , e . document ) ) return ;
118126
127+ const dirty = e . document . isDirty ;
128+
119129 // If we haven't changed state, kick out
120- if ( this . _context . state . dirty === e . document . isDirty ) return ;
130+ if ( dirty === this . _context . state . dirty ) {
131+ this . _onDirtyStateChangedDebounced . cancel ( ) ;
121132
122- // Logger.log('GitContextTracker.onTextDocumentChanged', 'Dirty state changed', e);
133+ return ;
134+ }
123135
124- this . _context . state . dirty = e . document . isDirty ;
125- this . updateBlameability ( BlameabilityChangeReason . DocumentChanged ) ;
136+ // Logger.log('GitContextTracker.onTextDocumentChanged', `Dirty(${dirty}) state changed`);
137+
138+ if ( dirty ) {
139+ this . _onDirtyStateChangedDebounced . cancel ( ) ;
140+ this . onDirtyStateChanged ( dirty ) ;
141+
142+ return ;
143+ }
144+
145+ this . _onDirtyStateChangedDebounced ( dirty ) ;
126146 }
127147
128148 private async updateContext ( reason : BlameabilityChangeReason , editor : TextEditor | undefined , force : boolean = false ) {
0 commit comments