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
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { URI, UriComponents } from 'vs/base/common/uri';
import { ILanguageService } from 'vs/editor/common/languages/language';
import { IModelService } from 'vs/editor/common/services/model';
import { MainThreadLanguagesShape, MainContext, ExtHostContext, ExtHostLanguagesShape } from '../common/extHost.protocol';
import { extHostNamedCustomer, IExtHostContext } from 'vs/workbench/services/extensions/common/extHostCustomers';
import { IPosition } from 'vs/editor/common/core/position';
import { IRange, Range } from 'vs/editor/common/core/range';
import { StandardTokenType } from 'vs/editor/common/languages';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { ILanguageStatus, ILanguageStatusService } from 'vs/workbench/services/languageStatus/common/languageStatusService';
import { DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
@extHostNamedCustomer(MainContext.MainThreadLanguages)
export class MainThreadLanguages implements MainThreadLanguagesShape {
private readonly _disposables = new DisposableStore();
private readonly _proxy: ExtHostLanguagesShape;
private readonly _status = new Map<number, IDisposable>();
constructor(
_extHostContext: IExtHostContext,
@ILanguageService private readonly _languageService: ILanguageService,
@IModelService private readonly _modelService: IModelService,
@ITextModelService private _resolverService: ITextModelService,
@ILanguageStatusService private readonly _languageStatusService: ILanguageStatusService,
) {
this._proxy = _extHostContext.getProxy(ExtHostContext.ExtHostLanguages);
this._proxy.$acceptLanguageIds(_languageService.getRegisteredLanguageIds());
this._disposables.add(_languageService.onDidChange(_ => {
this._proxy.$acceptLanguageIds(_languageService.getRegisteredLanguageIds());
}));
}
dispose(): void {
this._disposables.dispose();
for (const status of this._status.values()) {
status.dispose();
}
this._status.clear();
}
async $changeLanguage(resource: UriComponents, languageId: string): Promise<void> {
if (!this._languageService.isRegisteredLanguageId(languageId)) {
return Promise.reject(new Error(`Unknown language id: ${languageId}`));
}
const uri = URI.revive(resource);
const ref = await this._resolverService.createModelReference(uri);
try {
this._modelService.setMode(ref.object.textEditorModel, this._languageService.createById(languageId));
} finally {
ref.dispose();
}
}
async $tokensAtPosition(resource: UriComponents, position: IPosition): Promise<undefined | { type: StandardTokenType; range: IRange }> {
const uri = URI.revive(resource);
const model = this._modelService.getModel(uri);
if (!model) {
return undefined;
}
model.tokenizeIfCheap(position.lineNumber);
const tokens = model.getLineTokens(position.lineNumber);
const idx = tokens.findTokenIndexAtOffset(position.column - 1);
return {
type: tokens.getStandardTokenType(idx),
range: new Range(position.lineNumber, 1 + tokens.getStartOffset(idx), position.lineNumber, 1 + tokens.getEndOffset(idx))
};
}
// --- language status
$setLanguageStatus(handle: number, status: ILanguageStatus): void {
this._status.get(handle)?.dispose();
this._status.set(handle, this._languageStatusService.addStatus(status));
}
$removeLanguageStatus(handle: number): void {
this._status.get(handle)?.dispose();
}
}
|