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

import {OrderGuaranteeEventEmitter} from 'vs/base/common/eventEmitter';
A
Alex Dima 已提交
8
import * as strings from 'vs/base/common/strings';
E
Erich Gamma 已提交
9 10
import {Position} from 'vs/editor/common/core/position';
import {Range} from 'vs/editor/common/core/range';
A
Alex Dima 已提交
11
import * as editorCommon from 'vs/editor/common/editorCommon';
E
Erich Gamma 已提交
12
import {ModelLine} from 'vs/editor/common/model/modelLine';
13
import {guessIndentation} from 'vs/editor/common/model/indentationGuesser';
14
import {DEFAULT_INDENTATION, DEFAULT_TRIM_AUTO_WHITESPACE} from 'vs/editor/common/config/defaultConfig';
15
import {PrefixSumComputer} from 'vs/editor/common/viewModel/prefixSumComputer';
A
Alex Dima 已提交
16
import {IndentRange, computeRanges} from 'vs/editor/common/model/indentRanges';
A
Alex Dima 已提交
17
import {CharCode} from 'vs/base/common/charCode';
E
Erich Gamma 已提交
18

A
Alex Dima 已提交
19
const LIMIT_FIND_COUNT = 999;
A
Alex Dima 已提交
20
export const LONG_LINE_BOUNDARY = 1000;
E
Erich Gamma 已提交
21

A
Alex Dima 已提交
22
export class TextModel extends OrderGuaranteeEventEmitter implements editorCommon.ITextModel {
23 24
	private static MODEL_SYNC_LIMIT = 5 * 1024 * 1024; // 5 MB
	private static MODEL_TOKENIZATION_LIMIT = 20 * 1024 * 1024; // 20 MB
E
Erich Gamma 已提交
25

26 27 28 29
	public static DEFAULT_CREATION_OPTIONS: editorCommon.ITextModelCreationOptions = {
		tabSize: DEFAULT_INDENTATION.tabSize,
		insertSpaces: DEFAULT_INDENTATION.insertSpaces,
		detectIndentation: false,
A
Alex Dima 已提交
30
		defaultEOL: editorCommon.DefaultEndOfLine.LF,
31
		trimAutoWhitespace: DEFAULT_TRIM_AUTO_WHITESPACE,
32 33
	};

A
Alex Dima 已提交
34
	/*protected*/ _lines:ModelLine[];
35 36 37
	protected _EOL:string;
	protected _isDisposed:boolean;
	protected _isDisposing:boolean;
38
	protected _options: editorCommon.ITextModelResolvedOptions;
39
	protected _lineStarts: PrefixSumComputer;
A
Alex Dima 已提交
40
	private _indentRanges: IndentRange[];
E
Erich Gamma 已提交
41 42 43 44 45 46 47 48

	private _versionId:number;
	/**
	 * Unlike, versionId, this can go down (via undo) or go to previous values (via redo)
	 */
	private _alternativeVersionId: number;
	private _BOM:string;

49 50 51
	private _shouldSimplifyMode: boolean;
	private _shouldDenyMode: boolean;

A
Alex Dima 已提交
52
	constructor(allowedEventTypes:string[], rawText:editorCommon.IRawText) {
A
Alex Dima 已提交
53
		allowedEventTypes.push(editorCommon.EventType.ModelRawContentChanged, editorCommon.EventType.ModelOptionsChanged);
E
Erich Gamma 已提交
54 55
		super(allowedEventTypes);

56 57 58
		this._shouldSimplifyMode = (rawText.length > TextModel.MODEL_SYNC_LIMIT);
		this._shouldDenyMode = (rawText.length > TextModel.MODEL_TOKENIZATION_LIMIT);

59
		this._options = rawText.options;
E
Erich Gamma 已提交
60 61 62 63 64 65
		this._constructLines(rawText);
		this._setVersionId(1);
		this._isDisposed = false;
		this._isDisposing = false;
	}

66 67 68 69 70 71 72 73
	public isTooLargeForHavingAMode(): boolean {
		return this._shouldDenyMode;
	}

	public isTooLargeForHavingARichMode(): boolean {
		return this._shouldSimplifyMode;
	}

74 75 76 77
	public getOptions(): editorCommon.ITextModelResolvedOptions {
		return this._options;
	}

78 79 80 81
	public updateOptions(newOpts:editorCommon.ITextModelUpdateOptions): void {
		let somethingChanged = false;
		let changed:editorCommon.IModelOptionsChangedEvent = {
			tabSize: false,
82 83
			insertSpaces: false,
			trimAutoWhitespace: false
84 85 86 87 88 89 90 91 92 93
		};

		if (typeof newOpts.insertSpaces !== 'undefined') {
			if (this._options.insertSpaces !== newOpts.insertSpaces) {
				somethingChanged = true;
				changed.insertSpaces = true;
				this._options.insertSpaces = newOpts.insertSpaces;
			}
		}
		if (typeof newOpts.tabSize !== 'undefined') {
94 95
			let newTabSize = newOpts.tabSize | 0;
			if (this._options.tabSize !== newTabSize) {
96 97
				somethingChanged = true;
				changed.tabSize = true;
98 99 100 101 102
				this._options.tabSize = newTabSize;

				for (let i = 0, len = this._lines.length; i < len; i++) {
					this._lines[i].updateTabSize(newTabSize);
				}
103 104
			}
		}
105 106 107 108 109 110 111
		if (typeof newOpts.trimAutoWhitespace !== 'undefined') {
			if (this._options.trimAutoWhitespace !== newOpts.trimAutoWhitespace) {
				somethingChanged = true;
				changed.trimAutoWhitespace = true;
				this._options.trimAutoWhitespace = newOpts.trimAutoWhitespace;
			}
		}
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

		if (somethingChanged) {
			this.emit(editorCommon.EventType.ModelOptionsChanged, changed);
		}
	}

	public detectIndentation(defaultInsertSpaces:boolean, defaultTabSize:number): void {
		let lines = this._lines.map(line => line.text);
		let guessedIndentation = guessIndentation(lines, defaultTabSize, defaultInsertSpaces);
		this.updateOptions({
			insertSpaces: guessedIndentation.insertSpaces,
			tabSize: guessedIndentation.tabSize
		});
	}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	private _normalizeIndentationFromWhitespace(str:string): string {
		let tabSize = this._options.tabSize;
		let insertSpaces = this._options.insertSpaces;

		let spacesCnt = 0;
		for (let i = 0; i < str.length; i++) {
			if (str.charAt(i) === '\t') {
				spacesCnt += tabSize;
			} else {
				spacesCnt++;
			}
		}

		let result = '';
		if (!insertSpaces) {
			let tabsCnt = Math.floor(spacesCnt / tabSize);
			spacesCnt = spacesCnt % tabSize;
			for (let i = 0; i < tabsCnt; i++) {
				result += '\t';
			}
		}

		for (let i = 0; i < spacesCnt; i++) {
			result += ' ';
		}

		return result;
	}

	public normalizeIndentation(str:string): string {
		let firstNonWhitespaceIndex = strings.firstNonWhitespaceIndex(str);
		if (firstNonWhitespaceIndex === -1) {
			firstNonWhitespaceIndex = str.length;
		}
		return this._normalizeIndentationFromWhitespace(str.substring(0, firstNonWhitespaceIndex)) + str.substring(firstNonWhitespaceIndex);
	}

	public getOneIndent(): string {
		let tabSize = this._options.tabSize;
		let insertSpaces = this._options.insertSpaces;

		if (insertSpaces) {
			let result = '';
			for (let i = 0; i < tabSize; i++) {
				result += ' ';
			}
			return result;
		} else {
			return '\t';
		}
	}

E
Erich Gamma 已提交
179 180 181 182 183 184 185 186
	public getVersionId(): number {
		return this._versionId;
	}

	public getAlternativeVersionId(): number {
		return this._alternativeVersionId;
	}

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
	private _ensureLineStarts(): void {
		if (!this._lineStarts) {
			const lineStartValues:number[] = [];
			const eolLength = this._EOL.length;
			for (let i = 0, len = this._lines.length; i < len; i++) {
				lineStartValues.push(this._lines[i].text.length + eolLength);
			}
			this._lineStarts = new PrefixSumComputer(lineStartValues);
		}
	}

	public getOffsetAt(rawPosition: editorCommon.IPosition): number {
		let position = this.validatePosition(rawPosition);
		this._ensureLineStarts();
		return this._lineStarts.getAccumulatedValue(position.lineNumber - 2) + position.column - 1;
	}

	public getPositionAt(offset: number): Position {
		offset = Math.floor(offset);
		offset = Math.max(0, offset);

		this._ensureLineStarts();
		let out = this._lineStarts.getIndexOf(offset);

		let lineLength = this._lines[out.index].text.length;

		// Ensure we return a valid position
		return new Position(out.index + 1, Math.min(out.remainder + 1, lineLength + 1));
	}

A
Alex Dima 已提交
217
	protected _increaseVersionId(): void {
E
Erich Gamma 已提交
218 219 220
		this._setVersionId(this._versionId + 1);
	}

A
Alex Dima 已提交
221
	protected _setVersionId(newVersionId:number): void {
E
Erich Gamma 已提交
222 223 224 225
		this._versionId = newVersionId;
		this._alternativeVersionId = this._versionId;
	}

A
Alex Dima 已提交
226
	protected _overwriteAlternativeVersionId(newAlternativeVersionId:number): void {
E
Erich Gamma 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
		this._alternativeVersionId = newAlternativeVersionId;
	}

	public isDisposed(): boolean {
		return this._isDisposed;
	}

	public dispose(): void {
		this._isDisposed = true;
		// Null out members, such that any use of a disposed model will throw exceptions sooner rather than later
		this._lines = null;
		this._EOL = null;
		this._BOM = null;

		super.dispose();
	}

A
Alex Dima 已提交
244
	protected _createContentChangedFlushEvent(): editorCommon.IModelContentChangedFlushEvent {
E
Erich Gamma 已提交
245
		return {
A
Alex Dima 已提交
246
			changeType: editorCommon.EventType.ModelRawContentChangedFlush,
E
Erich Gamma 已提交
247 248 249 250 251 252 253 254 255
			detail: null,
			// TODO@Alex -> remove these fields from here
			versionId: -1,
			isUndoing: false,
			isRedoing: false
		};
	}

	protected _emitContentChanged2(startLineNumber:number, startColumn:number, endLineNumber:number, endColumn:number, rangeLength:number, text:string, isUndoing:boolean, isRedoing:boolean): void {
A
Alex Dima 已提交
256
		var e:editorCommon.IModelContentChangedEvent2 = {
E
Erich Gamma 已提交
257 258 259
			range: new Range(startLineNumber, startColumn, endLineNumber, endColumn),
			rangeLength: rangeLength,
			text: text,
260
			eol: this._EOL,
E
Erich Gamma 已提交
261 262 263 264 265
			versionId: this.getVersionId(),
			isUndoing: isUndoing,
			isRedoing: isRedoing
		};
		if (!this._isDisposing) {
A
Alex Dima 已提交
266
			this.emit(editorCommon.EventType.ModelContentChanged2, e);
E
Erich Gamma 已提交
267 268 269
		}
	}

A
Alex Dima 已提交
270
	protected _resetValue(e:editorCommon.IModelContentChangedFlushEvent, newValue:editorCommon.IRawText): void {
271 272
		this._constructLines(newValue);

E
Erich Gamma 已提交
273 274 275 276 277 278
		this._increaseVersionId();

		e.detail = this.toRawText();
		e.versionId = this._versionId;
	}

A
Alex Dima 已提交
279
	public toRawText(): editorCommon.IRawText {
E
Erich Gamma 已提交
280 281 282 283
		return {
			BOM: this._BOM,
			EOL: this._EOL,
			lines: this.getLinesContent(),
284
			length: this.getValueLength(),
285
			options: this._options
E
Erich Gamma 已提交
286 287 288
		};
	}

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	public equals(other: editorCommon.IRawText): boolean {
		if (this._BOM !== other.BOM) {
			return false;
		}
		if (this._EOL !== other.EOL) {
			return false;
		}
		if (this._lines.length !== other.lines.length) {
			return false;
		}
		for (let i = 0, len = this._lines.length; i < len; i++) {
			if (this._lines[i].text !== other.lines[i]) {
				return false;
			}
		}
		return true;
	}

	public setValue(value:string): void {
A
Alex Dima 已提交
308 309 310
		if (value === null) {
			// There's nothing to do
			return;
311
		}
A
Alex Dima 已提交
312 313 314 315 316 317 318 319
		let rawText: editorCommon.IRawText = null;
		rawText = TextModel.toRawText(value, {
			tabSize: this._options.tabSize,
			insertSpaces: this._options.insertSpaces,
			trimAutoWhitespace: this._options.trimAutoWhitespace,
			detectIndentation: false,
			defaultEOL: this._options.defaultEOL
		});
320 321 322 323
		this.setValueFromRawText(rawText);
	}

	public setValueFromRawText(newValue:editorCommon.IRawText): void {
E
Erich Gamma 已提交
324 325 326 327 328 329 330 331 332
		if (newValue === null) {
			// There's nothing to do
			return;
		}
		var oldFullModelRange = this.getFullModelRange();
		var oldModelValueLength = this.getValueLengthInRange(oldFullModelRange);
		var endLineNumber = this.getLineCount();
		var endColumn = this.getLineMaxColumn(endLineNumber);
		var e = this._createContentChangedFlushEvent();
333

E
Erich Gamma 已提交
334 335 336 337 338
		this._resetValue(e, newValue);
		this._emitModelContentChangedFlushEvent(e);
		this._emitContentChanged2(1, 1, endLineNumber, endColumn, oldModelValueLength, this.getValue(), false, false);
	}

A
Alex Dima 已提交
339
	public getValue(eol?:editorCommon.EndOfLinePreference, preserveBOM:boolean=false): string {
E
Erich Gamma 已提交
340 341 342 343 344 345 346 347 348 349
		var fullModelRange = this.getFullModelRange();
		var fullModelValue = this.getValueInRange(fullModelRange, eol);

		if (preserveBOM) {
			return this._BOM + fullModelValue;
		}

		return fullModelValue;
	}

A
Alex Dima 已提交
350
	public getValueLength(eol?: editorCommon.EndOfLinePreference, preserveBOM: boolean = false): number {
E
Erich Gamma 已提交
351 352 353 354 355 356 357 358 359 360
		var fullModelRange = this.getFullModelRange();
		var fullModelValue = this.getValueLengthInRange(fullModelRange, eol);

		if (preserveBOM) {
			return this._BOM.length + fullModelValue;
		}

		return fullModelValue;
	}

A
Alex Dima 已提交
361
	public getEmptiedValueInRange(rawRange:editorCommon.IRange, fillCharacter: string = '', eol:editorCommon.EndOfLinePreference=editorCommon.EndOfLinePreference.TextDefined): string {
E
Erich Gamma 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
		var range = this.validateRange(rawRange);

		if (range.isEmpty()) {
			return '';
		}

		if (range.startLineNumber === range.endLineNumber) {
			return this._repeatCharacter(fillCharacter, range.endColumn - range.startColumn);
		}

		var lineEnding = this._getEndOfLine(eol),
			startLineIndex = range.startLineNumber - 1,
			endLineIndex = range.endLineNumber - 1,
			resultLines:string[] = [];

		resultLines.push(this._repeatCharacter(fillCharacter, this._lines[startLineIndex].text.length - range.startColumn + 1));
		for (var i = startLineIndex + 1; i < endLineIndex; i++) {
			resultLines.push(this._repeatCharacter(fillCharacter, this._lines[i].text.length));
		}
		resultLines.push(this._repeatCharacter(fillCharacter, range.endColumn - 1));

		return resultLines.join(lineEnding);
	}

	private _repeatCharacter(fillCharacter:string, count:number): string {
		var r = '';
		for (var i = 0; i < count; i++) {
			r += fillCharacter;
		}
		return r;
	}

A
Alex Dima 已提交
394
	public getValueInRange(rawRange:editorCommon.IRange, eol:editorCommon.EndOfLinePreference=editorCommon.EndOfLinePreference.TextDefined): string {
E
Erich Gamma 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
		var range = this.validateRange(rawRange);

		if (range.isEmpty()) {
			return '';
		}

		if (range.startLineNumber === range.endLineNumber) {
			return this._lines[range.startLineNumber - 1].text.substring(range.startColumn - 1, range.endColumn - 1);
		}

		var lineEnding = this._getEndOfLine(eol),
			startLineIndex = range.startLineNumber - 1,
			endLineIndex = range.endLineNumber - 1,
			resultLines:string[] = [];

		resultLines.push(this._lines[startLineIndex].text.substring(range.startColumn - 1));
		for (var i = startLineIndex + 1; i < endLineIndex; i++) {
			resultLines.push(this._lines[i].text);
		}
		resultLines.push(this._lines[endLineIndex].text.substring(0, range.endColumn - 1));

		return resultLines.join(lineEnding);
	}

A
Alex Dima 已提交
419
	public getValueLengthInRange(rawRange:editorCommon.IRange, eol:editorCommon.EndOfLinePreference=editorCommon.EndOfLinePreference.TextDefined): number {
E
Erich Gamma 已提交
420 421 422 423 424 425 426 427 428 429
		var range = this.validateRange(rawRange);

		if (range.isEmpty()) {
			return 0;
		}

		if (range.startLineNumber === range.endLineNumber) {
			return (range.endColumn - range.startColumn);
		}

430 431 432
		let startOffset = this.getOffsetAt(new Position(range.startLineNumber, range.startColumn));
		let endOffset = this.getOffsetAt(new Position(range.endLineNumber, range.endColumn));
		return endOffset - startOffset;
E
Erich Gamma 已提交
433 434
	}

A
Alex Dima 已提交
435
	public isDominatedByLongLines(): boolean {
E
Erich Gamma 已提交
436 437 438 439 440 441 442 443 444
		var smallLineCharCount = 0,
			longLineCharCount = 0,
			i: number,
			len: number,
			lines = this._lines,
			lineLength: number;

		for (i = 0, len = this._lines.length; i < len; i++) {
			lineLength = lines[i].text.length;
A
Alex Dima 已提交
445
			if (lineLength >= LONG_LINE_BOUNDARY) {
E
Erich Gamma 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
				longLineCharCount += lineLength;
			} else {
				smallLineCharCount += lineLength;
			}
		}

		return (longLineCharCount > smallLineCharCount);
	}

	public getLineCount(): number {
		return this._lines.length;
	}

	public getLineContent(lineNumber:number): string {
		if (lineNumber < 1 || lineNumber > this.getLineCount()) {
			throw new Error('Illegal value ' + lineNumber + ' for `lineNumber`');
		}

		return this._lines[lineNumber - 1].text;
	}

467 468 469 470 471 472 473 474
	public getIndentLevel(lineNumber:number): number {
		if (lineNumber < 1 || lineNumber > this.getLineCount()) {
			throw new Error('Illegal value ' + lineNumber + ' for `lineNumber`');
		}

		return this._lines[lineNumber - 1].getIndentLevel();
	}

A
Alex Dima 已提交
475 476 477 478
	protected _resetIndentRanges(): void {
		this._indentRanges = null;
	}

A
Alex Dima 已提交
479
	private _getIndentRanges(): IndentRange[] {
A
Alex Dima 已提交
480 481 482
		if (!this._indentRanges) {
			this._indentRanges = computeRanges(this);
		}
A
Alex Dima 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
		return this._indentRanges;
	}

	public getIndentRanges(): IndentRange[] {
		let indentRanges = this._getIndentRanges();
		return IndentRange.deepCloneArr(indentRanges);
	}

	public getLineIndentGuide(lineNumber:number): number {
		if (lineNumber < 1 || lineNumber > this.getLineCount()) {
			throw new Error('Illegal value ' + lineNumber + ' for `lineNumber`');
		}

		let indentRanges = this._getIndentRanges();

		for (let i = indentRanges.length - 1; i >= 0; i--) {
			let rng = indentRanges[i];

501
			if (rng.startLineNumber === lineNumber) {
502
				return Math.ceil(rng.indent / this._options.tabSize);
503
			}
A
Alex Dima 已提交
504 505 506
			if (rng.startLineNumber < lineNumber && lineNumber <= rng.endLineNumber) {
				return 1 + Math.floor(rng.indent / this._options.tabSize);
			}
507
			if (rng.endLineNumber + 1 === lineNumber) {
508 509 510 511 512 513 514
				let bestIndent = rng.indent;
				while (i > 0) {
					i--;
					rng = indentRanges[i];
					if (rng.endLineNumber + 1 === lineNumber) {
						bestIndent = rng.indent;
					}
515
				}
516
				return Math.ceil(bestIndent / this._options.tabSize);
517
			}
A
Alex Dima 已提交
518 519 520
		}

		return 0;
A
Alex Dima 已提交
521 522
	}

E
Erich Gamma 已提交
523 524 525 526 527 528 529 530 531 532 533 534
	public getLinesContent(): string[] {
		var r: string[] = [];
		for (var i = 0, len = this._lines.length; i < len; i++) {
			r[i] = this._lines[i].text;
		}
		return r;
	}

	public getEOL(): string {
		return this._EOL;
	}

A
Alex Dima 已提交
535 536
	public setEOL(eol: editorCommon.EndOfLineSequence): void {
		var newEOL = (eol === editorCommon.EndOfLineSequence.CRLF ? '\r\n' : '\n');
E
Erich Gamma 已提交
537 538 539 540 541 542 543 544 545 546 547
		if (this._EOL === newEOL) {
			// Nothing to do
			return;
		}

		var oldFullModelRange = this.getFullModelRange();
		var oldModelValueLength = this.getValueLengthInRange(oldFullModelRange);
		var endLineNumber = this.getLineCount();
		var endColumn = this.getLineMaxColumn(endLineNumber);

		this._EOL = newEOL;
548
		this._lineStarts = null;
E
Erich Gamma 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
		this._increaseVersionId();

		var e = this._createContentChangedFlushEvent();
		e.detail = this.toRawText();
		e.versionId = this._versionId;

		this._emitModelContentChangedFlushEvent(e);
		this._emitContentChanged2(1, 1, endLineNumber, endColumn, oldModelValueLength, this.getValue(), false, false);
	}

	public getLineMinColumn(lineNumber:number): number {
		return 1;
	}

	public getLineMaxColumn(lineNumber:number): number {
		if (lineNumber < 1 || lineNumber > this.getLineCount()) {
			throw new Error('Illegal value ' + lineNumber + ' for `lineNumber`');
		}

		return this._lines[lineNumber - 1].text.length + 1;
	}

	public getLineFirstNonWhitespaceColumn(lineNumber: number): number {
		if (lineNumber < 1 || lineNumber > this.getLineCount()) {
			throw new Error('Illegal value ' + lineNumber + ' for `lineNumber`');
		}

A
Alex Dima 已提交
576
		var result = strings.firstNonWhitespaceIndex(this._lines[lineNumber - 1].text);
E
Erich Gamma 已提交
577 578 579 580 581 582 583 584 585 586 587
		if (result === -1) {
			return 0;
		}
		return result + 1;
	}

	public getLineLastNonWhitespaceColumn(lineNumber: number): number {
		if (lineNumber < 1 || lineNumber > this.getLineCount()) {
			throw new Error('Illegal value ' + lineNumber + ' for `lineNumber`');
		}

A
Alex Dima 已提交
588
		var result = strings.lastNonWhitespaceIndex(this._lines[lineNumber - 1].text);
E
Erich Gamma 已提交
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
		if (result === -1) {
			return 0;
		}
		return result + 2;
	}

	public validateLineNumber(lineNumber:number): number {
		if (lineNumber < 1) {
			lineNumber = 1;
		}
		if (lineNumber > this._lines.length) {
			lineNumber = this._lines.length;
		}
		return lineNumber;
	}

A
Alex Dima 已提交
605
	public validatePosition(position:editorCommon.IPosition): Position {
E
Erich Gamma 已提交
606 607 608 609 610
		var lineNumber = position.lineNumber ? position.lineNumber : 1;
		var column = position.column ? position.column : 1;

		if (lineNumber < 1) {
			lineNumber = 1;
A
aioute Gao 已提交
611
			column = 1;
E
Erich Gamma 已提交
612
		}
A
aioute Gao 已提交
613
		else if (lineNumber > this._lines.length) {
E
Erich Gamma 已提交
614
			lineNumber = this._lines.length;
A
aioute Gao 已提交
615
			column = this.getLineMaxColumn(lineNumber);
E
Erich Gamma 已提交
616
		}
A
aioute Gao 已提交
617 618 619 620 621 622 623 624
		else {
			var maxColumn = this.getLineMaxColumn(lineNumber);
			if (column < 1) {
				column = 1;
			}
			else if (column > maxColumn) {
				column = maxColumn;
			}
E
Erich Gamma 已提交
625 626 627 628 629
		}

		return new Position(lineNumber, column);
	}

630
	public validateRange(range:editorCommon.IRange): Range {
E
Erich Gamma 已提交
631 632 633 634 635
		var start = this.validatePosition(new Position(range.startLineNumber, range.startColumn));
		var end = this.validatePosition(new Position(range.endLineNumber, range.endColumn));
		return new Range(start.lineNumber, start.column, end.lineNumber, end.column);
	}

A
Alex Dima 已提交
636
	public modifyPosition(rawPosition: editorCommon.IPosition, offset: number) : Position {
637
		return this.getPositionAt(this.getOffsetAt(rawPosition) + offset);
E
Erich Gamma 已提交
638 639
	}

640
	public getFullModelRange(): Range {
E
Erich Gamma 已提交
641 642 643 644
		var lineCount = this.getLineCount();
		return new Range(1, 1, lineCount, this.getLineMaxColumn(lineCount));
	}

A
Alex Dima 已提交
645
	protected _emitModelContentChangedFlushEvent(e:editorCommon.IModelContentChangedFlushEvent): void {
E
Erich Gamma 已提交
646
		if (!this._isDisposing) {
A
Alex Dima 已提交
647
			this.emit(editorCommon.EventType.ModelRawContentChanged, e);
E
Erich Gamma 已提交
648 649 650
		}
	}

651
	public static toRawText(rawText:string, opts:editorCommon.ITextModelCreationOptions): editorCommon.IRawText {
E
Erich Gamma 已提交
652 653 654 655 656 657 658
		// Count the number of lines that end with \r\n
		var carriageReturnCnt = 0,
			lastCarriageReturnIndex = -1;
		while ((lastCarriageReturnIndex = rawText.indexOf('\r', lastCarriageReturnIndex + 1)) !== -1) {
			carriageReturnCnt++;
		}

A
Alex Dima 已提交
659
		// Split the text into lines
E
Erich Gamma 已提交
660 661 662 663
		var lines = rawText.split(/\r\n|\r|\n/);

		// Remove the BOM (if present)
		var BOM = '';
A
Alex Dima 已提交
664 665
		if (strings.startsWithUTF8BOM(lines[0])) {
			BOM = strings.UTF8_BOM_CHARACTER;
E
Erich Gamma 已提交
666 667 668 669 670 671 672
			lines[0] = lines[0].substr(1);
		}

		var lineFeedCnt = lines.length - 1;
		var EOL = '';
		if (lineFeedCnt === 0) {
			// This is an empty file or a file with precisely one line
673
			EOL = (opts.defaultEOL === editorCommon.DefaultEndOfLine.LF ? '\n' : '\r\n');
E
Erich Gamma 已提交
674 675 676 677 678 679 680 681
		} else if (carriageReturnCnt > lineFeedCnt / 2) {
			// More than half of the file contains \r\n ending lines
			EOL = '\r\n';
		} else {
			// At least one line more ends in \n
			EOL = '\n';
		}

682
		let resolvedOpts: editorCommon.ITextModelResolvedOptions;
683
		if (opts.detectIndentation) {
684
			let guessedIndentation = guessIndentation(lines, opts.tabSize, opts.insertSpaces);
685 686 687
			resolvedOpts = {
				tabSize: guessedIndentation.tabSize,
				insertSpaces: guessedIndentation.insertSpaces,
688
				trimAutoWhitespace: opts.trimAutoWhitespace,
689 690 691 692 693 694
				defaultEOL: opts.defaultEOL
			};
		} else {
			resolvedOpts = {
				tabSize: opts.tabSize,
				insertSpaces: opts.insertSpaces,
695
				trimAutoWhitespace: opts.trimAutoWhitespace,
696 697 698 699
				defaultEOL: opts.defaultEOL
			};
		}

E
Erich Gamma 已提交
700 701 702 703
		return {
			BOM: BOM,
			EOL: EOL,
			lines: lines,
704
			length: rawText.length,
705
			options: resolvedOpts
E
Erich Gamma 已提交
706 707 708
		};
	}

A
Alex Dima 已提交
709
	protected _constructLines(rawText:editorCommon.IRawText): void {
710 711 712
		const tabSize = rawText.options.tabSize;
		let rawLines = rawText.lines;
		let modelLines: ModelLine[] = [];
E
Erich Gamma 已提交
713

714 715
		for (let i = 0, len = rawLines.length; i < len; i++) {
			modelLines[i] = new ModelLine(i + 1, rawLines[i], tabSize);
E
Erich Gamma 已提交
716 717 718 719
		}
		this._BOM = rawText.BOM;
		this._EOL = rawText.EOL;
		this._lines = modelLines;
720
		this._lineStarts = null;
A
Alex Dima 已提交
721
		this._resetIndentRanges();
E
Erich Gamma 已提交
722 723
	}

A
Alex Dima 已提交
724
	private _getEndOfLine(eol:editorCommon.EndOfLinePreference): string {
E
Erich Gamma 已提交
725
		switch (eol) {
A
Alex Dima 已提交
726
			case editorCommon.EndOfLinePreference.LF:
E
Erich Gamma 已提交
727
				return '\n';
A
Alex Dima 已提交
728
			case editorCommon.EndOfLinePreference.CRLF:
E
Erich Gamma 已提交
729
				return '\r\n';
A
Alex Dima 已提交
730
			case editorCommon.EndOfLinePreference.TextDefined:
E
Erich Gamma 已提交
731 732 733 734 735
				return this.getEOL();
		}
		throw new Error('Unknown EOL preference');
	}

736 737 738 739 740 741 742 743
	private static _isMultiline(searchString:string): boolean {
		if (!searchString || searchString.length === 0) {
			return false;
		}

		for (let i = 0, len = searchString.length; i < len; i++) {
			let chCode = searchString.charCodeAt(i);

A
Alex Dima 已提交
744
			if (chCode === CharCode.Backslash) {
745 746 747 748 749 750 751 752 753 754

				// move to next char
				i++;

				if (i >= len) {
					// string ends with a \
					break;
				}

				let nextChCode = searchString.charCodeAt(i);
A
Alex Dima 已提交
755
				if (nextChCode === CharCode.n || nextChCode === CharCode.r) {
756 757 758 759 760 761 762 763
					return true;
				}
			}
		}

		return false;
	}

S
Sandeep Somavarapu 已提交
764
	public static parseSearchRequest(searchString:string, isRegex:boolean, matchCase:boolean, wholeWord:boolean): RegExp {
765 766 767 768 769
		if (searchString === '') {
			return null;
		}

		// Try to create a RegExp out of the params
S
Sandeep Somavarapu 已提交
770 771
		var regex: RegExp = null;
		var multiline = isRegex && TextModel._isMultiline(searchString);
772
		try {
S
Sandeep Somavarapu 已提交
773
			regex = strings.createRegExp(searchString, isRegex, {matchCase, wholeWord, multiline, global: true});
774 775 776 777
		} catch (err) {
			return null;
		}

E
Erich Gamma 已提交
778
		if (!regex) {
779 780 781
			return null;
		}

S
Sandeep Somavarapu 已提交
782
		return regex;
783 784 785
	}

	public findMatches(searchString:string, rawSearchScope:any, isRegex:boolean, matchCase:boolean, wholeWord:boolean, limitResultCount:number = LIMIT_FIND_COUNT): Range[] {
S
Sandeep Somavarapu 已提交
786 787
		let regex = TextModel.parseSearchRequest(searchString, isRegex, matchCase, wholeWord);
		if (!regex) {
E
Erich Gamma 已提交
788 789 790
			return [];
		}

791
		let searchRange:Range;
E
Erich Gamma 已提交
792
		if (Range.isIRange(rawSearchScope)) {
A
Alex Dima 已提交
793
			searchRange = this.validateRange(rawSearchScope);
E
Erich Gamma 已提交
794 795 796 797
		} else {
			searchRange = this.getFullModelRange();
		}

S
Sandeep Somavarapu 已提交
798 799
		if (regex.multiline) {
			return this._doFindMatchesMultiline(searchRange, regex, limitResultCount);
800
		}
S
Sandeep Somavarapu 已提交
801
		return this._doFindMatchesLineByLine(searchRange, regex, limitResultCount);
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
	}

	private _doFindMatchesMultiline(searchRange:Range, searchRegex:RegExp, limitResultCount:number): Range[] {
		let deltaOffset = this.getOffsetAt(searchRange.getStartPosition());
		let text = this.getValueInRange(searchRange);

		let result: Range[] = [];
		let prevStartOffset = 0;
		let prevEndOffset = 0;
		let counter = 0;

		let m:RegExpExecArray;
		while ((m = searchRegex.exec(text))) {
			let startOffset = deltaOffset + m.index;
			let endOffset = startOffset + m[0].length;

			if (prevStartOffset === startOffset && prevEndOffset === endOffset) {
				// Exit early if the regex matches the same range
				return result;
			}

			let startPosition = this.getPositionAt(startOffset);
			let endPosition = this.getPositionAt(endOffset);

			result[counter++] = new Range(startPosition.lineNumber, startPosition.column, endPosition.lineNumber, endPosition.column);
			if (counter >= limitResultCount) {
				return result;
			}

			prevStartOffset = startOffset;
			prevEndOffset = endOffset;
		}

		return result;
	}

	private _doFindMatchesLineByLine(searchRange:Range, searchRegex:RegExp, limitResultCount:number): Range[] {
		let result:Range[] = [];
		let text: string;
		let counter = 0;

		// Early case for a search range that starts & stops on the same line number
		if (searchRange.startLineNumber === searchRange.endLineNumber) {
			text = this._lines[searchRange.startLineNumber - 1].text.substring(searchRange.startColumn - 1, searchRange.endColumn - 1);
			counter = this._findMatchesInLine(searchRegex, text, searchRange.startLineNumber, searchRange.startColumn - 1, counter, result, limitResultCount);
			return result;
		}

		// Collect results from first line
		text = this._lines[searchRange.startLineNumber - 1].text.substring(searchRange.startColumn - 1);
		counter = this._findMatchesInLine(searchRegex, text, searchRange.startLineNumber, searchRange.startColumn - 1, counter, result, limitResultCount);

		// Collect results from middle lines
		for (let lineNumber = searchRange.startLineNumber + 1; lineNumber < searchRange.endLineNumber && counter < limitResultCount; lineNumber++) {
			counter = this._findMatchesInLine(searchRegex, this._lines[lineNumber - 1].text, lineNumber, 0, counter, result, limitResultCount);
		}

		// Collect results from last line
		if (counter < limitResultCount) {
			text = this._lines[searchRange.endLineNumber - 1].text.substring(0, searchRange.endColumn - 1);
			counter = this._findMatchesInLine(searchRegex, text, searchRange.endLineNumber, 0, counter, result, limitResultCount);
		}

		return result;
E
Erich Gamma 已提交
866 867
	}

868
	public findNextMatch(searchString:string, rawSearchStart:editorCommon.IPosition, isRegex:boolean, matchCase:boolean, wholeWord:boolean): Range {
S
Sandeep Somavarapu 已提交
869 870
		let regex = TextModel.parseSearchRequest(searchString, isRegex, matchCase, wholeWord);
		if (!regex) {
E
Erich Gamma 已提交
871 872 873
			return null;
		}

874
		let searchStart = this.validatePosition(rawSearchStart);
S
Sandeep Somavarapu 已提交
875 876
		if (regex.multiline) {
			return this._doFindNextMatchMultiline(searchStart, regex);
877
		}
S
Sandeep Somavarapu 已提交
878
		return this._doFindNextMatchLineByLine(searchStart, regex);
879 880 881

	}

S
Sandeep Somavarapu 已提交
882 883 884 885 886
	private _doFindNextMatchMultiline(searchStart: Position, searchRegex: RegExp): Range {
		let searchTextStart: editorCommon.IPosition = { lineNumber: searchStart.lineNumber, column: 1 };
		let deltaOffset = this.getOffsetAt(searchTextStart);
		let text = this.getValueInRange(new Range(searchTextStart.lineNumber, searchTextStart.column, this.getLineCount(), this.getLineMaxColumn(this.getLineCount())));
		searchRegex.lastIndex = searchStart.column - 1;
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
		let m = searchRegex.exec(text);
		if (m) {
			let startOffset = deltaOffset + m.index;
			let endOffset = startOffset + m[0].length;
			let startPosition = this.getPositionAt(startOffset);
			let endPosition = this.getPositionAt(endOffset);
			return new Range(startPosition.lineNumber, startPosition.column, endPosition.lineNumber, endPosition.column);
		}

		if (searchStart.lineNumber !== 1 || searchStart.column !== -1) {
			// Try again from the top
			return this._doFindNextMatchMultiline(new Position(1, 1), searchRegex);
		}

		return null;
	}

	private _doFindNextMatchLineByLine(searchStart:Position, searchRegex:RegExp): Range {
		let lineCount = this.getLineCount();
		let startLineNumber = searchStart.lineNumber;
		let text: string;
		let r: Range;
E
Erich Gamma 已提交
909 910

		// Look in first line
S
Sandeep Somavarapu 已提交
911 912
		text = this._lines[startLineNumber - 1].text;
		r = this._findFirstMatchInLine(searchRegex, text, startLineNumber, searchStart.column);
E
Erich Gamma 已提交
913 914 915 916
		if (r) {
			return r;
		}

917 918
		for (let i = 1; i <= lineCount; i++) {
			let lineIndex = (startLineNumber + i - 1) % lineCount;
E
Erich Gamma 已提交
919
			text = this._lines[lineIndex].text;
S
Sandeep Somavarapu 已提交
920
			r = this._findFirstMatchInLine(searchRegex, text, lineIndex + 1, 1);
E
Erich Gamma 已提交
921 922 923 924 925 926 927 928
			if (r) {
				return r;
			}
		}

		return null;
	}

929
	public findPreviousMatch(searchString:string, rawSearchStart:editorCommon.IPosition, isRegex:boolean, matchCase:boolean, wholeWord:boolean): Range {
S
Sandeep Somavarapu 已提交
930 931
		let regex = TextModel.parseSearchRequest(searchString, isRegex, matchCase, wholeWord);
		if (!regex) {
932 933 934
			return null;
		}

935
		let searchStart = this.validatePosition(rawSearchStart);
S
Sandeep Somavarapu 已提交
936 937
		if (regex.multiline) {
			return this._doFindPreviousMatchMultiline(searchStart, regex);
938
		}
S
Sandeep Somavarapu 已提交
939
		return this._doFindPreviousMatchLineByLine(searchStart, regex);
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
	}

	private _doFindPreviousMatchMultiline(searchStart:Position, searchRegex:RegExp): Range {
		let matches = this._doFindMatchesMultiline(new Range(1, 1, searchStart.lineNumber, searchStart.column), searchRegex, 10 * LIMIT_FIND_COUNT);
		if (matches.length > 0) {
			return matches[matches.length - 1];
		}

		if (searchStart.lineNumber !== this.getLineCount() || searchStart.column !== this.getLineMaxColumn(this.getLineCount())) {
			// Try again with all content
			return this._doFindPreviousMatchMultiline(new Position(this.getLineCount(), this.getLineMaxColumn(this.getLineCount())), searchRegex);
		}

		return null;
	}

	private _doFindPreviousMatchLineByLine(searchStart:Position, searchRegex:RegExp): Range {
		let lineCount = this.getLineCount();
		let startLineNumber = searchStart.lineNumber;
		let text: string;
		let r: Range;
961 962 963

		// Look in first line
		text = this._lines[startLineNumber - 1].text.substring(0, searchStart.column - 1);
964
		r = this._findLastMatchInLine(searchRegex, text, startLineNumber);
965 966 967 968
		if (r) {
			return r;
		}

969
		for (var i = 1; i <= lineCount; i++) {
970 971
			var lineIndex = (lineCount + startLineNumber - i - 1) % lineCount;
			text = this._lines[lineIndex].text;
972
			r = this._findLastMatchInLine(searchRegex, text, lineIndex + 1);
973 974 975 976 977 978 979 980
			if (r) {
				return r;
			}
		}

		return null;
	}

S
Sandeep Somavarapu 已提交
981 982 983 984 985
	private _findFirstMatchInLine(searchRegex: RegExp, text: string, lineNumber: number, fromColumn: number): Range {
		// Set regex to search from column
		searchRegex.lastIndex = fromColumn - 1;
		var m: RegExpExecArray = searchRegex.exec(text);
		return m ? new Range(lineNumber, m.index + 1, lineNumber, m.index + 1 + m[0].length) : null;
E
Erich Gamma 已提交
986 987
	}

988 989
	private _findLastMatchInLine(searchRegex:RegExp, text:string, lineNumber:number): Range {
		let bestResult: Range = null;
990 991 992 993 994 995 996
		let m:RegExpExecArray;
		while ((m = searchRegex.exec(text))) {
			let result = new Range(lineNumber, m.index + 1, lineNumber, m.index + 1 + m[0].length);
			if (result.equalsRange(bestResult)) {
				break;
			}
			bestResult = result;
997 998 999 1000
			if (m.index + m[0].length === text.length) {
				// Reached the end of the line
				break;
			}
1001 1002 1003 1004
		}
		return bestResult;
	}

1005
	private _findMatchesInLine(searchRegex:RegExp, text:string, lineNumber:number, deltaOffset:number, counter:number, result:Range[], limitResultCount:number): number {
E
Erich Gamma 已提交
1006
		var m:RegExpExecArray;
1007 1008
		// Reset regex to search from the beginning
		searchRegex.lastIndex = 0;
E
Erich Gamma 已提交
1009 1010 1011
		do {
			m = searchRegex.exec(text);
			if (m) {
1012 1013
				var range = new Range(lineNumber, m.index + 1 + deltaOffset, lineNumber, m.index + 1 + m[0].length + deltaOffset);
				if (range.equalsRange(result[result.length - 1])) {
1014
					// Exit early if the regex matches the same range
1015 1016 1017
					return counter;
				}
				result.push(range);
E
Erich Gamma 已提交
1018 1019 1020 1021
				counter++;
				if (counter >= limitResultCount) {
					return counter;
				}
1022 1023 1024 1025
				if (m.index + m[0].length === text.length) {
					// Reached the end of the line
					return counter;
				}
E
Erich Gamma 已提交
1026 1027 1028 1029
			}
		} while(m);
		return counter;
	}
1030
}
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

export class RawText {

	public static fromString(rawText:string, opts:editorCommon.ITextModelCreationOptions): editorCommon.IRawText {
		return TextModel.toRawText(rawText, opts);
	}

	public static fromStringWithModelOptions(rawText:string, model:editorCommon.IModel): editorCommon.IRawText {
		let opts = model.getOptions();
		return TextModel.toRawText(rawText, {
			tabSize: opts.tabSize,
			insertSpaces: opts.insertSpaces,
1043
			trimAutoWhitespace: opts.trimAutoWhitespace,
1044 1045 1046 1047 1048
			detectIndentation: false,
			defaultEOL: opts.defaultEOL
		});
	}

A
aioute Gao 已提交
1049
}