forked from akvelon/flutter-code-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhidden_line_ranges_builder.dart
70 lines (61 loc) · 2.03 KB
/
hidden_line_ranges_builder.dart
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
import '../code/code_lines.dart';
import 'hidden_line_ranges.dart';
import 'hidden_ranges.dart';
import 'line_numbering_breakpoint.dart';
class HiddenLineRangesBuilder {
final HiddenLineRanges hiddenLineRanges;
factory HiddenLineRangesBuilder({
required CodeLines codeLines,
required HiddenRanges hiddenRanges,
}) {
final breakpoints = <LineNumberingBreakpoint>[];
int spread = 0;
for (final range in hiddenRanges.ranges) {
final startLine = range.firstLine;
final endLine = codeLines.characterIndexToLineIndex(range.end);
final newlines = endLine - startLine;
if (newlines == 0) {
continue;
}
// If there is anything on the start line before the hidden range,
// that content is visible so the line is not affected by the breakpoint,
// and the breakpoint happens on the next line instead.
//
// Example 1 ('abc' is in the first line before the hidden range):
// 0 abcde 0 abcde
// 1 fghXX -> 1 fghij
// 2 XXXXX 2 klmno
// 3 XXXij
// 4 klmno
// maps as 0 -> 0, 1 -> 1, 4 -> 2
// ^- this is the only breakpoint here.
//
// Example 2 (the hidden range starts at the beginning of the line):
// 0 abcde 0 abcde
// 1 XXXXX -> 1 ij
// 2 XXXXX 2 klmno
// 3 XXXij
// 4 klmno
// maps as 0 -> 0, 3 -> 1, 4 -> 2
// ^- this is the only breakpoint here.
final add = range.wholeFirstLine ? 0 : 1;
breakpoints.add(
LineNumberingBreakpoint(
full: endLine + add,
visible: endLine + add - spread - newlines,
spreadBefore: spread,
),
);
spread += newlines;
}
return HiddenLineRangesBuilder._(
hiddenLineRanges: HiddenLineRanges(
breakpoints: breakpoints,
fullLineCount: codeLines.lines.length,
),
);
}
const HiddenLineRangesBuilder._({
required this.hiddenLineRanges,
});
}