Skip to content

Commit 668dc06

Browse files
committed
Extract more code to textSource.ts
1 parent 220a10c commit 668dc06

22 files changed

Lines changed: 131 additions & 119 deletions

src/vs/editor/common/editorCommon.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { IndentRange } from 'vs/editor/common/model/indentRanges';
2121
import { ICommandHandlerDescription } from 'vs/platform/commands/common/commands';
2222
import { ContextKeyExpr, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
2323
import { FontInfo } from 'vs/editor/common/config/fontInfo';
24+
import { ITextSource } from 'vs/editor/common/model/textSource';
2425

2526
/**
2627
* @internal
@@ -2457,37 +2458,6 @@ export interface IModelContentChangedEvent {
24572458
readonly isRedoing: boolean;
24582459
}
24592460

2460-
/**
2461-
* The raw text backing a model.
2462-
* @internal
2463-
*/
2464-
export interface ITextSource {
2465-
/**
2466-
* The entire text length.
2467-
*/
2468-
readonly length: number;
2469-
/**
2470-
* The text split into lines.
2471-
*/
2472-
readonly lines: string[];
2473-
/**
2474-
* The BOM (leading character sequence of the file).
2475-
*/
2476-
readonly BOM: string;
2477-
/**
2478-
* The end of line sequence.
2479-
*/
2480-
readonly EOL: string;
2481-
/**
2482-
* The text contains Unicode characters classified as "R" or "AL".
2483-
*/
2484-
readonly containsRTL: boolean;
2485-
/**
2486-
* The text contains only characters inside the ASCII range 32-126 or \t \r \n
2487-
*/
2488-
readonly isBasicASCII: boolean;
2489-
}
2490-
24912461
/**
24922462
* The raw text backing a model.
24932463
* @internal

src/vs/editor/common/model/editableTextModel.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Selection } from 'vs/editor/common/core/selection';
1414
import { Position } from 'vs/editor/common/core/position';
1515
import { IDisposable } from 'vs/base/common/lifecycle';
1616
import { LanguageIdentifier } from 'vs/editor/common/modes';
17+
import { ITextSource } from 'vs/editor/common/model/textSource';
1718

1819
export interface IValidatedEditOperation {
1920
sortIndex: number;
@@ -69,7 +70,7 @@ export class EditableTextModel extends TextModelWithDecorations implements edito
6970
super.dispose();
7071
}
7172

72-
protected _resetValue(newValue: editorCommon.ITextSource): void {
73+
protected _resetValue(newValue: ITextSource): void {
7374
super._resetValue(newValue);
7475

7576
// Destroy my edit history and settings

src/vs/editor/common/model/textModel.ts

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { DEFAULT_INDENTATION, DEFAULT_TRIM_AUTO_WHITESPACE } from 'vs/editor/com
1515
import { PrefixSumComputer } from 'vs/editor/common/viewModel/prefixSumComputer';
1616
import { IndentRange, computeRanges } from 'vs/editor/common/model/indentRanges';
1717
import { TextModelSearch, SearchParams } from 'vs/editor/common/model/textModelSearch';
18-
import { ITextSource2 } from 'vs/editor/common/model/textSource';
18+
import { ITextSource, RawTextSource, IRawTextSource } from 'vs/editor/common/model/textSource';
1919

2020
const LIMIT_FIND_COUNT = 999;
2121
export const LONG_LINE_BOUNDARY = 1000;
@@ -286,7 +286,7 @@ export class TextModel extends OrderGuaranteeEventEmitter implements editorCommo
286286
}
287287
}
288288

289-
protected _resetValue(newValue: editorCommon.ITextSource): void {
289+
protected _resetValue(newValue: ITextSource): void {
290290
this._constructLines(newValue);
291291
this._increaseVersionId();
292292
}
@@ -304,7 +304,7 @@ export class TextModel extends OrderGuaranteeEventEmitter implements editorCommo
304304
};
305305
}
306306

307-
public equals(other: editorCommon.ITextSource): boolean {
307+
public equals(other: ITextSource): boolean {
308308
this._assertNotDisposed();
309309
if (this._BOM !== other.BOM) {
310310
return false;
@@ -340,7 +340,7 @@ export class TextModel extends OrderGuaranteeEventEmitter implements editorCommo
340340
this.setValueFromRawText(rawText);
341341
}
342342

343-
public setValueFromRawText(newValue: editorCommon.ITextSource): void {
343+
public setValueFromRawText(newValue: ITextSource): void {
344344
this._assertNotDisposed();
345345
if (newValue === null) {
346346
// There's nothing to do
@@ -750,43 +750,12 @@ export class TextModel extends OrderGuaranteeEventEmitter implements editorCommo
750750
}
751751
}
752752

753-
public static toTextSource(rawText: string): ITextSource2 {
754-
// Count the number of lines that end with \r\n
755-
let carriageReturnCnt = 0;
756-
let lastCarriageReturnIndex = -1;
757-
while ((lastCarriageReturnIndex = rawText.indexOf('\r', lastCarriageReturnIndex + 1)) !== -1) {
758-
carriageReturnCnt++;
759-
}
760-
761-
const containsRTL = strings.containsRTL(rawText);
762-
const isBasicASCII = (containsRTL ? false : strings.isBasicASCII(rawText));
763-
764-
// Split the text into lines
765-
const lines = rawText.split(/\r\n|\r|\n/);
766-
767-
// Remove the BOM (if present)
768-
let BOM = '';
769-
if (strings.startsWithUTF8BOM(lines[0])) {
770-
BOM = strings.UTF8_BOM_CHARACTER;
771-
lines[0] = lines[0].substr(1);
772-
}
773-
774-
return {
775-
BOM: BOM,
776-
lines: lines,
777-
length: rawText.length,
778-
containsRTL: containsRTL,
779-
isBasicASCII: isBasicASCII,
780-
totalCRCount: carriageReturnCnt
781-
};
782-
}
783-
784753
/**
785754
* if text source is empty or with precisely one line, returns null. No end of line is detected.
786755
* if text source contains more lines ending with '\r\n', returns '\r\n'.
787756
* Otherwise returns '\n'. More lines end with '\n'.
788757
*/
789-
public static getEndOfLine(textSource: ITextSource2): string {
758+
public static getEndOfLine(textSource: IRawTextSource): string {
790759
const lineFeedCnt = textSource.lines.length - 1;
791760
if (lineFeedCnt === 0) {
792761
// This is an empty file or a file with precisely one line
@@ -801,11 +770,11 @@ export class TextModel extends OrderGuaranteeEventEmitter implements editorCommo
801770
}
802771

803772
public static toRawText(rawText: string, opts: editorCommon.ITextModelCreationOptions): editorCommon.IRawText {
804-
const textSource = TextModel.toTextSource(rawText);
773+
const textSource = RawTextSource.fromString(rawText);
805774
return TextModel.toRawTextFromTextSource(textSource, opts);
806775
}
807776

808-
public static toRawTextFromTextSource(textSource: ITextSource2, opts: editorCommon.ITextModelCreationOptions): editorCommon.IRawText {
777+
public static toRawTextFromTextSource(textSource: IRawTextSource, opts: editorCommon.ITextModelCreationOptions): editorCommon.IRawText {
809778
let EOL = TextModel.getEndOfLine(textSource);
810779
if (!EOL) {
811780
// This is an empty file or a file with precisely one line
@@ -841,7 +810,7 @@ export class TextModel extends OrderGuaranteeEventEmitter implements editorCommo
841810
};
842811
}
843812

844-
private _constructLines(rawText: editorCommon.ITextSource): void {
813+
private _constructLines(rawText: ITextSource): void {
845814
const tabSize = this._options.tabSize;
846815
let rawLines = rawText.lines;
847816
let modelLines: ModelLine[] = [];
@@ -898,15 +867,15 @@ export class TextModel extends OrderGuaranteeEventEmitter implements editorCommo
898867

899868
export class RawText {
900869

901-
public static toRawText(textSourceOrString: ITextSource2 | string, opts: editorCommon.ITextModelCreationOptions): editorCommon.IRawText {
870+
public static toRawText(textSourceOrString: IRawTextSource | string, opts: editorCommon.ITextModelCreationOptions): editorCommon.IRawText {
902871
if (typeof textSourceOrString === 'string') {
903872
return RawText.fromString(textSourceOrString, opts);
904873
} else {
905874
return RawText.fromTextSource(textSourceOrString, opts);
906875
}
907876
}
908877

909-
public static toRawTextWithModelOptions(textSourceOrString: ITextSource2 | string, model: editorCommon.IModel): editorCommon.IRawText {
878+
public static toRawTextWithModelOptions(textSourceOrString: IRawTextSource | string, model: editorCommon.IModel): editorCommon.IRawText {
910879
if (typeof textSourceOrString === 'string') {
911880
return RawText.fromStringWithModelOptions(textSourceOrString, model);
912881
} else {
@@ -918,7 +887,7 @@ export class RawText {
918887
return TextModel.toRawText(rawText, opts);
919888
}
920889

921-
public static fromTextSource(textSource: ITextSource2, opts: editorCommon.ITextModelCreationOptions): editorCommon.IRawText {
890+
public static fromTextSource(textSource: IRawTextSource, opts: editorCommon.ITextModelCreationOptions): editorCommon.IRawText {
922891
return TextModel.toRawTextFromTextSource(textSource, opts);
923892
}
924893

@@ -933,7 +902,7 @@ export class RawText {
933902
});
934903
}
935904

936-
public static fromTextSourceWithModelOptions(textSource: ITextSource2, model: editorCommon.IModel): editorCommon.IRawText {
905+
public static fromTextSourceWithModelOptions(textSource: IRawTextSource, model: editorCommon.IModel): editorCommon.IRawText {
937906
let opts = model.getOptions();
938907
return TextModel.toRawTextFromTextSource(textSource, {
939908
tabSize: opts.tabSize,

src/vs/editor/common/model/textModelWithDecorations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { MarkersTracker, LineMarker } from 'vs/editor/common/model/modelLine';
1414
import { Position } from 'vs/editor/common/core/position';
1515
import { INewMarker, TextModelWithMarkers } from 'vs/editor/common/model/textModelWithMarkers';
1616
import { LanguageIdentifier } from 'vs/editor/common/modes';
17+
import { ITextSource } from 'vs/editor/common/model/textSource';
1718

1819
class DecorationsTracker {
1920

@@ -165,7 +166,7 @@ export class TextModelWithDecorations extends TextModelWithMarkers implements ed
165166
super.dispose();
166167
}
167168

168-
protected _resetValue(newValue: editorCommon.ITextSource): void {
169+
protected _resetValue(newValue: ITextSource): void {
169170
super._resetValue(newValue);
170171

171172
// Destroy all my decorations

src/vs/editor/common/model/textModelWithMarkers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
import { IdGenerator } from 'vs/base/common/idGenerator';
88
import { Position } from 'vs/editor/common/core/position';
9-
import { ITextSource, IRawText, ITextModelWithMarkers } from 'vs/editor/common/editorCommon';
9+
import { IRawText, ITextModelWithMarkers } from 'vs/editor/common/editorCommon';
1010
import { LineMarker } from 'vs/editor/common/model/modelLine';
1111
import { TextModelWithTokens } from 'vs/editor/common/model/textModelWithTokens';
1212
import { LanguageIdentifier } from 'vs/editor/common/modes';
13+
import { ITextSource } from 'vs/editor/common/model/textSource';
1314

1415
export interface IMarkerIdToMarkerMap {
1516
[key: string]: LineMarker;

src/vs/editor/common/model/textModelWithTokens.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageCo
2121
import { LineTokens, LineToken } from 'vs/editor/common/core/lineTokens';
2222
import { getWordAtText } from 'vs/editor/common/model/wordHelper';
2323
import { TokenizationResult2 } from 'vs/editor/common/core/token';
24+
import { ITextSource } from 'vs/editor/common/model/textSource';
2425

2526
class ModelTokensChangedEventBuilder {
2627

@@ -107,7 +108,7 @@ export class TextModelWithTokens extends TextModel implements editorCommon.IToke
107108
return false;
108109
}
109110

110-
protected _resetValue(newValue: editorCommon.ITextSource): void {
111+
protected _resetValue(newValue: ITextSource): void {
111112
super._resetValue(newValue);
112113
// Cancel tokenization, clear all tokens and begin tokenizing
113114
this._resetTokenizationState();

src/vs/editor/common/model/textSource.ts

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
*--------------------------------------------------------------------------------------------*/
55
'use strict';
66

7+
import * as strings from 'vs/base/common/strings';
8+
79
/**
8-
* The text source
10+
* A processed string ready to be turned into an editor model.
911
*/
10-
export interface ITextSource2 {
12+
export interface IRawTextSource {
1113
/**
1214
* The entire text length.
1315
*/
@@ -33,3 +35,68 @@ export interface ITextSource2 {
3335
*/
3436
readonly isBasicASCII: boolean;
3537
}
38+
39+
export class RawTextSource {
40+
41+
public static fromString(rawText: string): IRawTextSource {
42+
// Count the number of lines that end with \r\n
43+
let carriageReturnCnt = 0;
44+
let lastCarriageReturnIndex = -1;
45+
while ((lastCarriageReturnIndex = rawText.indexOf('\r', lastCarriageReturnIndex + 1)) !== -1) {
46+
carriageReturnCnt++;
47+
}
48+
49+
const containsRTL = strings.containsRTL(rawText);
50+
const isBasicASCII = (containsRTL ? false : strings.isBasicASCII(rawText));
51+
52+
// Split the text into lines
53+
const lines = rawText.split(/\r\n|\r|\n/);
54+
55+
// Remove the BOM (if present)
56+
let BOM = '';
57+
if (strings.startsWithUTF8BOM(lines[0])) {
58+
BOM = strings.UTF8_BOM_CHARACTER;
59+
lines[0] = lines[0].substr(1);
60+
}
61+
62+
return {
63+
BOM: BOM,
64+
lines: lines,
65+
length: rawText.length,
66+
containsRTL: containsRTL,
67+
isBasicASCII: isBasicASCII,
68+
totalCRCount: carriageReturnCnt
69+
};
70+
}
71+
72+
}
73+
74+
/**
75+
* A processed string with its EOL resolved ready to be turned into an editor model.
76+
*/
77+
export interface ITextSource {
78+
/**
79+
* The entire text length.
80+
*/
81+
readonly length: number;
82+
/**
83+
* The text split into lines.
84+
*/
85+
readonly lines: string[];
86+
/**
87+
* The BOM (leading character sequence of the file).
88+
*/
89+
readonly BOM: string;
90+
/**
91+
* The end of line sequence.
92+
*/
93+
readonly EOL: string;
94+
/**
95+
* The text contains Unicode characters classified as "R" or "AL".
96+
*/
97+
readonly containsRTL: boolean;
98+
/**
99+
* The text contains only characters inside the ASCII range 32-126 or \t \r \n
100+
*/
101+
readonly isBasicASCII: boolean;
102+
}

src/vs/editor/common/services/modelService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import { TPromise } from 'vs/base/common/winjs.base';
1010
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
1111
import { IModel, ITextModelCreationOptions } from 'vs/editor/common/editorCommon';
1212
import { IMode } from 'vs/editor/common/modes';
13-
import { ITextSource2 } from 'vs/editor/common/model/textSource';
13+
import { IRawTextSource } from 'vs/editor/common/model/textSource';
1414

1515
export var IModelService = createDecorator<IModelService>('modelService');
1616

1717
export interface IModelService {
1818
_serviceBrand: any;
1919

20-
createModel(value: string | ITextSource2, modeOrPromise: TPromise<IMode> | IMode, resource: URI): IModel;
20+
createModel(value: string | IRawTextSource, modeOrPromise: TPromise<IMode> | IMode, resource: URI): IModel;
2121

22-
updateModel(model: IModel, value: string | ITextSource2): void;
22+
updateModel(model: IModel, value: string | IRawTextSource): void;
2323

2424
setMode(model: IModel, modeOrPromise: TPromise<IMode> | IMode): void;
2525

src/vs/editor/common/services/modelServiceImpl.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur
2424
import { DEFAULT_INDENTATION, DEFAULT_TRIM_AUTO_WHITESPACE } from 'vs/editor/common/config/defaultConfig';
2525
import { PLAINTEXT_LANGUAGE_IDENTIFIER } from 'vs/editor/common/modes/modesRegistry';
2626
import { RawText } from 'vs/editor/common/model/textModel';
27-
import { ITextSource2 } from 'vs/editor/common/model/textSource';
27+
import { IRawTextSource } from 'vs/editor/common/model/textSource';
2828

2929
function MODEL_ID(resource: URI): string {
3030
return resource.toString();
@@ -339,7 +339,7 @@ export class ModelServiceImpl implements IModelService {
339339

340340
// --- begin IModelService
341341

342-
private _createModelData(value: string | ITextSource2, languageIdentifier: LanguageIdentifier, resource: URI): ModelData {
342+
private _createModelData(value: string | IRawTextSource, languageIdentifier: LanguageIdentifier, resource: URI): ModelData {
343343
// create & save the model
344344
const options = this.getCreationOptions(languageIdentifier.language);
345345

@@ -358,7 +358,7 @@ export class ModelServiceImpl implements IModelService {
358358
return modelData;
359359
}
360360

361-
public updateModel(model: editorCommon.IModel, value: string | ITextSource2): void {
361+
public updateModel(model: editorCommon.IModel, value: string | IRawTextSource): void {
362362
let options = this.getCreationOptions(model.getLanguageIdentifier().language);
363363
let rawText: editorCommon.IRawText = RawText.toRawText(value, options);
364364

@@ -371,7 +371,7 @@ export class ModelServiceImpl implements IModelService {
371371
model.setValueFromRawText(rawText);
372372
}
373373

374-
public createModel(value: string | ITextSource2, modeOrPromise: TPromise<IMode> | IMode, resource: URI): editorCommon.IModel {
374+
public createModel(value: string | IRawTextSource, modeOrPromise: TPromise<IMode> | IMode, resource: URI): editorCommon.IModel {
375375
let modelData: ModelData;
376376

377377
if (!modeOrPromise || TPromise.is(modeOrPromise)) {

0 commit comments

Comments
 (0)