nullMode.ts 2.1 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 {IState, ILineTokens} from 'vs/editor/common/modes';
8
import {ModeTransition} from 'vs/editor/common/core/modeTransition';
A
Alex Dima 已提交
9
import {Token} from 'vs/editor/common/core/token';
10
import {ITokenizationResult} from 'vs/editor/common/modes/abstractState';
A
Alex Dima 已提交
11
import {LineStream} from 'vs/editor/common/modes/lineStream';
E
Erich Gamma 已提交
12

13
export class NullState implements IState {
E
Erich Gamma 已提交
14

15
	private modeId: string;
16
	private stateData: IState;
E
Erich Gamma 已提交
17

18 19
	constructor(modeId: string, stateData: IState) {
		this.modeId = modeId;
E
Erich Gamma 已提交
20 21 22
		this.stateData = stateData;
	}

23 24
	public clone(): IState {
		let stateDataClone:IState = (this.stateData ? this.stateData.clone() : null);
25
		return new NullState(this.modeId, stateDataClone);
E
Erich Gamma 已提交
26 27
	}

28
	public equals(other:IState): boolean {
29
		if (this.modeId !== other.getModeId()) {
E
Erich Gamma 已提交
30 31
			return false;
		}
32
		let otherStateData = other.getStateData();
E
Erich Gamma 已提交
33 34 35 36 37 38 39 40 41
		if (!this.stateData && !otherStateData) {
			return true;
		}
		if (this.stateData && otherStateData) {
			return this.stateData.equals(otherStateData);
		}
		return false;
	}

42 43
	public getModeId(): string {
		return this.modeId;
E
Erich Gamma 已提交
44 45
	}

A
Alex Dima 已提交
46
	public tokenize(stream:LineStream):ITokenizationResult {
E
Erich Gamma 已提交
47 48 49 50
		stream.advanceToEOS();
		return { type:'' };
	}

51
	public getStateData(): IState {
E
Erich Gamma 已提交
52 53 54
		return this.stateData;
	}

55
	public setStateData(stateData:IState):void {
E
Erich Gamma 已提交
56 57 58 59
		this.stateData = stateData;
	}
}

60
export const NULL_MODE_ID = 'vs.editor.nullMode';
E
Erich Gamma 已提交
61

62
export function nullTokenize(modeId: string, buffer:string, state: IState, deltaOffset:number = 0, stopAtOffset?:number): ILineTokens {
A
Alex Dima 已提交
63
	let tokens:Token[] = [new Token(deltaOffset, '')];
E
Erich Gamma 已提交
64

65
	let modeTransitions:ModeTransition[] = [new ModeTransition(deltaOffset, modeId)];
E
Erich Gamma 已提交
66 67 68 69 70 71 72 73

	return {
		tokens: tokens,
		actualStopOffset: deltaOffset + buffer.length,
		endState: state,
		modeTransitions: modeTransitions
	};
}