summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/File.zig29
1 files changed, 9 insertions, 20 deletions
diff --git a/src/File.zig b/src/File.zig
index 1199e2fb..ad76909d 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -611,6 +611,11 @@ pub fn lineIndex(gpa: std.mem.Allocator, f: *State) ![]const usize {
return starts;
}
+/// `std.sort`'s order for offsets: a key against an item.
+fn ascending(key: usize, item: usize) std.math.Order {
+ return std.math.order(key, item);
+}
+
/// The line index of `new` from `old`'s: an edit replaces one span, so
/// line starts before it stand, starts after it shift, and only the span
/// is scanned. Rebuilding scanned the whole file on every keystroke.
@@ -623,17 +628,9 @@ fn editedLineStarts(gpa: std.mem.Allocator, starts: []const usize, old: []const
const tail = span.tail;
// A start s follows the newline at s-1: kept while that newline is in
// the common head, shifted while it is in the common tail.
- const kept = std.sort.upperBound(usize, starts, head, struct {
- fn order(key: usize, item: usize) std.math.Order {
- return std.math.order(key, item);
- }
- }.order);
+ const kept = std.sort.upperBound(usize, starts, head, ascending);
const old_tail_at = old.len - tail;
- const moved = starts.len - std.sort.upperBound(usize, starts, old_tail_at, struct {
- fn order(key: usize, item: usize) std.math.Order {
- return std.math.order(key, item);
- }
- }.order);
+ const moved = starts.len - std.sort.upperBound(usize, starts, old_tail_at, ascending);
const middle = new[head .. new.len - tail];
const out = try gpa.alloc(usize, kept + std.mem.count(u8, middle, "\n") + moved);
@memcpy(out[0..kept], starts[0..kept]);
@@ -773,11 +770,7 @@ pub fn textLineCount(t: *Text, text: []const u8) usize {
pub fn textPosition(t: *Text, text: []const u8, offset: usize) modal.Cursor {
const index = contentIndex(t, text) orelse return modal.positionAt(text, offset);
const bounded = @min(offset, text.len);
- const row = std.sort.upperBound(usize, index, bounded, struct {
- fn cmp(key: usize, item: usize) std.math.Order {
- return std.math.order(key, item);
- }
- }.cmp) - 1;
+ const row = std.sort.upperBound(usize, index, bounded, ascending) - 1;
const start = index[row];
const end = if (row + 1 < index.len) index[row + 1] - 1 else text.len;
return .{ .row = row, .col = modal.graphemeStart(text[start..end], @min(bounded - start, end - start)) };
@@ -1062,11 +1055,7 @@ fn positionIn(gpa: std.mem.Allocator, f: *State, at: usize) struct { row: usize,
const c = modal.positionAt(f.content, at);
return .{ .row = c.row, .col = c.col };
};
- const row = std.sort.upperBound(usize, index, at, struct {
- fn order(key: usize, item: usize) std.math.Order {
- return std.math.order(key, item);
- }
- }.order) -| 1;
+ const row = std.sort.upperBound(usize, index, at, ascending) -| 1;
return .{ .row = row, .col = at - index[row] };
}