@@ -16,12 +16,14 @@ import { IModelContentChange, IModelContentChangedEvent } from 'vs/editor/common
16
16
import { ContiguousMultilineTokensBuilder } from 'vs/editor/common/tokens/contiguousMultilineTokensBuilder' ;
17
17
import { IConfigurationService } from 'vs/platform/configuration/common/configuration' ;
18
18
import { ArrayEdit , MonotonousIndexTransformer , SingleArrayEdit } from 'vs/workbench/services/textMate/browser/arrayOperation' ;
19
- import { TextMateTokenizationWorker } from 'vs/workbench/services/textMate/browser/worker/textMate.worker' ;
20
- import type { StateDeltas } from 'vs/workbench/services/textMate/browser/workerHost/textMateWorkerHost' ;
19
+ import type { StateDeltas , TextMateTokenizationWorker } from 'vs/workbench/services/textMate/browser/backgroundTokenization/worker/textMateTokenizationWorker.worker' ;
21
20
import type { applyStateStackDiff , StateStack } from 'vscode-textmate' ;
22
21
23
22
export class TextMateWorkerTokenizerController extends Disposable {
24
- private _pendingChanges : IModelContentChangedEvent [ ] = [ ] ;
23
+ private static _id = 0 ;
24
+
25
+ public readonly controllerId = TextMateWorkerTokenizerController . _id ++ ;
26
+ private readonly _pendingChanges : IModelContentChangedEvent [ ] = [ ] ;
25
27
26
28
/**
27
29
* These states will eventually equal the worker states.
@@ -47,13 +49,13 @@ export class TextMateWorkerTokenizerController extends Disposable {
47
49
this . _register ( keepAlive ( this . _loggingEnabled ) ) ;
48
50
49
51
this . _register ( this . _model . onDidChangeContent ( ( e ) => {
50
- if ( this . shouldLog ) {
52
+ if ( this . _shouldLog ) {
51
53
console . log ( 'model change' , {
52
54
fileName : this . _model . uri . fsPath . split ( '\\' ) . pop ( ) ,
53
55
changes : changesToString ( e . changes ) ,
54
56
} ) ;
55
57
}
56
- this . _worker . acceptModelChanged ( this . _model . uri . toString ( ) , e ) ;
58
+ this . _worker . acceptModelChanged ( this . controllerId , e ) ;
57
59
this . _pendingChanges . push ( e ) ;
58
60
} ) ) ;
59
61
@@ -62,7 +64,7 @@ export class TextMateWorkerTokenizerController extends Disposable {
62
64
const encodedLanguageId =
63
65
this . _languageIdCodec . encodeLanguageId ( languageId ) ;
64
66
this . _worker . acceptModelLanguageChanged (
65
- this . _model . uri . toString ( ) ,
67
+ this . controllerId ,
66
68
languageId ,
67
69
encodedLanguageId
68
70
) ;
@@ -78,27 +80,33 @@ export class TextMateWorkerTokenizerController extends Disposable {
78
80
languageId,
79
81
encodedLanguageId,
80
82
maxTokenizationLineLength : this . _maxTokenizationLineLength . get ( ) ,
83
+ controllerId : this . controllerId ,
81
84
} ) ;
82
85
83
86
this . _register ( autorun ( 'update maxTokenizationLineLength' , reader => {
84
87
const maxTokenizationLineLength = this . _maxTokenizationLineLength . read ( reader ) ;
85
- this . _worker . acceptMaxTokenizationLineLength ( this . _model . uri . toString ( ) , maxTokenizationLineLength ) ;
88
+ this . _worker . acceptMaxTokenizationLineLength ( this . controllerId , maxTokenizationLineLength ) ;
86
89
} ) ) ;
87
90
}
88
91
89
- get shouldLog ( ) {
90
- return this . _loggingEnabled . get ( ) ;
91
- }
92
-
93
92
public override dispose ( ) : void {
94
93
super . dispose ( ) ;
95
- this . _worker . acceptRemovedModel ( this . _model . uri . toString ( ) ) ;
94
+ this . _worker . acceptRemovedModel ( this . controllerId ) ;
95
+ }
96
+
97
+ public requestTokens ( startLineNumber : number , endLineNumberExclusive : number ) : void {
98
+ this . _worker . retokenize ( this . controllerId , startLineNumber , endLineNumberExclusive ) ;
96
99
}
97
100
98
101
/**
99
102
* This method is called from the worker through the worker host.
100
103
*/
101
- public async setTokensAndStates ( versionId : number , rawTokens : ArrayBuffer , stateDeltas : StateDeltas [ ] ) : Promise < void > {
104
+ public async setTokensAndStates ( controllerId : number , versionId : number , rawTokens : ArrayBuffer , stateDeltas : StateDeltas [ ] ) : Promise < void > {
105
+ if ( this . controllerId !== controllerId ) {
106
+ // This event is for an outdated controller (the worker didn't receive the delete/create messages yet), ignore the event.
107
+ return ;
108
+ }
109
+
102
110
// _states state, change{k}, ..., change{versionId}, state delta base & rawTokens, change{j}, ..., change{m}, current renderer state
103
111
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^
104
112
// | past changes | future states
@@ -107,15 +115,15 @@ export class TextMateWorkerTokenizerController extends Disposable {
107
115
new Uint8Array ( rawTokens )
108
116
) ;
109
117
110
- if ( this . shouldLog ) {
118
+ if ( this . _shouldLog ) {
111
119
console . log ( 'received background tokenization result' , {
112
120
fileName : this . _model . uri . fsPath . split ( '\\' ) . pop ( ) ,
113
121
updatedTokenLines : tokens . map ( ( t ) => t . getLineRange ( ) ) . join ( ' & ' ) ,
114
122
updatedStateLines : stateDeltas . map ( ( s ) => new LineRange ( s . startLineNumber , s . startLineNumber + s . stateDeltas . length ) . toString ( ) ) . join ( ' & ' ) ,
115
123
} ) ;
116
124
}
117
125
118
- if ( this . shouldLog ) {
126
+ if ( this . _shouldLog ) {
119
127
const changes = this . _pendingChanges . filter ( c => c . versionId <= versionId ) . map ( c => c . changes ) . map ( c => changesToString ( c ) ) . join ( ' then ' ) ;
120
128
console . log ( 'Applying changes to local states' , changes ) ;
121
129
}
@@ -130,7 +138,7 @@ export class TextMateWorkerTokenizerController extends Disposable {
130
138
}
131
139
132
140
if ( this . _pendingChanges . length > 0 ) {
133
- if ( this . shouldLog ) {
141
+ if ( this . _shouldLog ) {
134
142
const changes = this . _pendingChanges . map ( c => c . changes ) . map ( c => changesToString ( c ) ) . join ( ' then ' ) ;
135
143
console . log ( 'Considering non-processed changes' , changes ) ;
136
144
}
@@ -205,6 +213,9 @@ export class TextMateWorkerTokenizerController extends Disposable {
205
213
// First set states, then tokens, so that events fired from set tokens don't read invalid states
206
214
this . _backgroundTokenizationStore . setTokens ( tokens ) ;
207
215
}
216
+
217
+ private get _shouldLog ( ) { return this . _loggingEnabled . get ( ) ; }
218
+
208
219
}
209
220
210
221
function fullLineArrayEditFromModelContentChange ( c : IModelContentChange [ ] ) : ArrayEdit {
0 commit comments