Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HiddenLineRanges.cutLineIndexIfVisible (#112) #114

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions lib/src/code_field/code_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,6 @@ class CodeController extends TextEditingController {
super.value = _getValueWithCode(_code);
}



/// The value with [newCode] preserving the current selection.
TextEditingValue _getValueWithCode(Code newCode) {
return TextEditingValue(
Expand Down
37 changes: 22 additions & 15 deletions lib/src/hidden_ranges/hidden_line_ranges.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ import 'line_numbering_breakpoint.dart';
class HiddenLineRanges with EquatableMixin {
final List<LineNumberingBreakpoint> breakpoints;
final int fullLineCount;
final int visibleLineCount;

const HiddenLineRanges({
required this.breakpoints,
required this.fullLineCount,
required this.visibleLineCount,
});

static const empty = HiddenLineRanges(
breakpoints: [],
fullLineCount: 1,
visibleLineCount: 1,
);

/// Returns the visible line index to which the full [lineIndex] maps
Expand All @@ -34,6 +31,14 @@ class HiddenLineRanges with EquatableMixin {
if (breakpoints.isEmpty) {
return lineIndex;
}
if (lineIndex <= breakpoints.first.full) {
return breakpoints.first.cutLineIndexIfVisible(lineIndex);
}
if (lineIndex >= breakpoints.last.full) {
return breakpoints.last.cutLineIndexIfVisible(lineIndex);
}

// At this point, we always have a breakpoint before and after.

int lower = 0;
int upper = breakpoints.length - 1;
Expand All @@ -49,30 +54,32 @@ class HiddenLineRanges with EquatableMixin {
(upper - lower))
.floor();

if (index < lower) {
return breakpoints[lower].cutLineIndexIfVisible(lineIndex);
}
if (index > upper) {
return breakpoints[upper].cutLineIndexIfVisible(lineIndex);
}

final breakpoint = breakpoints[index];

switch ((breakpoint.full - lineIndex).sign) {
case -1:
case -1: // We are after this breakpoint.
final nextBreakpoint = breakpoints[index + 1];
if (nextBreakpoint.full > lineIndex) {
// ... and before the next one.
return nextBreakpoint.cutLineIndexIfVisible(lineIndex);
}
lower = index + 1;
continue;
case 1:

case 1: // We are before this breakpoint.
final previousBreakpoint = breakpoints[index - 1];
if (previousBreakpoint.full < lineIndex) {
// ... and after the previous one.
return breakpoint.cutLineIndexIfVisible(lineIndex);
}
upper = index - 1;
continue;
}

return breakpoint.visible;
}

// upper == lower
final breakpoint = breakpoints[upper];
return breakpoint.cutLineIndexIfVisible(lineIndex);
throw Exception('Never get here');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be message of this exception should be more specific

}

Iterable<int> get visibleLineNumbers sync* {
Expand Down
1 change: 0 additions & 1 deletion lib/src/hidden_ranges/hidden_line_ranges_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class HiddenLineRangesBuilder {
hiddenLineRanges: HiddenLineRanges(
breakpoints: breakpoints,
fullLineCount: codeLines.lines.length,
visibleLineCount: codeLines.lines.length - spread,
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/hidden_ranges/hidden_ranges.dart
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class HiddenRanges {
}
}

/// Translates the [selection] of the full text
/// Translates the [selection] of the full text
/// to selection of the visible text.
TextSelection cutSelection(TextSelection selection) {
if (selection.isCollapsed) {
Expand Down
5 changes: 0 additions & 5 deletions test/src/hidden_ranges/hidden_line_ranges_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ void main() {
const HiddenLineRanges(
breakpoints: [],
fullLineCount: 1,
visibleLineCount: 1,
),
);
});
Expand All @@ -45,7 +44,6 @@ void main() {
const HiddenLineRanges(
breakpoints: [],
fullLineCount: 15,
visibleLineCount: 15,
),
);
});
Expand Down Expand Up @@ -86,7 +84,6 @@ void main() {
const HiddenLineRanges(
breakpoints: [],
fullLineCount: 15,
visibleLineCount: 15,
),
);
});
Expand Down Expand Up @@ -130,7 +127,6 @@ void main() {
LineNumberingBreakpoint(full: 14, visible: 10, spreadBefore: 2),
],
fullLineCount: 15,
visibleLineCount: 11,
),
);
});
Expand Down Expand Up @@ -174,7 +170,6 @@ void main() {
LineNumberingBreakpoint(full: 15, visible: 10, spreadBefore: 2),
],
fullLineCount: 15,
visibleLineCount: 9,
),
);
});
Expand Down
16 changes: 11 additions & 5 deletions test/src/hidden_ranges/hidden_line_ranges_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ void main() {
const noBreakpointsRanges = HiddenLineRanges(
breakpoints: [],
fullLineCount: 10,
visibleLineCount: 10,
);

const midBreakpointsRanges = HiddenLineRanges(
breakpoints: [
LineNumberingBreakpoint(full: 4, visible: 2, spreadBefore: 0),
LineNumberingBreakpoint(full: 9, visible: 5, spreadBefore: 2),
LineNumberingBreakpoint(full: 100, visible: 12, spreadBefore: 4),
LineNumberingBreakpoint(full: 210, visible: 113, spreadBefore: 88),
LineNumberingBreakpoint(full: 220, visible: 115, spreadBefore: 97),
LineNumberingBreakpoint(full: 230, visible: 118, spreadBefore: 105),
],
fullLineCount: 110,
visibleLineCount: 22,
fullLineCount: 231,
);

const startEndHiddenRanges = HiddenLineRanges(
Expand All @@ -25,7 +26,6 @@ void main() {
LineNumberingBreakpoint(full: 10, visible: 5, spreadBefore: 3),
],
fullLineCount: 10,
visibleLineCount: 5,
);

group('HiddenLineRanges.', () {
Expand Down Expand Up @@ -56,6 +56,9 @@ void main() {
expect(midBreakpointsRanges.cutLineIndexIfVisible(100), 12);
expect(midBreakpointsRanges.cutLineIndexIfVisible(101), 13);
expect(midBreakpointsRanges.cutLineIndexIfVisible(200), 112);
expect(midBreakpointsRanges.cutLineIndexIfVisible(209), null);
expect(midBreakpointsRanges.cutLineIndexIfVisible(219), null);
expect(midBreakpointsRanges.cutLineIndexIfVisible(229), null);
});
});

Expand All @@ -75,7 +78,10 @@ void main() {
0, 1,
4, 5, 6,
9, 10, 11, 12, 13, 14, 15,
100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
...List<int>.generate(200 - 100 + 1, (i) => i + 100), // 100-200
210, 211,
220, 221, 222,
230,
],
);
});
Expand Down