1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
87
88
89
90
91
92
93
94
95
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
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
179
180
181
182
183
184
185
186
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { LineTokens } from 'vs/editor/common/tokens/lineTokens';
import { Position } from 'vs/editor/common/core/position';
import { IRange } from 'vs/editor/common/core/range';
import { EndOfLinePreference, ITextModel, PositionAffinity } from 'vs/editor/common/model';
import { LineInjectedText } from 'vs/editor/common/textModelEvents';
import { InjectedText, ModelLineProjectionData } from 'vs/editor/common/modelLineProjectionData';
import { SingleLineInlineDecoration, ViewLineData } from 'vs/editor/common/viewModel';
export interface IModelLineProjection {
isVisible(): boolean;
/**
* This invalidates the current instance (potentially reuses and returns it again).
*/
setVisible(isVisible: boolean): IModelLineProjection;
getProjectionData(): ModelLineProjectionData | null;
getViewLineCount(): number;
getViewLineContent(model: ISimpleModel, modelLineNumber: number, outputLineIndex: number): string;
getViewLineLength(model: ISimpleModel, modelLineNumber: number, outputLineIndex: number): number;
getViewLineMinColumn(model: ISimpleModel, modelLineNumber: number, outputLineIndex: number): number;
getViewLineMaxColumn(model: ISimpleModel, modelLineNumber: number, outputLineIndex: number): number;
getViewLineData(model: ISimpleModel, modelLineNumber: number, outputLineIndex: number): ViewLineData;
getViewLinesData(model: ISimpleModel, modelLineNumber: number, outputLineIdx: number, lineCount: number, globalStartIndex: number, needed: boolean[], result: Array<ViewLineData | null>): void;
getModelColumnOfViewPosition(outputLineIndex: number, outputColumn: number): number;
getViewPositionOfModelPosition(deltaLineNumber: number, inputColumn: number, affinity?: PositionAffinity): Position;
getViewLineNumberOfModelPosition(deltaLineNumber: number, inputColumn: number): number;
normalizePosition(outputLineIndex: number, outputPosition: Position, affinity: PositionAffinity): Position;
getInjectedTextAt(outputLineIndex: number, column: number): InjectedText | null;
}
export interface ISimpleModel {
tokenization: {
getLineTokens(lineNumber: number): LineTokens;
};
getLineContent(lineNumber: number): string;
getLineLength(lineNumber: number): number;
getLineMinColumn(lineNumber: number): number;
getLineMaxColumn(lineNumber: number): number;
getValueInRange(range: IRange, eol?: EndOfLinePreference): string;
}
export function createModelLineProjection(lineBreakData: ModelLineProjectionData | null, isVisible: boolean): IModelLineProjection {
if (lineBreakData === null) {
// No mapping needed
if (isVisible) {
return IdentityModelLineProjection.INSTANCE;
}
return HiddenModelLineProjection.INSTANCE;
} else {
return new ModelLineProjection(lineBreakData, isVisible);
}
}
/**
* This projection is used to
* * wrap model lines
* * inject text
*/
class ModelLineProjection implements IModelLineProjection {
private readonly _projectionData: ModelLineProjectionData;
private _isVisible: boolean;
constructor(lineBreakData: ModelLineProjectionData, isVisible: boolean) {
this._projectionData = lineBreakData;
this._isVisible = isVisible;
}
public isVisible(): boolean {
return this._isVisible;
}
public setVisible(isVisible: boolean): IModelLineProjection {
this._isVisible = isVisible;
return this;
}
public getProjectionData(): ModelLineProjectionData | null {
return this._projectionData;
}
public getViewLineCount(): number {
if (!this._isVisible) {
return 0;
}
return this._projectionData.getOutputLineCount();
}
public getViewLineContent(model: ISimpleModel, modelLineNumber: number, outputLineIndex: number): string {
this._assertVisible();
const startOffsetInInputWithInjections = outputLineIndex > 0 ? this._projectionData.breakOffsets[outputLineIndex - 1] : 0;
const endOffsetInInputWithInjections = this._projectionData.breakOffsets[outputLineIndex];
let r: string;
if (this._projectionData.injectionOffsets !== null) {
const injectedTexts = this._projectionData.injectionOffsets.map(
(offset, idx) => new LineInjectedText(
0,
0,
offset + 1,
this._projectionData.injectionOptions![idx],
0
)
);
const lineWithInjections = LineInjectedText.applyInjectedText(
model.getLineContent(modelLineNumber),
injectedTexts
);
r = lineWithInjections.substring(startOffsetInInputWithInjections, endOffsetInInputWithInjections);
} else {
r = model.getValueInRange({
startLineNumber: modelLineNumber,
startColumn: startOffsetInInputWithInjections + 1,
endLineNumber: modelLineNumber,
endColumn: endOffsetInInputWithInjections + 1
});
}
if (outputLineIndex > 0) {
r = spaces(this._projectionData.wrappedTextIndentLength) + r;
}
return r;
}
public getViewLineLength(model: ISimpleModel, modelLineNumber: number, outputLineIndex: number): number {
this._assertVisible();
return this._projectionData.getLineLength(outputLineIndex);
}
public getViewLineMinColumn(_model: ITextModel, _modelLineNumber: number, outputLineIndex: number): number {
this._assertVisible();
return this._projectionData.getMinOutputOffset(outputLineIndex) + 1;
}
public getViewLineMaxColumn(model: ISimpleModel, modelLineNumber: number, outputLineIndex: number): number {
this._assertVisible();
return this._projectionData.getMaxOutputOffset(outputLineIndex) + 1;
}
/**
* Try using {@link getViewLinesData} instead.
*/
public getViewLineData(model: ISimpleModel, modelLineNumber: number, outputLineIndex: number): ViewLineData {
const arr = new Array<ViewLineData>();
this.getViewLinesData(model, modelLineNumber, outputLineIndex, 1, 0, [true], arr);
return arr[0];
}
public getViewLinesData(model: ISimpleModel, modelLineNumber: number, outputLineIdx: number, lineCount: number, globalStartIndex: number, needed: boolean[], result: Array<ViewLineData | null>): void {
this._assertVisible();
const lineBreakData = this._projectionData;
const injectionOffsets = lineBreakData.injectionOffsets;
const injectionOptions = lineBreakData.injectionOptions;
let inlineDecorationsPerOutputLine: SingleLineInlineDecoration[][] | null = null;
if (injectionOffsets) {
inlineDecorationsPerOutputLine = [];
let totalInjectedTextLengthBefore = 0;
let currentInjectedOffset = 0;
for (let outputLineIndex = 0; outputLineIndex < lineBreakData.getOutputLineCount(); outputLineIndex++) {
const inlineDecorations = new Array<SingleLineInlineDecoration>();
inlineDecorationsPerOutputLine[outputLineIndex] = inlineDecorations;
const lineStartOffsetInInputWithInjections = outputLineIndex > 0 ? lineBreakData.breakOffsets[outputLineIndex - 1] : 0;
const lineEndOffsetInInputWithInjections = lineBreakData.breakOffsets[outputLineIndex];
while (currentInjectedOffset < injectionOffsets.length) {
const length = injectionOptions![currentInjectedOffset].content.length;
const injectedTextStartOffsetInInputWithInjections = injectionOffsets[currentInjectedOffset] + totalInjectedTextLengthBefore;
const injectedTextEndOffsetInInputWithInjections = injectedTextStartOffsetInInputWithInjections + length;
if (injectedTextStartOffsetInInputWithInjections > lineEndOffsetInInputWithInjections) {
// Injected text only starts in later wrapped lines.
break;
}
if (lineStartOffsetInInputWithInjections < injectedTextEndOffsetInInputWithInjections) {
// Injected text ends after or in this line (but also starts in or before this line).
const options = injectionOptions![currentInjectedOffset];
if (options.inlineClassName) {
const offset = (outputLineIndex > 0 ? lineBreakData.wrappedTextIndentLength : 0);
const start = offset + Math.max(injectedTextStartOffsetInInputWithInjections - lineStartOffsetInInputWithInjections, 0);
const end = offset + Math.min(injectedTextEndOffsetInInputWithInjections - lineStartOffsetInInputWithInjections, lineEndOffsetInInputWithInjections);
if (start !== end) {
inlineDecorations.push(new SingleLineInlineDecoration(start, end, options.inlineClassName, options.inlineClassNameAffectsLetterSpacing!));
}
}
}
if (injectedTextEndOffsetInInputWithInjections <= lineEndOffsetInInputWithInjections) {
totalInjectedTextLengthBefore += length;
currentInjectedOffset++;
} else {
// injected text breaks into next line, process it again
break;
}
}
}
}
let lineWithInjections: LineTokens;
if (injectionOffsets) {
lineWithInjections = model.tokenization.getLineTokens(modelLineNumber).withInserted(injectionOffsets.map((offset, idx) => ({
offset,
text: injectionOptions![idx].content,
tokenMetadata: LineTokens.defaultTokenMetadata
})));
} else {
lineWithInjections = model.tokenization.getLineTokens(modelLineNumber);
}
for (let outputLineIndex = outputLineIdx; outputLineIndex < outputLineIdx + lineCount; outputLineIndex++) {
const globalIndex = globalStartIndex + outputLineIndex - outputLineIdx;
if (!needed[globalIndex]) {
result[globalIndex] = null;
continue;
}
result[globalIndex] = this._getViewLineData(lineWithInjections, inlineDecorationsPerOutputLine ? inlineDecorationsPerOutputLine[outputLineIndex] : null, outputLineIndex);
}
}
private _getViewLineData(lineWithInjections: LineTokens, inlineDecorations: null | SingleLineInlineDecoration[], outputLineIndex: number): ViewLineData {
this._assertVisible();
const lineBreakData = this._projectionData;
const deltaStartIndex = (outputLineIndex > 0 ? lineBreakData.wrappedTextIndentLength : 0);
const lineStartOffsetInInputWithInjections = outputLineIndex > 0 ? lineBreakData.breakOffsets[outputLineIndex - 1] : 0;
const lineEndOffsetInInputWithInjections = lineBreakData.breakOffsets[outputLineIndex];
const tokens = lineWithInjections.sliceAndInflate(lineStartOffsetInInputWithInjections, lineEndOffsetInInputWithInjections, deltaStartIndex);
let lineContent = tokens.getLineContent();
if (outputLineIndex > 0) {
lineContent = spaces(lineBreakData.wrappedTextIndentLength) + lineContent;
}
const minColumn = this._projectionData.getMinOutputOffset(outputLineIndex) + 1;
const maxColumn = lineContent.length + 1;
const continuesWithWrappedLine = (outputLineIndex + 1 < this.getViewLineCount());
const startVisibleColumn = (outputLineIndex === 0 ? 0 : lineBreakData.breakOffsetsVisibleColumn[outputLineIndex - 1]);
return new ViewLineData(
lineContent,
continuesWithWrappedLine,
minColumn,
maxColumn,
startVisibleColumn,
tokens,
inlineDecorations
);
}
public getModelColumnOfViewPosition(outputLineIndex: number, outputColumn: number): number {
this._assertVisible();
return this._projectionData.translateToInputOffset(outputLineIndex, outputColumn - 1) + 1;
}
public getViewPositionOfModelPosition(deltaLineNumber: number, inputColumn: number, affinity: PositionAffinity = PositionAffinity.None): Position {
this._assertVisible();
const r = this._projectionData.translateToOutputPosition(inputColumn - 1, affinity);
return r.toPosition(deltaLineNumber);
}
public getViewLineNumberOfModelPosition(deltaLineNumber: number, inputColumn: number): number {
this._assertVisible();
const r = this._projectionData.translateToOutputPosition(inputColumn - 1);
return deltaLineNumber + r.outputLineIndex;
}
public normalizePosition(outputLineIndex: number, outputPosition: Position, affinity: PositionAffinity): Position {
const baseViewLineNumber = outputPosition.lineNumber - outputLineIndex;
const normalizedOutputPosition = this._projectionData.normalizeOutputPosition(outputLineIndex, outputPosition.column - 1, affinity);
const result = normalizedOutputPosition.toPosition(baseViewLineNumber);
return result;
}
public getInjectedTextAt(outputLineIndex: number, outputColumn: number): InjectedText | null {
return this._projectionData.getInjectedText(outputLineIndex, outputColumn - 1);
}
private _assertVisible() {
if (!this._isVisible) {
throw new Error('Not supported');
}
}
}
/**
* This projection does not change the model line.
*/
class IdentityModelLineProjection implements IModelLineProjection {
public static readonly INSTANCE = new IdentityModelLineProjection();
private constructor() { }
public isVisible(): boolean {
return true;
}
public setVisible(isVisible: boolean): IModelLineProjection {
if (isVisible) {
return this;
}
return HiddenModelLineProjection.INSTANCE;
}
public getProjectionData(): ModelLineProjectionData | null {
return null;
}
public getViewLineCount(): number {
return 1;
}
public getViewLineContent(model: ISimpleModel, modelLineNumber: number, _outputLineIndex: number): string {
return model.getLineContent(modelLineNumber);
}
public getViewLineLength(model: ISimpleModel, modelLineNumber: number, _outputLineIndex: number): number {
return model.getLineLength(modelLineNumber);
}
public getViewLineMinColumn(model: ISimpleModel, modelLineNumber: number, _outputLineIndex: number): number {
return model.getLineMinColumn(modelLineNumber);
}
public getViewLineMaxColumn(model: ISimpleModel, modelLineNumber: number, _outputLineIndex: number): number {
return model.getLineMaxColumn(modelLineNumber);
}
public getViewLineData(model: ISimpleModel, modelLineNumber: number, _outputLineIndex: number): ViewLineData {
const lineTokens = model.tokenization.getLineTokens(modelLineNumber);
const lineContent = lineTokens.getLineContent();
return new ViewLineData(
lineContent,
false,
1,
lineContent.length + 1,
0,
lineTokens.inflate(),
null
);
}
public getViewLinesData(model: ISimpleModel, modelLineNumber: number, _fromOuputLineIndex: number, _toOutputLineIndex: number, globalStartIndex: number, needed: boolean[], result: Array<ViewLineData | null>): void {
if (!needed[globalStartIndex]) {
result[globalStartIndex] = null;
return;
}
result[globalStartIndex] = this.getViewLineData(model, modelLineNumber, 0);
}
public getModelColumnOfViewPosition(_outputLineIndex: number, outputColumn: number): number {
return outputColumn;
}
public getViewPositionOfModelPosition(deltaLineNumber: number, inputColumn: number): Position {
return new Position(deltaLineNumber, inputColumn);
}
public getViewLineNumberOfModelPosition(deltaLineNumber: number, _inputColumn: number): number {
return deltaLineNumber;
}
public normalizePosition(outputLineIndex: number, outputPosition: Position, affinity: PositionAffinity): Position {
return outputPosition;
}
public getInjectedTextAt(_outputLineIndex: number, _outputColumn: number): InjectedText | null {
return null;
}
}
/**
* This projection hides the model line.
*/
class HiddenModelLineProjection implements IModelLineProjection {
public static readonly INSTANCE = new HiddenModelLineProjection();
private constructor() { }
public isVisible(): boolean {
return false;
}
public setVisible(isVisible: boolean): IModelLineProjection {
if (!isVisible) {
return this;
}
return IdentityModelLineProjection.INSTANCE;
}
public getProjectionData(): ModelLineProjectionData | null {
return null;
}
public getViewLineCount(): number {
return 0;
}
public getViewLineContent(_model: ISimpleModel, _modelLineNumber: number, _outputLineIndex: number): string {
throw new Error('Not supported');
}
public getViewLineLength(_model: ISimpleModel, _modelLineNumber: number, _outputLineIndex: number): number {
throw new Error('Not supported');
}
public getViewLineMinColumn(_model: ISimpleModel, _modelLineNumber: number, _outputLineIndex: number): number {
throw new Error('Not supported');
}
public getViewLineMaxColumn(_model: ISimpleModel, _modelLineNumber: number, _outputLineIndex: number): number {
throw new Error('Not supported');
}
public getViewLineData(_model: ISimpleModel, _modelLineNumber: number, _outputLineIndex: number): ViewLineData {
throw new Error('Not supported');
}
public getViewLinesData(_model: ISimpleModel, _modelLineNumber: number, _fromOuputLineIndex: number, _toOutputLineIndex: number, _globalStartIndex: number, _needed: boolean[], _result: ViewLineData[]): void {
throw new Error('Not supported');
}
public getModelColumnOfViewPosition(_outputLineIndex: number, _outputColumn: number): number {
throw new Error('Not supported');
}
public getViewPositionOfModelPosition(_deltaLineNumber: number, _inputColumn: number): Position {
throw new Error('Not supported');
}
public getViewLineNumberOfModelPosition(_deltaLineNumber: number, _inputColumn: number): number {
throw new Error('Not supported');
}
public normalizePosition(outputLineIndex: number, outputPosition: Position, affinity: PositionAffinity): Position {
throw new Error('Not supported');
}
public getInjectedTextAt(_outputLineIndex: number, _outputColumn: number): InjectedText | null {
throw new Error('Not supported');
}
}
let _spaces: string[] = [''];
function spaces(count: number): string {
if (count >= _spaces.length) {
for (let i = 1; i <= count; i++) {
_spaces[i] = _makeSpaces(i);
}
}
return _spaces[count];
}
function _makeSpaces(count: number): string {
return new Array(count + 1).join(' ');
}
|