summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-04 23:50:44 -0300
committerGabriel Schneider <[email protected]>2026-11-05 01:16:28 -0300
commit4523ad87cae6b31e1db67043ec5b3ba9a995b6ea (patch)
tree0149dc84ffd448dcab2bc209cd08ef8f803dfa07 /src
parent6cddaba55b83e6c164cdd04eb7972590bf195e34 (diff)
downloadpardes-4523ad87cae6b31e1db67043ec5b3ba9a995b6ea.tar.gz
pardes-4523ad87cae6b31e1db67043ec5b3ba9a995b6ea.zip
edit: LineAt, lineCountOf, rowOf, lineAt and indexedLine, copies of File's indexed text functions, go: typing, backspace, J and editTextEol ask File.textLine, textLineStart, textLineCount and textPosition
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src')
-rw-r--r--src/edit.zig78
1 files changed, 20 insertions, 58 deletions
diff --git a/src/edit.zig b/src/edit.zig
index f6722499..a2faec39 100644
--- a/src/edit.zig
+++ b/src/edit.zig
@@ -475,9 +475,8 @@ fn editTextEol(p: *Pardes, t: *Text, b: Bounds) ?panes.EditText {
const eb = editText(p, t, b.lo_row, b.hi_row, -1) orelse return null;
const r: usize = @intCast(@max(0, b.hi_row - eb.row0));
// A file's own line count and line, from its index.
- const lines = if (indexedLine(p, t, eb.text, 0) != null) panes.File.nlines(p.gpa, &t.pane().?.file.?) else modal.lineCount(eb.text);
- if (r + 1 < lines) return eb;
- if (b.hi_col < @as(i32, @intCast(lineAt(p, t, eb.text, r).line.len))) return eb;
+ if (r + 1 < panes.File.textLineCount(t, eb.text)) return eb;
+ if (b.hi_col < @as(i32, @intCast(panes.File.textLine(t, eb.text, r).len))) return eb;
return editText(p, t, b.lo_row, b.hi_row + 1, -1);
}
@@ -486,47 +485,6 @@ pub fn setEditText(p: *Pardes, t: *Text, new: []u8) void {
setEditTextEnd(p, t, new, null);
}
-const LineAt = struct { start: usize, line: []const u8 };
-
-/// `text`'s line count: the file's index when `text` is the pane's file.
-fn lineCountOf(p: *Pardes, t: *Text, text: []const u8) usize {
- if (indexedLine(p, t, text, 0) != null) return panes.File.nlines(p.gpa, &t.pane().?.file.?);
- return modal.cursorLineCount(text);
-}
-
-/// The row offset `off` is on: a search of the file's index when `text` is
-/// the pane's file, else the newlines before it counted (modal.lineAtOffset).
-fn rowOf(p: *Pardes, t: *Text, text: []const u8, off: usize) usize {
- if (indexedLine(p, t, text, 0) != null) {
- const index = panes.File.lineIndex(p.gpa, &t.pane().?.file.?) catch return modal.lineAtOffset(text, off);
- return std.sort.upperBound(usize, index, @min(off, text.len), struct {
- fn order(key: usize, item: usize) std.math.Order {
- return std.math.order(key, item);
- }
- }.order) -| 1;
- }
- return modal.lineAtOffset(text, off);
-}
-
-/// Line `row` of `text` and where it starts: through the file's line index
-/// when `text` is the pane's file (a lookup), else counted from the top.
-/// Finding line 94000 by counting was most of a keystroke in a 7 MB file.
-fn lineAt(p: *Pardes, t: *Text, text: []const u8, row: usize) LineAt {
- return indexedLine(p, t, text, row) orelse .{ .start = modal.lineStartOffset(text, row), .line = modal.lineSlice(text, row) };
-}
-
-/// lineAt's lookup, null when `text` is not the pane's file or `row` is past its last line.
-fn indexedLine(p: *Pardes, t: *Text, text: []const u8, row: usize) ?LineAt {
- if (t.what != .body) return null;
- const pane = t.pane() orelse return null;
- const f = if (pane.file) |*f| f else return null;
- if (f.content.ptr != text.ptr or f.content.len != text.len) return null;
- if (row >= panes.File.nlines(p.gpa, f)) return null;
- const start = @min(panes.File.lineStart(p.gpa, f, row), text.len);
- const end = std.mem.indexOfScalarPos(u8, text, start, '\n') orelse text.len;
- return .{ .start = start, .line = text[start..end] };
-}
-
/// `setEditText` with where the edit ended in `new` (in the edit text's
/// rows) when the caller knows it, as typing does: then nothing scans the
/// whole text to find it. Null, it is found by comparing the texts.
@@ -884,8 +842,13 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) void {
const text = eb.text;
const c = modal.Cursor{ .row = @intCast(@max(0, t.cur_row - eb.row0)), .col = @intCast(@max(0, t.cur_col)) };
if (key.text.len > 0 and !key.ctrl and !key.alt) {
- // insertAt's offset (col clamped to the line), the line found once.
- const at = if (indexedLine(p, t, text, c.row)) |l| l.start + @min(c.col, l.line.len) else modal.insertOffset(text, c);
+ // insertAt's offset (col clamped to the line), through the file's
+ // line index when `text` is the file's: finding line 94000 by
+ // counting was most of a keystroke in a 7 MB file.
+ const at = if (c.row < panes.File.textLineCount(t, text))
+ panes.File.textLineStart(t, text, c.row) + @min(c.col, panes.File.textLine(t, text, c.row).len)
+ else
+ modal.insertOffset(text, c);
const new = modal.spliceAlloc(p.gpa, text, at, at, key.text) catch return;
const end = modal.advanceBy(c, key.text);
setEditTextSpan(p, t, new, end, .{ .head = at, .tail = text.len - at });
@@ -986,8 +949,8 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) void {
},
Key.backspace => {
if (t.cur_col > 0) {
- const found = lineAt(p, t, text, c.row);
- const line = found.line;
+ const start = panes.File.textLineStart(t, text, c.row);
+ const line = panes.File.textLine(t, text, c.row);
var prev = modal.prevGrapheme(line, c.col);
// in leading blanks, back to the previous indent stop (helix
// dedent): a whole unit when on one, else the part past it
@@ -1001,8 +964,8 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) void {
while (drop > 0 and prev > 0 and line[prev - 1] == ' ') : (drop -= 1) prev -= 1;
}
// deleteSpan's splice, the line found once.
- const s = found.start + @min(prev, line.len);
- const e = found.start + @min(c.col, line.len);
+ const s = start + @min(prev, line.len);
+ const e = start + @min(c.col, line.len);
const new = if (e > s) modal.spliceAlloc(p.gpa, text, s, e, "") catch return else p.gpa.dupe(u8, text) catch return;
const span: panes.File.Span = if (e > s) .{ .head = s, .tail = text.len - e } else .{ .head = text.len, .tail = 0 };
setEditTextSpan(p, t, new, .{ .row = c.row, .col = prev }, span);
@@ -1530,25 +1493,24 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void {
const got = t.ranges(text, eb.row0, &rs);
// Lines, their count and rows from the file's index when it has one:
// counted from the top, a J in a 7 MB file was 8 ms.
- const nlines = lineCountOf(p, t, text);
+ const nlines = panes.File.textLineCount(t, text);
const arena = p.scratch.allocator();
var changes: std.ArrayList(sam.Op) = .empty;
var next: usize = 0; // lines below this one are joined already
for (rs[0..got.n]) |r| {
- // Text.rangeLineSpan, through rowOf
+ // Text.rangeLineSpan, through the file's index
const lo = @min(r.anchor, r.head);
const hi = @max(r.anchor, r.head);
- const span = .{ .start = rowOf(p, t, text, lo), .end = rowOf(p, t, text, if (lo == hi) hi else @max(modal.prevGrapheme(text, hi), lo)) };
+ const span = .{ .start = panes.File.textPosition(t, text, lo).row, .end = panes.File.textPosition(t, text, if (lo == hi) hi else @max(modal.prevGrapheme(text, hi), lo)).row };
var end = span.end;
if (span.start == end) end = @min(end + 1, nlines - 1);
var l = @max(span.start, next);
while (l < end) : (l += 1) {
- const here = lineAt(p, t, text, l);
- const from = here.start + here.line.len;
- var to = if (l + 1 >= nlines) text.len else lineAt(p, t, text, l + 1).start;
+ const from = panes.File.textLineStart(t, text, l) + panes.File.textLine(t, text, l).len;
+ var to = if (l + 1 >= nlines) text.len else panes.File.textLineStart(t, text, l + 1);
while (to < text.len and (text[to] == ' ' or text[to] == '\t')) to += 1;
- const last = lineAt(p, t, text, @min(l + 1, nlines - 1));
- const sep: []const u8 = if (to == last.start + last.line.len) "" else " ";
+ const last = @min(l + 1, nlines - 1);
+ const sep: []const u8 = if (to == panes.File.textLineStart(t, text, last) + panes.File.textLine(t, text, last).len) "" else " ";
changes.append(arena, .{ .q0 = @intCast(from), .q1 = @intCast(to), .text = sep }) catch return;
}
next = @max(next, end);