summaryrefslogtreecommitdiff
path: root/src/edit.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/edit.zig')
-rw-r--r--src/edit.zig107
1 files changed, 36 insertions, 71 deletions
diff --git a/src/edit.zig b/src/edit.zig
index 55365692..c9477c29 100644
--- a/src/edit.zig
+++ b/src/edit.zig
@@ -8,6 +8,7 @@ const mouse = @import("mouse.zig");
const normal = @import("normal.zig");
const std = @import("std");
const modal = @import("modal.zig");
+const sam = @import("sam_edit.zig");
const panes = @import("panes.zig");
const body_layer = @import("body_layer.zig");
const config = @import("config.zig");
@@ -1318,23 +1319,10 @@ pub fn normalChange(p: *Pardes, t: *Text) void {
const r1 = @max(t.msel.r0, t.msel.r1);
pushUndo(p, t);
const eb = editText(p, t, r0, r1 + 1, -1) orelse return;
- const arena = p.scratch.allocator();
const lo: usize = @intCast(@max(0, r0 - eb.row0));
const hi: usize = @intCast(@max(0, r1 - eb.row0));
- var total: usize = hi - lo;
- for (lo..hi + 1) |r| total += modal.lineSlice(eb.text, r).len;
- const buf = arena.alloc(u8, total) catch return;
- var at: usize = 0;
- for (lo..hi + 1) |r| {
- if (r > lo) {
- buf[at] = '\n';
- at += 1;
- }
- const line = modal.lineSlice(eb.text, r);
- @memcpy(buf[at..][0..line.len], line);
- at += line.len;
- }
- setYank(p, buf);
+ // the lines are adjacent in the text already, their newlines and all
+ setYank(p, eb.text[modal.lineStartOffset(eb.text, lo)..modal.lineEndOffset(eb.text, hi)]);
var body: []u8 = eb.text;
if (hi > lo) {
const d = modal.deleteLines(p.gpa, body, lo + 1, hi) catch {
@@ -1485,45 +1473,22 @@ pub fn normalCase(p: *Pardes, t: *Text, op: modal.CaseOp) void {
setEditText(p, t, new);
}
-/// a change to apply to file content: delete [from, to), insert `ins` at
-/// `from`. Ascending and disjoint.
-const TextChange = struct { from: usize, to: usize, ins: []const u8 };
-
-fn mapThroughChanges(chs: []const TextChange, pos: usize) usize {
+/// Where `pos` lands after `chs`, which are ascending and disjoint.
+fn mapThroughChanges(chs: []const sam.Op, pos: usize) usize {
var delta: i64 = 0;
for (chs) |ch| {
- if (pos < ch.from) break;
- if (ch.from == ch.to) {
- delta += @intCast(ch.ins.len);
+ if (pos < ch.q0) break;
+ if (ch.q0 == ch.q1) {
+ delta += @intCast(ch.text.len);
continue;
}
- if (pos == ch.from) break;
- if (pos < ch.to) return @intCast(@as(i64, @intCast(ch.from)) + delta);
- delta += @as(i64, @intCast(ch.ins.len)) - @as(i64, @intCast(ch.to - ch.from));
+ if (pos == ch.q0) break;
+ if (pos < ch.q1) return @intCast(@as(i64, @intCast(ch.q0)) + delta);
+ delta += @as(i64, @intCast(ch.text.len)) - @as(i64, @intCast(ch.q1 - ch.q0));
}
return @intCast(@as(i64, @intCast(pos)) + delta);
}
-/// apply a change list. gpa-owned result.
-fn applyChanges(p: *Pardes, text: []const u8, chs: []const TextChange) ![]u8 {
- var total = text.len;
- for (chs) |ch| total = total - (ch.to - ch.from) + ch.ins.len;
- const out = try p.gpa.alloc(u8, total);
- errdefer p.gpa.free(out);
- var copied: usize = 0;
- var at: usize = 0;
- for (chs) |ch| {
- const unchanged = text[copied..ch.from];
- @memcpy(out[at..][0..unchanged.len], unchanged);
- at += unchanged.len;
- @memcpy(out[at..][0..ch.ins.len], ch.ins);
- at += ch.ins.len;
- copied = ch.to;
- }
- @memcpy(out[at..], text[copied..]);
- return out;
-}
-
/// `J` / `Alt-J`: join every range's lines with the next, in ONE edit
/// (helix join_selections): the newline and the next line's leading blanks
/// become one space, or nothing when that line is blank, and a line two
@@ -1545,7 +1510,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void {
// counted from the top, a J in a 7 MB file was 8 ms.
const nlines = lineCountOf(p, t, text);
const arena = p.scratch.allocator();
- var changes: std.ArrayList(TextChange) = .empty;
+ 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
@@ -1562,7 +1527,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void {
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 " ";
- changes.append(arena, .{ .from = from, .to = to, .ins = sep }) catch return;
+ changes.append(arena, .{ .q0 = @intCast(from), .q1 = @intCast(to), .text = sep }) catch return;
}
next = @max(next, end);
}
@@ -1570,7 +1535,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void {
const chs_len = chs.len;
if (chs_len == 0) return;
pushUndo(p, t);
- const new = applyChanges(p, text, chs[0..chs_len]) catch return;
+ const new = sam.apply(p.gpa, text, chs[0..chs_len]) catch return;
// every range's cells ride through the edit; Alt-J replaces them with
// the spaces, when it put any in
var cells: [Text.max_selections]Text.SelRange = undefined;
@@ -1579,9 +1544,9 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void {
if (select_space) {
var delta: i64 = 0;
for (chs[0..chs_len]) |ch| {
- const at: usize = @intCast(@as(i64, @intCast(ch.from)) + delta);
- delta += @as(i64, @intCast(ch.ins.len)) - @as(i64, @intCast(ch.to - ch.from));
- if (ch.ins.len == 0 or n == cells.len) continue;
+ const at: usize = @intCast(@as(i64, @intCast(ch.q0)) + delta);
+ delta += @as(i64, @intCast(ch.text.len)) - @as(i64, @intCast(ch.q1 - ch.q0));
+ if (ch.text.len == 0 or n == cells.len) continue;
const pos = modal.positionAt(new, at);
cells[n] = .{ .row = @as(i32, @intCast(pos.row)) + eb.row0, .col = @intCast(pos.col), .arow = @as(i32, @intCast(pos.row)) + eb.row0, .acol = @intCast(pos.col) };
n += 1;
@@ -1627,23 +1592,23 @@ pub fn normalRotateContents(p: *Pardes, t: *Text, forward: bool, cnt: usize) voi
const n = got.n;
if (n < 2) return;
const by = @min(cnt, n);
- var chs: [Text.max_selections]TextChange = undefined;
+ var chs: [Text.max_selections]sam.Op = undefined;
for (rs[0..n], 0..) |r, i| {
const src = rs[if (forward) (i + n - by) % n else (i + by) % n];
chs[i] = .{
- .from = @min(r.anchor, r.head),
- .to = @max(r.anchor, r.head),
- .ins = text[@min(src.anchor, src.head)..@max(src.anchor, src.head)],
+ .q0 = @intCast(@min(r.anchor, r.head)),
+ .q1 = @intCast(@max(r.anchor, r.head)),
+ .text = text[@min(src.anchor, src.head)..@max(src.anchor, src.head)],
};
}
pushUndo(p, t);
- const new = applyChanges(p, text, chs[0..n]) catch return;
+ const new = sam.apply(p.gpa, text, chs[0..n]) catch return;
var cells: [Text.max_selections]Text.SelRange = undefined;
var delta: i64 = 0;
for (chs[0..n], rs[0..n], 0..) |ch, r, i| {
- const from: usize = @intCast(@as(i64, @intCast(ch.from)) + delta);
- delta += @as(i64, @intCast(ch.ins.len)) - @as(i64, @intCast(ch.to - ch.from));
- const to = from + ch.ins.len;
+ const from: usize = @intCast(@as(i64, @intCast(ch.q0)) + delta);
+ delta += @as(i64, @intCast(ch.text.len)) - @as(i64, @intCast(ch.q1 - ch.q0));
+ const to = from + ch.text.len;
const c = Text.rangeCells(new, if (r.anchor > r.head) .{ .anchor = to, .head = from } else .{ .anchor = from, .head = to });
const cc = modal.positionAt(new, c.cur);
const ac = modal.positionAt(new, c.anc);
@@ -1700,7 +1665,7 @@ pub fn normalAlign(p: *Pardes, t: *Text) void {
// each column's head position, left to right
for (1..n) |i| widths[i] += widths[i - 1];
const arena = p.scratch.allocator();
- var chs: [Text.max_selections]TextChange = undefined;
+ var chs: [Text.max_selections]sam.Op = undefined;
var m: usize = 0;
line = std.math.maxInt(usize);
for (rs[0..n], 0..) |r, i| {
@@ -1716,12 +1681,12 @@ pub fn normalAlign(p: *Pardes, t: *Text) void {
const spaces = arena.alloc(u8, pad) catch return;
@memset(spaces, ' ');
const at = @min(r.anchor, r.head);
- chs[m] = .{ .from = at, .to = at, .ins = spaces };
+ chs[m] = .{ .q0 = @intCast(at), .q1 = @intCast(at), .text = spaces };
m += 1;
}
if (m == 0) return;
pushUndo(p, t);
- const new = applyChanges(p, text, chs[0..m]) catch return;
+ const new = sam.apply(p.gpa, text, chs[0..m]) catch return;
var cells: [Text.max_selections]Text.SelRange = undefined;
for (rs[0..n], 0..) |r, i| {
const c = Text.rangeCells(text, r);
@@ -1751,7 +1716,7 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) void {
// one run of spaces, sliced per line: `>` never inserts more than this
const pad = arena.alloc(u8, modal.INDENT_W * cnt) catch return;
@memset(pad, ' ');
- const chs = arena.alloc(TextChange, span.end - span.start + 1) catch return;
+ const chs = arena.alloc(sam.Op, span.end - span.start + 1) catch return;
var chs_len: usize = 0;
var l = span.start;
while (l <= span.end) : (l += 1) {
@@ -1762,7 +1727,7 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) void {
if (nw == line.len) continue; // blank lines stay blank (helix)
if (add) {
const ins = modal.INDENT_W * cnt - (nw % modal.INDENT_W);
- chs[chs_len] = .{ .from = ls, .to = ls, .ins = pad[0..ins] };
+ chs[chs_len] = .{ .q0 = @intCast(ls), .q1 = @intCast(ls), .text = pad[0..ins] };
chs_len += 1;
} else {
const want = modal.INDENT_W * cnt;
@@ -1778,7 +1743,7 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) void {
if (w >= want) break;
}
if (pos > 0) {
- chs[chs_len] = .{ .from = ls, .to = ls + pos, .ins = "" };
+ chs[chs_len] = .{ .q0 = @intCast(ls), .q1 = @intCast(ls + pos), .text = "" };
chs_len += 1;
}
}
@@ -1790,7 +1755,7 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) void {
modal.offsetAt(text, .{ .row = @intCast(@max(0, t.vsel.row - eb.row0)), .col = @intCast(@max(0, t.vsel.col)) })
else
cur_off;
- const new = applyChanges(p, text, chs[0..chs_len]) catch return;
+ const new = sam.apply(p.gpa, text, chs[0..chs_len]) catch return;
const nc = mapThroughChanges(chs[0..chs_len], cur_off);
const na = mapThroughChanges(chs[0..chs_len], anc_off);
setEditText(p, t, new);
@@ -1864,7 +1829,7 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) void {
}
if (live == 0) return; // nothing but blank lines
const ins = std.fmt.allocPrint(arena, "{s} ", .{token}) catch return;
- const chs = arena.alloc(TextChange, lines_len) catch return;
+ const chs = arena.alloc(sam.Op, lines_len) catch return;
var chs_len: usize = 0;
for (lines[0..lines_len]) |l| {
const ls = modal.lineStartOffset(text, l);
@@ -1875,16 +1840,16 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) void {
// the @min can never bind: margin is 1 only when every line has a
// space after its own token, which is a byte past `at + token.len`
chs[chs_len] = if (commented)
- .{ .from = at, .to = @min(at + token.len + margin, le), .ins = "" }
+ .{ .q0 = @intCast(at), .q1 = @intCast(@min(at + token.len + margin, le)), .text = "" }
else
- .{ .from = at, .to = at, .ins = ins };
+ .{ .q0 = @intCast(at), .q1 = @intCast(at), .text = ins };
chs_len += 1;
}
pushUndo(p, t);
// one edit, and the WHOLE selection rides through it (helix maps the
// selection with the transaction)
var cells: [Text.max_selections]Text.SelRange = undefined;
- const new = applyChanges(p, text, chs[0..chs_len]) catch return;
+ const new = sam.apply(p.gpa, text, chs[0..chs_len]) catch return;
for (rs[0..got.n], 0..) |r, i| {
const c = Text.rangeCells(new, .{
.anchor = mapThroughChanges(chs[0..chs_len], r.anchor),