From c0ed5f823a37fb1b9b2300f53b23263bfc97a108 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 03:35:02 -0300 Subject: edit: Enter, o and O give the new line helix's indent where the file's grammar has an indent query, its indent_for_newline over the file's tree (indent.zig: the reference's containment engine, every capture and predicate the vendored queries use, the hybrid heuristic), else acme's copy as before; and Enter between a pair puts the closer on a line of its own at that indent and the cursor on one a level in, as helix's insert_newline does, in plain text too Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/edit.zig | 53 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 14 deletions(-) (limited to 'src/edit.zig') diff --git a/src/edit.zig b/src/edit.zig index a458a034..c5466531 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -14,6 +14,7 @@ const body_layer = @import("body_layer.zig"); const config = @import("config.zig"); const lsp = @import("lsp/lsp.zig"); const syntax = @import("syntax.zig"); +const indents = @import("indent.zig"); const platform = pardes.platform; const Pane = panes.Pane; const Text = panes.Text; @@ -553,6 +554,24 @@ pub fn retireEditText(p: *Pardes, new: []u8) void { p.retired_edit = new; } +/// helix's indent for a line opened after byte `at` of `row` (indent.zig), +/// from the file's tree, where its grammar has an indent query. +fn treeIndent(p: *Pardes, t: *Text, text: []const u8, row: usize, at: usize) Error!?[]const u8 { + const f = bodyFile(t) orelse return null; + const l = indents.lang(f.path) orelse return null; + const tree = normal.fileTree(p, f) orelse return null; + return indents.forNewline(p.scratch.allocator(), l, tree, text, row, at); +} + +/// helix's default auto-pairs (auto_pairs.rs DEFAULT_PAIRS): `o` opens one +/// that `c` closes. +fn opensPair(o: u8, c: u8) bool { + const pairs = "(){}[]''\"\"``"; + var i: usize = 0; + while (i < pairs.len) : (i += 2) if (pairs[i] == o and pairs[i + 1] == c) return true; + return false; +} + pub const InsertAt = enum { at, append, line_start, line_end, open_below, open_above }; /// Files insert only inside existing rows and at grapheme boundaries. @@ -639,7 +658,11 @@ pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) Error!void const abs: i32 = if (b) |bb| (if (below) bb.hi_row else bb.lo_row) else t.cur_row; const eb = (try editText(p, t, abs, abs, -1)) orelse return; const row: usize = @intCast(@max(0, abs - eb.row0)); - const ind = modal.indentText(modal.lineSlice(eb.text, row)); + // helix open_below/open_above: the tree's indent for a line + // after the one above the new one, else acme's copy of this one + const above = if (below) row else row -| 1; + const after = if (!below and row == 0) 0 else panes.File.textLineStart(t, eb.text, above) + panes.File.textLine(t, eb.text, above).len; + const ind = (try treeIndent(p, t, eb.text, above, after)) orelse modal.indentText(modal.lineSlice(eb.text, row)); const arena = p.scratch.allocator(); const block_len = std.math.mul(usize, cnt, ind.len + 1) catch return error.OutOfMemory; const block = try arena.alloc(u8, block_len); @@ -921,27 +944,29 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { t.show(); return; } - // The line's own indent, as it is (tabs stay tabs), and a - // level more after an opening delimiter: a tab where the - // indent is tabs, else spaces. - const lead = modal.indentText(line); - const more = modal.newlineIndentWidth(line, c.col) - lead.len; - const tabbed = std.mem.indexOfScalar(u8, lead, '\t') != null; - const indent = lead.len + (if (more == 0) 0 else if (tabbed) 1 else more); + // The tree's indent where the file's grammar has an indent + // query (helix indent_for_newline), else the line's own as it + // is (tabs stay tabs) and a level more after an opening + // delimiter. Between a pair (helix insert_newline's + // on_auto_pair) the closer goes to a line of its own at that + // indent, the cursor to one a level in. const arena = p.scratch.allocator(); - const block = try arena.alloc(u8, 1 + indent); - block[0] = '\n'; - @memcpy(block[1..][0..lead.len], lead); - @memset(block[1 + lead.len ..], if (tabbed) '\t' else ' '); + const lead = modal.indentText(line); + const tree = try treeIndent(p, t, text, c.row, panes.File.textLineStart(t, text, c.row) + before.len); + const pair = bodyFile(t) != null and c.col < line.len and opensPair(before[before.len - 1], line[c.col]); + const more = modal.newlineIndentWidth(line, c.col) > lead.len; + const base = tree orelse if (pair or !more) lead else try std.mem.concat(arena, u8, &.{ lead, indents.unit(null, lead) }); + const mid = if (!pair) base else try std.mem.concat(arena, u8, &.{ base, indents.unit(if (bodyFile(t)) |f| indents.lang(f.path) else null, base) }); + const block = try std.mem.concat(arena, u8, &.{ "\n", mid, if (pair) "\n" else "", if (pair) base else "" }); // a tag keeps its blanks: they part its words, and the last // may be its read-only prefix's const kept = if (t.what == .body) std.mem.trimEnd(u8, before, " \t").len else c.col; const cut = try modal.deleteSpan(p.gpa, text, .{ .row = c.row, .col = kept }, c); defer p.gpa.free(cut); const new = try modal.insertAt(p.gpa, cut, .{ .row = c.row, .col = kept }, block); - try setEditTextEnd(p, t, new, .{ .row = c.row + 1, .col = indent }); + try setEditTextEnd(p, t, new, if (pair) .{ .row = c.row + 2, .col = base.len } else .{ .row = c.row + 1, .col = mid.len }); t.cur_row += 1; - t.cur_col = @intCast(indent); + t.cur_col = @intCast(mid.len); t.cur_pinned = true; t.show(); }, -- cgit v1.3