@@ -26,32 +26,60 @@ class ModelWorkerTextMateTokenizer extends Disposable {
2626
2727 private readonly _worker : TextMateWorker ;
2828 private readonly _model : ITextModel ;
29+ private _isSynced : boolean ;
2930
3031 constructor ( worker : TextMateWorker , model : ITextModel ) {
3132 super ( ) ;
3233 this . _worker = worker ;
3334 this . _model = model ;
35+ this . _isSynced = false ;
3436
37+ this . _register ( this . _model . onDidChangeAttached ( ( ) => this . _onDidChangeAttached ( ) ) ) ;
38+ this . _onDidChangeAttached ( ) ;
39+
40+ this . _register ( this . _model . onDidChangeContent ( ( e ) => {
41+ if ( this . _isSynced ) {
42+ this . _worker . acceptModelChanged ( this . _model . uri . toString ( ) , e ) ;
43+ }
44+ } ) ) ;
45+
46+ this . _register ( this . _model . onDidChangeLanguage ( ( e ) => {
47+ if ( this . _isSynced ) {
48+ this . _worker . acceptModelLanguageChanged ( this . _model . uri . toString ( ) , this . _model . getLanguageIdentifier ( ) . id ) ;
49+ }
50+ } ) ) ;
51+ }
52+
53+ private _onDidChangeAttached ( ) : void {
54+ if ( this . _model . isAttachedToEditor ( ) ) {
55+ if ( ! this . _isSynced ) {
56+ this . _beginSync ( ) ;
57+ }
58+ } else {
59+ if ( this . _isSynced ) {
60+ this . _endSync ( ) ;
61+ }
62+ }
63+ }
64+
65+ private _beginSync ( ) : void {
66+ this . _isSynced = true ;
3567 this . _worker . acceptNewModel ( {
3668 uri : this . _model . uri ,
3769 versionId : this . _model . getVersionId ( ) ,
3870 lines : this . _model . getLinesContent ( ) ,
3971 EOL : this . _model . getEOL ( ) ,
4072 languageId : this . _model . getLanguageIdentifier ( ) . id ,
4173 } ) ;
74+ }
4275
43- this . _register ( this . _model . onDidChangeContent ( ( e ) => {
44- this . _worker . acceptModelChanged ( this . _model . uri . toString ( ) , e ) ;
45- } ) ) ;
46-
47- this . _register ( this . _model . onDidChangeLanguage ( ( e ) => {
48- this . _worker . acceptModelLanguageChanged ( this . _model . uri . toString ( ) , this . _model . getLanguageIdentifier ( ) . id ) ;
49- } ) ) ;
76+ private _endSync ( ) : void {
77+ this . _worker . acceptRemovedModel ( this . _model . uri . toString ( ) ) ;
5078 }
5179
5280 public dispose ( ) {
5381 super . dispose ( ) ;
54- this . _worker . acceptRemovedModel ( this . _model . uri . toString ( ) ) ;
82+ this . _endSync ( ) ;
5583 }
5684}
5785
0 commit comments