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
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
export interface ILineRange {
startLineNumber: number;
endLineNumber: number;
}
export interface FoldRange {
startLineNumber: number;
endLineNumber: number;
type: string | undefined;
isCollapsed: boolean;
isUserDefined: boolean;
isRecovered: boolean;
}
export const MAX_FOLDING_REGIONS = 0xFFFF;
export const MAX_LINE_NUMBER = 0xFFFFFF;
const MASK_INDENT = 0xFF000000;
class BitField {
private readonly _states: Uint32Array;
constructor(size: number) {
const numWords = Math.ceil(size / 32);
this._states = new Uint32Array(numWords);
}
public get(index: number): boolean {
const arrayIndex = (index / 32) | 0;
const bit = index % 32;
return (this._states[arrayIndex] & (1 << bit)) !== 0;
}
public set(index: number, newState: boolean) {
const arrayIndex = (index / 32) | 0;
const bit = index % 32;
const value = this._states[arrayIndex];
if (newState) {
this._states[arrayIndex] = value | (1 << bit);
} else {
this._states[arrayIndex] = value & ~(1 << bit);
}
}
}
export class FoldingRegions {
private readonly _startIndexes: Uint32Array;
private readonly _endIndexes: Uint32Array;
private readonly _collapseStates: BitField;
private readonly _userDefinedStates: BitField;
private readonly _recoveredStates: BitField;
private _parentsComputed: boolean;
private readonly _types: Array<string | undefined> | undefined;
constructor(startIndexes: Uint32Array, endIndexes: Uint32Array, types?: Array<string | undefined>) {
if (startIndexes.length !== endIndexes.length || startIndexes.length > MAX_FOLDING_REGIONS) {
throw new Error('invalid startIndexes or endIndexes size');
}
this._startIndexes = startIndexes;
this._endIndexes = endIndexes;
this._collapseStates = new BitField(startIndexes.length);
this._userDefinedStates = new BitField(startIndexes.length);
this._recoveredStates = new BitField(startIndexes.length);
this._types = types;
this._parentsComputed = false;
}
private ensureParentIndices() {
if (!this._parentsComputed) {
this._parentsComputed = true;
const parentIndexes: number[] = [];
const isInsideLast = (startLineNumber: number, endLineNumber: number) => {
const index = parentIndexes[parentIndexes.length - 1];
return this.getStartLineNumber(index) <= startLineNumber && this.getEndLineNumber(index) >= endLineNumber;
};
for (let i = 0, len = this._startIndexes.length; i < len; i++) {
const startLineNumber = this._startIndexes[i];
const endLineNumber = this._endIndexes[i];
if (startLineNumber > MAX_LINE_NUMBER || endLineNumber > MAX_LINE_NUMBER) {
throw new Error('startLineNumber or endLineNumber must not exceed ' + MAX_LINE_NUMBER);
}
while (parentIndexes.length > 0 && !isInsideLast(startLineNumber, endLineNumber)) {
parentIndexes.pop();
}
const parentIndex = parentIndexes.length > 0 ? parentIndexes[parentIndexes.length - 1] : -1;
parentIndexes.push(i);
this._startIndexes[i] = startLineNumber + ((parentIndex & 0xFF) << 24);
this._endIndexes[i] = endLineNumber + ((parentIndex & 0xFF00) << 16);
}
}
}
public get length(): number {
return this._startIndexes.length;
}
public getStartLineNumber(index: number): number {
return this._startIndexes[index] & MAX_LINE_NUMBER;
}
public getEndLineNumber(index: number): number {
return this._endIndexes[index] & MAX_LINE_NUMBER;
}
public getType(index: number): string | undefined {
return this._types ? this._types[index] : undefined;
}
public hasTypes() {
return !!this._types;
}
public isCollapsed(index: number): boolean {
return this._collapseStates.get(index);
}
public setCollapsed(index: number, newState: boolean) {
this._collapseStates.set(index, newState);
}
public isUserDefined(index: number): boolean {
return this._userDefinedStates.get(index);
}
public setUserDefined(index: number, newState: boolean) {
return this._userDefinedStates.set(index, newState);
}
public isRecovered(index: number): boolean {
return this._recoveredStates.get(index);
}
public setRecovered(index: number, newState: boolean) {
return this._recoveredStates.set(index, newState);
}
public setCollapsedAllOfType(type: string, newState: boolean) {
let hasChanged = false;
if (this._types) {
for (let i = 0; i < this._types.length; i++) {
if (this._types[i] === type) {
this.setCollapsed(i, newState);
hasChanged = true;
}
}
}
return hasChanged;
}
public toRegion(index: number): FoldingRegion {
return new FoldingRegion(this, index);
}
public getParentIndex(index: number) {
this.ensureParentIndices();
const parent = ((this._startIndexes[index] & MASK_INDENT) >>> 24) + ((this._endIndexes[index] & MASK_INDENT) >>> 16);
if (parent === MAX_FOLDING_REGIONS) {
return -1;
}
return parent;
}
public contains(index: number, line: number) {
return this.getStartLineNumber(index) <= line && this.getEndLineNumber(index) >= line;
}
private findIndex(line: number) {
let low = 0, high = this._startIndexes.length;
if (high === 0) {
return -1; // no children
}
while (low < high) {
const mid = Math.floor((low + high) / 2);
if (line < this.getStartLineNumber(mid)) {
high = mid;
} else {
low = mid + 1;
}
}
return low - 1;
}
public findRange(line: number): number {
let index = this.findIndex(line);
if (index >= 0) {
const endLineNumber = this.getEndLineNumber(index);
if (endLineNumber >= line) {
return index;
}
index = this.getParentIndex(index);
while (index !== -1) {
if (this.contains(index, line)) {
return index;
}
index = this.getParentIndex(index);
}
}
return -1;
}
public toString() {
const res: string[] = [];
for (let i = 0; i < this.length; i++) {
res[i] = `[${this.isUserDefined(i) ? '*' : ' '}${this.isRecovered(i) ? 'r' : ' '}${this.isCollapsed(i) ? '+' : '-'}] ${this.getStartLineNumber(i)}/${this.getEndLineNumber(i)}`;
}
return res.join(', ');
}
public toFoldRange(index: number): FoldRange {
return <FoldRange>{
startLineNumber: this._startIndexes[index] & MAX_LINE_NUMBER,
endLineNumber: this._endIndexes[index] & MAX_LINE_NUMBER,
type: this._types ? this._types[index] : undefined,
isCollapsed: this.isCollapsed(index),
isUserDefined: this.isUserDefined(index),
isRecovered: this.isRecovered(index)
};
}
public static fromFoldRanges(ranges: FoldRange[]): FoldingRegions {
const rangesLength = ranges.length;
const startIndexes = new Uint32Array(rangesLength);
const endIndexes = new Uint32Array(rangesLength);
let types: Array<string | undefined> | undefined = [];
let gotTypes = false;
for (let i = 0; i < rangesLength; i++) {
const range = ranges[i];
startIndexes[i] = range.startLineNumber;
endIndexes[i] = range.endLineNumber;
types.push(range.type);
if (range.type) {
gotTypes = true;
}
}
if (!gotTypes) {
types = undefined;
}
const regions = new FoldingRegions(startIndexes, endIndexes, types);
for (let i = 0; i < rangesLength; i++) {
if (ranges[i].isCollapsed) {
regions.setCollapsed(i, true);
}
if (ranges[i].isUserDefined) {
regions.setUserDefined(i, true);
}
if (ranges[i].isRecovered) {
regions.setRecovered(i, true);
}
}
return regions;
}
/**
* Two inputs, each a FoldingRegions or a FoldRange[], are merged.
* Each input must be pre-sorted on startLineNumber.
* The first list is assumed to always include all regions currently defined by range providers.
* The second list only contains the previously collapsed and all manual ranges.
* If the line position matches, the range of the new range is taken, and the range is no longer manual
* When an entry in one list overlaps an entry in the other, the second list's entry "wins" and
* overlapping entries in the first list are discarded.
* Invalid entries are discarded. An entry is invalid if:
* the start and end line numbers aren't a valid range of line numbers,
* it is out of sequence or has the same start line as a preceding entry,
* it overlaps a preceding entry and is not fully contained by that entry.
*/
public static sanitizeAndMerge(
rangesA: FoldingRegions | FoldRange[],
rangesB: FoldingRegions | FoldRange[],
maxLineNumber: number | undefined): FoldRange[] {
maxLineNumber = maxLineNumber ?? Number.MAX_VALUE;
const getIndexedFunction = (r: FoldingRegions | FoldRange[], limit: number) => {
return Array.isArray(r)
? ((i: number) => { return (i < limit) ? r[i] : undefined; })
: ((i: number) => { return (i < limit) ? r.toFoldRange(i) : undefined; });
};
const getA = getIndexedFunction(rangesA, rangesA.length);
const getB = getIndexedFunction(rangesB, rangesB.length);
let indexA = 0;
let indexB = 0;
let nextA = getA(0);
let nextB = getB(0);
const stackedRanges: FoldRange[] = [];
let topStackedRange: FoldRange | undefined;
let prevLineNumber = 0;
const resultRanges: FoldRange[] = [];
while (nextA || nextB) {
let useRange: FoldRange | undefined = undefined;
if (nextB && (!nextA || nextA.startLineNumber >= nextB.startLineNumber)) {
if (nextA && nextA.startLineNumber === nextB.startLineNumber) {
if (nextB.isUserDefined) {
// a user defined range (possibly unfolded)
useRange = nextB;
} else {
// a previously folded range or a (possibly unfolded) recovered range
useRange = nextA;
useRange.isCollapsed = nextB.isCollapsed && nextA.endLineNumber === nextB.endLineNumber;
useRange.isUserDefined = false;
useRange.isRecovered = false;
}
nextA = getA(++indexA); // not necessary, just for speed
} else {
useRange = nextB;
if (nextB.isCollapsed && !nextB.isUserDefined && !nextB.isRecovered) {
// a previously collapsed range
useRange.isRecovered = true;
useRange.isUserDefined = false;
}
}
nextB = getB(++indexB);
} else {
// nextA is next. The user folded B set takes precedence and we sometimes need to look
// ahead in it to check for an upcoming conflict.
let scanIndex = indexB;
let prescanB = nextB;
while (true) {
if (!prescanB || prescanB.startLineNumber > nextA!.endLineNumber) {
useRange = nextA;
break; // no conflict, use this nextA
}
if (prescanB.isUserDefined && prescanB.endLineNumber > nextA!.endLineNumber) {
// we found a user folded range, it wins
break; // without setting nextResult, so this nextA gets skipped
}
prescanB = getB(++scanIndex);
}
nextA = getA(++indexA);
}
if (useRange) {
while (topStackedRange
&& topStackedRange.endLineNumber < useRange.startLineNumber) {
topStackedRange = stackedRanges.pop();
}
if (useRange.endLineNumber > useRange.startLineNumber
&& useRange.startLineNumber > prevLineNumber
&& useRange.endLineNumber <= maxLineNumber
&& (!topStackedRange
|| topStackedRange.endLineNumber >= useRange.endLineNumber)) {
resultRanges.push(useRange);
prevLineNumber = useRange.startLineNumber;
if (topStackedRange) {
stackedRanges.push(topStackedRange);
}
topStackedRange = useRange;
}
}
}
return resultRanges;
}
}
export class FoldingRegion {
constructor(private readonly ranges: FoldingRegions, private index: number) {
}
public get startLineNumber() {
return this.ranges.getStartLineNumber(this.index);
}
public get endLineNumber() {
return this.ranges.getEndLineNumber(this.index);
}
public get regionIndex() {
return this.index;
}
public get parentIndex() {
return this.ranges.getParentIndex(this.index);
}
public get isCollapsed() {
return this.ranges.isCollapsed(this.index);
}
containedBy(range: ILineRange): boolean {
return range.startLineNumber <= this.startLineNumber && range.endLineNumber >= this.endLineNumber;
}
containsLine(lineNumber: number) {
return this.startLineNumber <= lineNumber && lineNumber <= this.endLineNumber;
}
hidesLine(lineNumber: number) {
return this.startLineNumber < lineNumber && lineNumber <= this.endLineNumber;
}
}
|