Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/common/languages/supports.ts
3294 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IViewLineTokens, LineTokens } from '../tokens/lineTokens.js';
7
import { StandardTokenType } from '../encodedTokenAttributes.js';
8
import { ILanguageIdCodec } from '../languages.js';
9
10
export function createScopedLineTokens(context: LineTokens, offset: number): ScopedLineTokens {
11
const tokenCount = context.getCount();
12
const tokenIndex = context.findTokenIndexAtOffset(offset);
13
const desiredLanguageId = context.getLanguageId(tokenIndex);
14
15
let lastTokenIndex = tokenIndex;
16
while (lastTokenIndex + 1 < tokenCount && context.getLanguageId(lastTokenIndex + 1) === desiredLanguageId) {
17
lastTokenIndex++;
18
}
19
20
let firstTokenIndex = tokenIndex;
21
while (firstTokenIndex > 0 && context.getLanguageId(firstTokenIndex - 1) === desiredLanguageId) {
22
firstTokenIndex--;
23
}
24
25
return new ScopedLineTokens(
26
context,
27
desiredLanguageId,
28
firstTokenIndex,
29
lastTokenIndex + 1,
30
context.getStartOffset(firstTokenIndex),
31
context.getEndOffset(lastTokenIndex)
32
);
33
}
34
35
export class ScopedLineTokens {
36
_scopedLineTokensBrand: void = undefined;
37
38
public readonly languageIdCodec: ILanguageIdCodec;
39
public readonly languageId: string;
40
private readonly _actual: LineTokens;
41
private readonly _firstTokenIndex: number;
42
private readonly _lastTokenIndex: number;
43
public readonly firstCharOffset: number;
44
private readonly _lastCharOffset: number;
45
46
constructor(
47
actual: LineTokens,
48
languageId: string,
49
firstTokenIndex: number,
50
lastTokenIndex: number,
51
firstCharOffset: number,
52
lastCharOffset: number
53
) {
54
this._actual = actual;
55
this.languageId = languageId;
56
this._firstTokenIndex = firstTokenIndex;
57
this._lastTokenIndex = lastTokenIndex;
58
this.firstCharOffset = firstCharOffset;
59
this._lastCharOffset = lastCharOffset;
60
this.languageIdCodec = actual.languageIdCodec;
61
}
62
63
public getLineContent(): string {
64
const actualLineContent = this._actual.getLineContent();
65
return actualLineContent.substring(this.firstCharOffset, this._lastCharOffset);
66
}
67
68
public getLineLength(): number {
69
return this._lastCharOffset - this.firstCharOffset;
70
}
71
72
public getActualLineContentBefore(offset: number): string {
73
const actualLineContent = this._actual.getLineContent();
74
return actualLineContent.substring(0, this.firstCharOffset + offset);
75
}
76
77
public getTokenCount(): number {
78
return this._lastTokenIndex - this._firstTokenIndex;
79
}
80
81
public findTokenIndexAtOffset(offset: number): number {
82
return this._actual.findTokenIndexAtOffset(offset + this.firstCharOffset) - this._firstTokenIndex;
83
}
84
85
public getStandardTokenType(tokenIndex: number): StandardTokenType {
86
return this._actual.getStandardTokenType(tokenIndex + this._firstTokenIndex);
87
}
88
89
public toIViewLineTokens(): IViewLineTokens {
90
return this._actual.sliceAndInflate(this.firstCharOffset, this._lastCharOffset, 0);
91
}
92
}
93
94
const enum IgnoreBracketsInTokens {
95
value = StandardTokenType.Comment | StandardTokenType.String | StandardTokenType.RegEx
96
}
97
98
export function ignoreBracketsInToken(standardTokenType: StandardTokenType): boolean {
99
return (standardTokenType & IgnoreBracketsInTokens.value) !== 0;
100
}
101
102