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
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TextDocument, FoldingRange, Position, Range, LanguageModes, LanguageMode } from './languageModes';
import { CancellationToken } from 'vscode-languageserver';
export async function getFoldingRanges(languageModes: LanguageModes, document: TextDocument, maxRanges: number | undefined, _cancellationToken: CancellationToken | null): Promise<FoldingRange[]> {
const htmlMode = languageModes.getMode('html');
const range = Range.create(Position.create(0, 0), Position.create(document.lineCount, 0));
let result: FoldingRange[] = [];
if (htmlMode && htmlMode.getFoldingRanges) {
result.push(... await htmlMode.getFoldingRanges(document));
}
// cache folding ranges per mode
const rangesPerMode: { [mode: string]: FoldingRange[] } = Object.create(null);
const getRangesForMode = async (mode: LanguageMode) => {
if (mode.getFoldingRanges) {
let ranges = rangesPerMode[mode.getId()];
if (!Array.isArray(ranges)) {
ranges = await mode.getFoldingRanges(document) || [];
rangesPerMode[mode.getId()] = ranges;
}
return ranges;
}
return [];
};
const modeRanges = languageModes.getModesInRange(document, range);
for (const modeRange of modeRanges) {
const mode = modeRange.mode;
if (mode && mode !== htmlMode && !modeRange.attributeValue) {
const ranges = await getRangesForMode(mode);
result.push(...ranges.filter(r => r.startLine >= modeRange.start.line && r.endLine < modeRange.end.line));
}
}
if (maxRanges && result.length > maxRanges) {
result = limitRanges(result, maxRanges);
}
return result;
}
function limitRanges(ranges: FoldingRange[], maxRanges: number) {
ranges = ranges.sort((r1, r2) => {
let diff = r1.startLine - r2.startLine;
if (diff === 0) {
diff = r1.endLine - r2.endLine;
}
return diff;
});
// compute each range's nesting level in 'nestingLevels'.
// count the number of ranges for each level in 'nestingLevelCounts'
let top: FoldingRange | undefined = undefined;
const previous: FoldingRange[] = [];
const nestingLevels: number[] = [];
const nestingLevelCounts: number[] = [];
const setNestingLevel = (index: number, level: number) => {
nestingLevels[index] = level;
if (level < 30) {
nestingLevelCounts[level] = (nestingLevelCounts[level] || 0) + 1;
}
};
// compute nesting levels and sanitize
for (let i = 0; i < ranges.length; i++) {
const entry = ranges[i];
if (!top) {
top = entry;
setNestingLevel(i, 0);
} else {
if (entry.startLine > top.startLine) {
if (entry.endLine <= top.endLine) {
previous.push(top);
top = entry;
setNestingLevel(i, previous.length);
} else if (entry.startLine > top.endLine) {
do {
top = previous.pop();
} while (top && entry.startLine > top.endLine);
if (top) {
previous.push(top);
}
top = entry;
setNestingLevel(i, previous.length);
}
}
}
}
let entries = 0;
let maxLevel = 0;
for (let i = 0; i < nestingLevelCounts.length; i++) {
const n = nestingLevelCounts[i];
if (n) {
if (n + entries > maxRanges) {
maxLevel = i;
break;
}
entries += n;
}
}
const result = [];
for (let i = 0; i < ranges.length; i++) {
const level = nestingLevels[i];
if (typeof level === 'number') {
if (level < maxLevel || (level === maxLevel && entries++ < maxRanges)) {
result.push(ranges[i]);
}
}
}
return result;
}
|