Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/test/common/viewModel/lineBreakData.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
8
import { PositionAffinity } from '../../../common/model.js';
9
import { ModelDecorationInjectedTextOptions } from '../../../common/model/textModel.js';
10
import { ModelLineProjectionData } from '../../../common/modelLineProjectionData.js';
11
12
suite('Editor ViewModel - LineBreakData', () => {
13
14
ensureNoDisposablesAreLeakedInTestSuite();
15
16
test('Basic', () => {
17
const data = new ModelLineProjectionData([], [], [100], [0], 10);
18
19
assert.strictEqual(data.translateToInputOffset(0, 50), 50);
20
assert.strictEqual(data.translateToInputOffset(1, 60), 150);
21
});
22
23
function sequence(length: number, start = 0): number[] {
24
const result = new Array<number>();
25
for (let i = 0; i < length; i++) {
26
result.push(i + start);
27
}
28
return result;
29
}
30
31
function testInverse(data: ModelLineProjectionData) {
32
for (let i = 0; i < 100; i++) {
33
const output = data.translateToOutputPosition(i);
34
assert.deepStrictEqual(data.translateToInputOffset(output.outputLineIndex, output.outputOffset), i);
35
}
36
}
37
38
function getInputOffsets(data: ModelLineProjectionData, outputLineIdx: number): number[] {
39
return sequence(20).map(i => data.translateToInputOffset(outputLineIdx, i));
40
}
41
42
function getOutputOffsets(data: ModelLineProjectionData, affinity: PositionAffinity): string[] {
43
return sequence(25).map(i => data.translateToOutputPosition(i, affinity).toString());
44
}
45
46
function mapTextToInjectedTextOptions(arr: string[]): ModelDecorationInjectedTextOptions[] {
47
return arr.map(e => ModelDecorationInjectedTextOptions.from({ content: e }));
48
}
49
50
suite('Injected Text 1', () => {
51
const data = new ModelLineProjectionData([2, 3, 10], mapTextToInjectedTextOptions(['1', '22', '333']), [10, 100], [], 10);
52
53
test('getInputOffsetOfOutputPosition', () => {
54
// For every view model position, what is the model position?
55
assert.deepStrictEqual(getInputOffsets(data, 0), ([0, 1, 2, 2, 3, 3, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, 12, 13]));
56
assert.deepStrictEqual(getInputOffsets(data, 1), ([7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 9, 10, 10, 10, 10, 11, 12, 13]));
57
});
58
59
test('getOutputPositionOfInputOffset', () => {
60
data.translateToOutputPosition(20);
61
assert.deepStrictEqual(getOutputOffsets(data, PositionAffinity.None), [
62
'0:0',
63
'0:1',
64
'0:2',
65
'0:4',
66
'0:7',
67
'0:8',
68
'0:9',
69
'1:10',
70
'1:11',
71
'1:12',
72
'1:13',
73
'1:17',
74
'1:18',
75
'1:19',
76
'1:20',
77
'1:21',
78
'1:22',
79
'1:23',
80
'1:24',
81
'1:25',
82
'1:26',
83
'1:27',
84
'1:28',
85
'1:29',
86
'1:30',
87
]);
88
89
assert.deepStrictEqual(getOutputOffsets(data, PositionAffinity.Left), [
90
'0:0',
91
'0:1',
92
'0:2',
93
'0:4',
94
'0:7',
95
'0:8',
96
'0:9',
97
'0:10',
98
'1:11',
99
'1:12',
100
'1:13',
101
'1:17',
102
'1:18',
103
'1:19',
104
'1:20',
105
'1:21',
106
'1:22',
107
'1:23',
108
'1:24',
109
'1:25',
110
'1:26',
111
'1:27',
112
'1:28',
113
'1:29',
114
'1:30',
115
]);
116
117
assert.deepStrictEqual(getOutputOffsets(data, PositionAffinity.Right), [
118
'0:0',
119
'0:1',
120
'0:3',
121
'0:6',
122
'0:7',
123
'0:8',
124
'0:9',
125
'1:10',
126
'1:11',
127
'1:12',
128
'1:16',
129
'1:17',
130
'1:18',
131
'1:19',
132
'1:20',
133
'1:21',
134
'1:22',
135
'1:23',
136
'1:24',
137
'1:25',
138
'1:26',
139
'1:27',
140
'1:28',
141
'1:29',
142
'1:30',
143
]);
144
});
145
146
test('getInputOffsetOfOutputPosition is inverse of getOutputPositionOfInputOffset', () => {
147
testInverse(data);
148
});
149
150
151
test('normalization', () => {
152
assert.deepStrictEqual(
153
sequence(25)
154
.map((v) =>
155
data.normalizeOutputPosition(1, v, PositionAffinity.Right)
156
)
157
.map((s) => s.toString()),
158
[
159
'1:0',
160
'1:1',
161
'1:2',
162
'1:3',
163
'1:4',
164
'1:5',
165
'1:6',
166
'1:7',
167
'1:8',
168
'1:9',
169
'1:10',
170
'1:11',
171
'1:12',
172
'1:16',
173
'1:16',
174
'1:16',
175
'1:16',
176
'1:17',
177
'1:18',
178
'1:19',
179
'1:20',
180
'1:21',
181
'1:22',
182
'1:23',
183
'1:24',
184
]
185
);
186
});
187
});
188
189
suite('Injected Text 2', () => {
190
const data = new ModelLineProjectionData([2, 2, 6], mapTextToInjectedTextOptions(['1', '22', '333']), [10, 100], [], 0);
191
192
test('getInputOffsetOfOutputPosition', () => {
193
assert.deepStrictEqual(
194
getInputOffsets(data, 0),
195
[0, 1, 2, 2, 2, 2, 3, 4, 5, 6, 6, 6, 6, 7, 8, 9, 10, 11, 12, 13]
196
);
197
assert.deepStrictEqual(
198
getInputOffsets(data, 1),
199
[
200
6, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
201
23,
202
]
203
);
204
});
205
206
test('getInputOffsetOfOutputPosition is inverse of getOutputPositionOfInputOffset', () => {
207
testInverse(data);
208
});
209
});
210
211
suite('Injected Text 3', () => {
212
const data = new ModelLineProjectionData([2, 2, 7], mapTextToInjectedTextOptions(['1', '22', '333']), [10, 100], [], 0);
213
214
test('getInputOffsetOfOutputPosition', () => {
215
assert.deepStrictEqual(
216
getInputOffsets(data, 0),
217
[0, 1, 2, 2, 2, 2, 3, 4, 5, 6, 7, 7, 7, 7, 8, 9, 10, 11, 12, 13]
218
);
219
assert.deepStrictEqual(
220
getInputOffsets(data, 1),
221
[
222
7, 7, 7, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
223
23,
224
]
225
);
226
});
227
228
test('getInputOffsetOfOutputPosition is inverse of getOutputPositionOfInputOffset', () => {
229
testInverse(data);
230
});
231
});
232
});
233
234