supports.ts 3.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

A
Alex Dima 已提交
7
import * as modes from 'vs/editor/common/modes';
A
Alex Dima 已提交
8
import { LineTokens } from 'vs/editor/common/core/lineTokens';
E
Erich Gamma 已提交
9

A
Alex Dima 已提交
10
export function createScopedLineTokens(context: LineTokens, offset: number): ScopedLineTokens {
A
Alex Dima 已提交
11 12 13 14 15 16 17
	let tokenCount = context.getTokenCount();
	let tokenIndex = context.findTokenIndexAtOffset(offset);
	let desiredLanguageId = context.getLanguageId(tokenIndex);

	let lastTokenIndex = tokenIndex;
	while (lastTokenIndex + 1 < tokenCount && context.getLanguageId(lastTokenIndex + 1) === desiredLanguageId) {
		lastTokenIndex++;
E
Erich Gamma 已提交
18 19
	}

A
Alex Dima 已提交
20 21 22
	let firstTokenIndex = tokenIndex;
	while (firstTokenIndex > 0 && context.getLanguageId(firstTokenIndex - 1) === desiredLanguageId) {
		firstTokenIndex--;
E
Erich Gamma 已提交
23 24
	}

A
Alex Dima 已提交
25 26 27 28 29 30 31 32
	return new ScopedLineTokens(
		context,
		desiredLanguageId,
		firstTokenIndex,
		lastTokenIndex + 1,
		context.getTokenStartOffset(firstTokenIndex),
		context.getTokenEndOffset(lastTokenIndex)
	);
E
Erich Gamma 已提交
33 34
}

A
Alex Dima 已提交
35 36 37
export class ScopedLineTokens {
	_scopedLineTokensBrand: void;

A
Alex Dima 已提交
38
	public readonly languageId: modes.LanguageId;
A
Alex Dima 已提交
39 40 41 42 43 44 45 46
	private readonly _actual: LineTokens;
	private readonly _firstTokenIndex: number;
	private readonly _lastTokenIndex: number;
	public readonly firstCharOffset: number;
	private readonly _lastCharOffset: number;

	constructor(
		actual: LineTokens,
A
Alex Dima 已提交
47
		languageId: modes.LanguageId,
A
Alex Dima 已提交
48 49 50 51 52
		firstTokenIndex: number,
		lastTokenIndex: number,
		firstCharOffset: number,
		lastCharOffset: number
	) {
E
Erich Gamma 已提交
53
		this._actual = actual;
A
Alex Dima 已提交
54
		this.languageId = languageId;
A
Alex Dima 已提交
55 56 57 58
		this._firstTokenIndex = firstTokenIndex;
		this._lastTokenIndex = lastTokenIndex;
		this.firstCharOffset = firstCharOffset;
		this._lastCharOffset = lastCharOffset;
E
Erich Gamma 已提交
59 60 61 62
	}

	public getLineContent(): string {
		var actualLineContent = this._actual.getLineContent();
A
Alex Dima 已提交
63
		return actualLineContent.substring(this.firstCharOffset, this._lastCharOffset);
E
Erich Gamma 已提交
64 65 66
	}

	public getTokenCount(): number {
A
Alex Dima 已提交
67
		return this._lastTokenIndex - this._firstTokenIndex;
E
Erich Gamma 已提交
68 69
	}

A
Alex Dima 已提交
70 71
	public findTokenIndexAtOffset(offset: number): number {
		return this._actual.findTokenIndexAtOffset(offset + this.firstCharOffset) - this._firstTokenIndex;
E
Erich Gamma 已提交
72 73
	}

J
Johannes Rieken 已提交
74
	public getTokenStartOffset(tokenIndex: number): number {
A
Alex Dima 已提交
75
		return this._actual.getTokenStartOffset(tokenIndex + this._firstTokenIndex) - this.firstCharOffset;
E
Erich Gamma 已提交
76 77
	}

A
Alex Dima 已提交
78
	public getStandardTokenType(tokenIndex: number): modes.StandardTokenType {
79 80
		return this._actual.getStandardTokenType(tokenIndex + this._firstTokenIndex);
	}
E
Erich Gamma 已提交
81 82
}

A
Alex Dima 已提交
83
const enum IgnoreBracketsInTokens {
A
Alex Dima 已提交
84
	value = modes.StandardTokenType.Comment | modes.StandardTokenType.String | modes.StandardTokenType.RegEx
A
Alex Dima 已提交
85 86
}

A
Alex Dima 已提交
87
export function ignoreBracketsInToken(standardTokenType: modes.StandardTokenType): boolean {
A
Alex Dima 已提交
88
	return (standardTokenType & IgnoreBracketsInTokens.value) !== 0;
89
}