testModes.ts 4.6 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';
E
Erich Gamma 已提交
8
import {AbstractState} from 'vs/editor/common/modes/abstractState';
9
import {RichEditSupport} from 'vs/editor/common/modes/supports/richEditSupport';
10
import {TokenizationSupport} from 'vs/editor/common/modes/supports/tokenizationSupport';
A
Alex Dima 已提交
11
import {MockMode} from 'vs/editor/test/common/mocks/mockMode';
E
Erich Gamma 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

export class CommentState extends AbstractState {

	constructor(mode:modes.IMode, stateCount:number) {
		super(mode);
	}

	public makeClone():CommentState {
		return this;
	}

	public equals(other:modes.IState):boolean {
		return true;
	}

	public tokenize(stream:modes.IStream):modes.ITokenizationResult {
		stream.advanceToEOS();
		return { type: 'state' };
	}
}

A
Alex Dima 已提交
33
export class CommentMode extends MockMode {
E
Erich Gamma 已提交
34 35

	public tokenizationSupport: modes.ITokenizationSupport;
36
	public richEditSupport: modes.IRichEditSupport;
E
Erich Gamma 已提交
37 38

	constructor(commentsConfig:modes.ICommentsConfiguration) {
A
Alex Dima 已提交
39
		super();
40
		this.tokenizationSupport = new TokenizationSupport(this, {
E
Erich Gamma 已提交
41 42 43
			getInitialState: () => new CommentState(this, 0)
		}, false, false);

44
		this.richEditSupport = {
A
Alex Dima 已提交
45
			comments:commentsConfig
46
		};
E
Erich Gamma 已提交
47 48 49
	}
}

A
Alex Dima 已提交
50
export abstract class AbstractIndentingMode extends MockMode {
E
Erich Gamma 已提交
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

	public getElectricCharacters():string[] {
		return null;
	}

	public onElectricCharacter(context:modes.ILineContext, offset:number):modes.IElectricAction {
		return null;
	}

	public onEnter(context:modes.ILineContext, offset:number):modes.IEnterAction {
		return null;
	}

}

export class ModelState1 extends AbstractState {

	constructor(mode:modes.IMode) {
		super(mode);
	}

	public makeClone():ModelState1 {
		return this;
	}

	public equals(other: modes.IState):boolean {
		return this === other;
	}

	public tokenize(stream:modes.IStream):modes.ITokenizationResult {
		(<ModelMode1>this.getMode()).calledFor.push(stream.next());
		stream.advanceToEOS();
		return { type: '' };
	}
}

A
Alex Dima 已提交
87
export class ModelMode1 extends MockMode {
E
Erich Gamma 已提交
88 89 90 91 92 93 94
	public calledFor:string[];

	public tokenizationSupport: modes.ITokenizationSupport;

	constructor() {
		super();
		this.calledFor = [];
95
		this.tokenizationSupport = new TokenizationSupport(this, {
E
Erich Gamma 已提交
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
			getInitialState: () => new ModelState1(this)
		}, false, false);
	}
}

export class ModelState2 extends AbstractState {

	private prevLineContent:string;

	constructor(mode:ModelMode2, prevLineContent:string) {
		super(mode);
		this.prevLineContent = prevLineContent;
	}

	public makeClone():ModelState2 {
		return new ModelState2(<ModelMode2>this.getMode(), this.prevLineContent);
	}

	public equals(other: modes.IState):boolean {
		return (other instanceof ModelState2) && (this.prevLineContent === (<ModelState2>other).prevLineContent);
	}

	public tokenize(stream:modes.IStream):modes.ITokenizationResult {
		var line= '';
		while (!stream.eos()) {
			line+= stream.next();
		}
		this.prevLineContent= line;
		return { type: '' };
	}
}

A
Alex Dima 已提交
128
export class ModelMode2 extends MockMode {
E
Erich Gamma 已提交
129 130 131 132 133 134 135
	public calledFor:any[];

	public tokenizationSupport: modes.ITokenizationSupport;

	constructor() {
		super();
		this.calledFor = null;
136
		this.tokenizationSupport = new TokenizationSupport(this, {
E
Erich Gamma 已提交
137 138 139 140 141
			getInitialState: () => new ModelState2(this, '')
		}, false, false);
	}
}

A
Alex Dima 已提交
142
export class BracketMode extends MockMode {
E
Erich Gamma 已提交
143

144
	public richEditSupport: modes.IRichEditSupport;
E
Erich Gamma 已提交
145 146 147

	constructor() {
		super();
148
		this.richEditSupport = new RichEditSupport(this.getId(), null, {
A
Alex Dima 已提交
149 150 151 152 153
			brackets: [
				['{', '}'],
				['[', ']'],
				['(', ')'],
			]
154
		});
E
Erich Gamma 已提交
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
	}
}

export class NState extends AbstractState {

	private n:number;
	private allResults:modes.ITokenizationResult[];

	constructor(mode:modes.IMode, n:number) {
		super(mode);
		this.n = n;
		this.allResults = null;
	}


	public makeClone():NState {
		return this;
	}

	public equals(other: modes.IState):boolean {
		return true;
	}

	public tokenize(stream:modes.IStream):modes.ITokenizationResult {
		var ndash = this.n, value = '';
		while(!stream.eos() && ndash > 0) {
			value += stream.next();
			ndash--;
		}
		return { type: 'n-' + (this.n - ndash) + '-' + value };
	}
}

A
Alex Dima 已提交
188
export class NMode extends MockMode {
E
Erich Gamma 已提交
189 190 191 192 193 194 195

	private n:number;

	public tokenizationSupport: modes.ITokenizationSupport;

	constructor(n:number) {
		super();
196
		this.n = n;
197
		this.tokenizationSupport = new TokenizationSupport(this, {
E
Erich Gamma 已提交
198 199 200 201
			getInitialState: () => new NState(this, this.n)
		}, false, false);
	}
}