1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ITextMateService } from 'vs/workbench/services/textMate/browser/textMate';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { AbstractTextMateService } from 'vs/workbench/services/textMate/browser/abstractTextMateService';
import { ILanguageService } from 'vs/editor/common/languages/language';
import { IWorkbenchThemeService } from 'vs/workbench/services/themes/common/workbenchThemeService';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { ILogService } from 'vs/platform/log/common/log';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { createWebWorker, MonacoWebWorker } from 'vs/editor/browser/services/webWorker';
import { IModelService } from 'vs/editor/common/services/model';
import type { IRawTheme } from 'vscode-textmate';
import { IValidGrammarDefinition } from 'vs/workbench/services/textMate/common/TMScopeRegistry';
import { TextMateWorker } from 'vs/workbench/services/textMate/browser/textMateWorker';
import { ITextModel } from 'vs/editor/common/model';
import { Disposable } from 'vs/base/common/lifecycle';
import { UriComponents, URI } from 'vs/base/common/uri';
import { ContiguousMultilineTokensBuilder } from 'vs/editor/common/tokens/contiguousMultilineTokensBuilder';
import { TMGrammarFactory } from 'vs/workbench/services/textMate/common/TMGrammarFactory';
import { IModelContentChangedEvent } from 'vs/editor/common/textModelEvents';
import { IExtensionResourceLoaderService } from 'vs/workbench/services/extensionResourceLoader/common/extensionResourceLoader';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IProgressService } from 'vs/platform/progress/common/progress';
import { FileAccess } from 'vs/base/common/network';
import { ILanguageIdCodec } from 'vs/editor/common/languages';
import { ILanguageConfigurationService } from 'vs/editor/common/languages/languageConfigurationRegistry';
const RUN_TEXTMATE_IN_WORKER = false;
class ModelWorkerTextMateTokenizer extends Disposable {
private readonly _worker: TextMateWorker;
private readonly _languageIdCodec: ILanguageIdCodec;
private readonly _model: ITextModel;
private _isSynced: boolean;
private _pendingChanges: IModelContentChangedEvent[] = [];
constructor(worker: TextMateWorker, languageIdCodec: ILanguageIdCodec, model: ITextModel) {
super();
this._worker = worker;
this._languageIdCodec = languageIdCodec;
this._model = model;
this._isSynced = false;
this._register(this._model.onDidChangeAttached(() => this._onDidChangeAttached()));
this._onDidChangeAttached();
this._register(this._model.onDidChangeContent((e) => {
if (this._isSynced) {
this._worker.acceptModelChanged(this._model.uri.toString(), e);
this._pendingChanges.push(e);
}
}));
this._register(this._model.onDidChangeLanguage((e) => {
if (this._isSynced) {
const languageId = this._model.getLanguageId();
const encodedLanguageId = this._languageIdCodec.encodeLanguageId(languageId);
this._worker.acceptModelLanguageChanged(this._model.uri.toString(), languageId, encodedLanguageId);
}
}));
}
private _onDidChangeAttached(): void {
if (this._model.isAttachedToEditor()) {
if (!this._isSynced) {
this._beginSync();
}
} else {
if (this._isSynced) {
this._endSync();
}
}
}
private _beginSync(): void {
this._isSynced = true;
const languageId = this._model.getLanguageId();
const encodedLanguageId = this._languageIdCodec.encodeLanguageId(languageId);
this._worker.acceptNewModel({
uri: this._model.uri,
versionId: this._model.getVersionId(),
lines: this._model.getLinesContent(),
EOL: this._model.getEOL(),
languageId,
encodedLanguageId
});
}
private _endSync(): void {
this._isSynced = false;
this._worker.acceptRemovedModel(this._model.uri.toString());
}
public override dispose() {
super.dispose();
this._endSync();
}
private _confirm(versionId: number): void {
while (this._pendingChanges.length > 0 && this._pendingChanges[0].versionId <= versionId) {
this._pendingChanges.shift();
}
}
public setTokens(versionId: number, rawTokens: ArrayBuffer): void {
this._confirm(versionId);
const tokens = ContiguousMultilineTokensBuilder.deserialize(new Uint8Array(rawTokens));
for (let i = 0; i < this._pendingChanges.length; i++) {
const change = this._pendingChanges[i];
for (let j = 0; j < tokens.length; j++) {
for (let k = 0; k < change.changes.length; k++) {
tokens[j].applyEdit(change.changes[k].range, change.changes[k].text);
}
}
}
this._model.tokenization.setTokens(tokens);
}
}
export class TextMateWorkerHost {
constructor(
private readonly textMateService: TextMateService,
@IExtensionResourceLoaderService private readonly _extensionResourceLoaderService: IExtensionResourceLoaderService
) {
}
async readFile(_resource: UriComponents): Promise<string> {
const resource = URI.revive(_resource);
return this._extensionResourceLoaderService.readExtensionResource(resource);
}
async setTokens(_resource: UriComponents, versionId: number, tokens: Uint8Array): Promise<void> {
const resource = URI.revive(_resource);
this.textMateService.setTokens(resource, versionId, tokens);
}
}
export class TextMateService extends AbstractTextMateService {
private _worker: MonacoWebWorker<TextMateWorker> | null;
private _workerProxy: TextMateWorker | null;
private _tokenizers: { [uri: string]: ModelWorkerTextMateTokenizer };
constructor(
@ILanguageService languageService: ILanguageService,
@IWorkbenchThemeService themeService: IWorkbenchThemeService,
@IExtensionResourceLoaderService extensionResourceLoaderService: IExtensionResourceLoaderService,
@INotificationService notificationService: INotificationService,
@ILogService logService: ILogService,
@IConfigurationService configurationService: IConfigurationService,
@IProgressService progressService: IProgressService,
@IModelService private readonly _modelService: IModelService,
@IWorkbenchEnvironmentService private readonly _environmentService: IWorkbenchEnvironmentService,
@ILanguageConfigurationService private readonly _languageConfigurationService: ILanguageConfigurationService,
) {
super(languageService, themeService, extensionResourceLoaderService, notificationService, logService, configurationService, progressService);
this._worker = null;
this._workerProxy = null;
this._tokenizers = Object.create(null);
this._register(this._modelService.onModelAdded(model => this._onModelAdded(model)));
this._register(this._modelService.onModelRemoved(model => this._onModelRemoved(model)));
this._modelService.getModels().forEach((model) => this._onModelAdded(model));
}
private _onModelAdded(model: ITextModel): void {
if (!this._workerProxy) {
return;
}
if (model.isTooLargeForSyncing()) {
return;
}
const key = model.uri.toString();
const tokenizer = new ModelWorkerTextMateTokenizer(this._workerProxy, this._languageService.languageIdCodec, model);
this._tokenizers[key] = tokenizer;
}
private _onModelRemoved(model: ITextModel): void {
const key = model.uri.toString();
if (this._tokenizers[key]) {
this._tokenizers[key].dispose();
delete this._tokenizers[key];
}
}
protected async _loadVSCodeOnigurumWASM(): Promise<Response | ArrayBuffer> {
const response = await fetch(this._environmentService.isBuilt
? FileAccess.asBrowserUri('../../../../../../node_modules.asar.unpacked/vscode-oniguruma/release/onig.wasm', require).toString(true)
: FileAccess.asBrowserUri('../../../../../../node_modules/vscode-oniguruma/release/onig.wasm', require).toString(true));
return response;
}
protected override _onDidCreateGrammarFactory(grammarDefinitions: IValidGrammarDefinition[]): void {
this._killWorker();
if (RUN_TEXTMATE_IN_WORKER) {
const workerHost = new TextMateWorkerHost(this, this._extensionResourceLoaderService);
const worker = createWebWorker<TextMateWorker>(this._modelService, this._languageConfigurationService, {
createData: {
grammarDefinitions
},
label: 'textMateWorker',
moduleId: 'vs/workbench/services/textMate/browser/textMateWorker',
host: workerHost
});
this._worker = worker;
worker.getProxy().then((proxy) => {
if (this._worker !== worker) {
// disposed in the meantime
return;
}
this._workerProxy = proxy;
if (this._currentTheme && this._currentTokenColorMap) {
this._workerProxy.acceptTheme(this._currentTheme, this._currentTokenColorMap);
}
this._modelService.getModels().forEach((model) => this._onModelAdded(model));
});
}
}
protected override _doUpdateTheme(grammarFactory: TMGrammarFactory | null, theme: IRawTheme, colorMap: string[]): void {
super._doUpdateTheme(grammarFactory, theme, colorMap);
if (this._currentTheme && this._currentTokenColorMap && this._workerProxy) {
this._workerProxy.acceptTheme(this._currentTheme, this._currentTokenColorMap);
}
}
protected override _onDidDisposeGrammarFactory(): void {
this._killWorker();
}
private _killWorker(): void {
for (let key of Object.keys(this._tokenizers)) {
this._tokenizers[key].dispose();
}
this._tokenizers = Object.create(null);
if (this._worker) {
this._worker.dispose();
this._worker = null;
}
this._workerProxy = null;
}
setTokens(resource: URI, versionId: number, tokens: ArrayBuffer): void {
const key = resource.toString();
if (!this._tokenizers[key]) {
return;
}
this._tokenizers[key].setTokens(versionId, tokens);
}
}
registerSingleton(ITextMateService, TextMateService);
|