diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/edit.zig | 53 | ||||
| -rw-r--r-- | src/indent.zig | 367 | ||||
| -rw-r--r-- | src/syntax.zig | 1 |
3 files changed, 407 insertions, 14 deletions
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(); }, diff --git a/src/indent.zig b/src/indent.zig new file mode 100644 index 00000000..fc2df450 --- /dev/null +++ b/src/indent.zig @@ -0,0 +1,367 @@ +//! helix's indent for a new line, per file type: indent.rs's containment +//! engine (helix-core/src/indent.rs at 278b24389, ~/05-genizah/helix) over +//! the file's tree (normal.fileTree) and its grammar's indents.scm +//! (vendor/queries), with every capture and predicate those use. Pure: the +//! tree and the text in, the indent's bytes out. A file whose grammar has no +//! query, or that has no grammar, keeps acme's copy (edit.zig). +const std = @import("std"); +const syntax = @import("syntax.zig"); +const regexp = @import("regexp.zig"); +const ts = syntax.ts; + +/// A file's indent query and its unit's width (helix languages.toml's +/// `[language.indent]`): 0 is a tab, four columns wide. +pub const Lang = struct { query: *ts.Query, width: u8 }; + +const widths = [_]struct { []const u8, u8 }{ + .{ "bash", 2 }, .{ "c", 2 }, .{ "c_sharp", 0 }, .{ "clojure", 2 }, .{ "cpp", 2 }, + .{ "css", 2 }, .{ "elixir", 2 }, .{ "fortran", 4 }, .{ "go", 0 }, .{ "html", 2 }, + .{ "java", 2 }, .{ "json", 2 }, .{ "javascript", 2 }, .{ "kotlin", 4 }, .{ "ocaml", 2 }, + .{ "php", 4 }, .{ "python", 4 }, .{ "ruby", 2 }, .{ "rust", 4 }, .{ "scala", 2 }, + .{ "zig", 4 }, +}; + +pub fn lang(path: []const u8) ?Lang { + const q, const name = syntax.indentQuery(path) orelse return null; + for (widths) |w| if (std.mem.eql(u8, w[0], name)) return .{ .query = q, .width = w[1] }; + return .{ .query = q, .width = 4 }; +} + +/// A level of indent on `ws`: a tab where `ws` has one (as acme's copy keeps +/// tabs; helix finds the file's style instead), else the language's unit, +/// four spaces without one. +pub fn unit(l: ?Lang, ws: []const u8) []const u8 { + const w = if (l) |x| x.width else 4; + if (w == 0 or std.mem.indexOfScalar(u8, ws, '\t') != null) return "\t"; + return " "[0..w]; +} + +/// The columns a tab and a level take (helix's tab_width and indent_width). +fn levelWidth(l: Lang) usize { + return if (l.width == 0) 4 else l.width; +} + +/// helix's indent_for_newline with its default `hybrid` heuristic +/// (indent.rs:1277): the indent of a line opened after byte `pos`, on line +/// `line`; null where the tree has nothing to say. +pub fn forNewline(arena: std.mem.Allocator, l: Lang, tree_ptr: *anyopaque, text: []const u8, line: usize, pos: usize) error{OutOfMemory}!?[]const u8 { + const tree: *const ts.Tree = @ptrCast(@alignCast(tree_ptr)); + const root = tree.rootNode(); + var start = lineStart(text, pos); + const new = try at(arena, l, root, text, line, start, pos, true) orelse return null; + // A line above's own indent, moved by what the tree says differs: four + // lines with something on them at most (:1308). + var row = line; + var tries: usize = 0; + while (tries < 4) { + const end = std.mem.indexOfScalarPos(u8, text, start, '\n') orelse text.len; + if (std.mem.indexOfNone(u8, text[start..end], " \t")) |first| { + tries += 1; + if (try at(arena, l, root, text, row, start, start + first, false)) |base| + if ((new.@"align" == null) == (base.@"align" == null) and std.mem.eql(u8, new.@"align" orelse "", base.@"align" orelse "")) + return try leveled(arena, l, text[start..][0..first], new.net() - base.net()); + } + if (row == 0) break; + row -= 1; + start = lineStart(text, start - 1); + } + return try leveled(arena, l, try sameWidth(arena, new.@"align" orelse ""), new.net()); +} + +/// indent.rs's Indentation (:452): levels in (@indent and @indent.always) +/// and out (@outdent and @outdent.always), or an alignment. +const Indentation = struct { + in: usize = 0, + out: usize = 0, + @"align": ?[]const u8 = null, + + fn net(i: Indentation) isize { + return @as(isize, @intCast(i.in)) - @as(isize, @intCast(i.out)); + } +}; + +const Kind = enum { indent, @"indent.always", outdent, @"outdent.always", @"align", anchor, extend, @"extend.prevent-once", @"opaque" }; + +/// A node a match captured (indent.rs:505, :526), the whitespace before its +/// @anchor for an @align, and whether its pattern says `scope "header"`. +const Cap = struct { node: ts.Node, kind: Kind, header: bool, ws: []const u8 }; + +/// treesitter_indent_for_pos (:978) for line `line` (starting at +/// `line_start`), from byte `pos`: init_indent_query (:798) then +/// containment_accounting (:1049). No injections: the file's tree is one +/// language's. +fn at(arena: std.mem.Allocator, l: Lang, root: ts.Node, text: []const u8, line: usize, line_start: usize, pos_in: usize, new_line: bool) !?Indentation { + const pos: u32 = @intCast(@min(pos_in, std.math.maxInt(u32))); + const nl: ?u32 = if (new_line) pos else null; + var node = root.descendantForByteRange(pos, pos) orelse return null; + // the deepest node ending before the position (:815) + var prec: ?ts.Node = null; + for (0..node.childCount()) |i| { + const child = node.child(@intCast(i)).?; + if (child.endByte() <= pos) prec = child; + } + if (prec) |*p| while (p.childCount() > 0) { + p.* = p.child(p.childCount() - 1).?; + }; + // the body a new line opens, past its header (candidate_body_for_new_line, :754) + const body = if (nl == null) null else if (prec) |p| bodyAfter(p, pos) else null; + const caps = try query(arena, l.query, root, text, if (prec) |p| p.endByte() -| 1 else pos, if (body) |b| b.endByte() else pos + 1, nl); + // inside an @opaque node begun on a line before: the line's own indent (:1068, :1015) + for (caps) |c| if (c.kind == .@"opaque" and c.node.startByte() <= pos and pos < c.node.endByte() and c.node.startPoint().row < line) + return .{ .@"align" = text[line_start..][0 .. std.mem.indexOfNone(u8, text[line_start..], " \t") orelse text.len - line_start] }; + // into that body when the query scopes it to its header, else @extend (:869) + if (body != null and for (caps) |c| { + if (c.node.eql(body.?) and c.header and @backingInt(c.kind) <= @backingInt(Kind.@"align")) break true; + } else false) node = body.? else if (prec) |p| extend(&node, p, caps, text, line, line_start, levelWidth(l)); + + // the @indent scopes holding the target line, a level each line they + // open on, less an @outdent that starts it (:1090) + const target = line + @intFromBool(new_line); + var r: Indentation = .{}; + var counted: std.ArrayList(usize) = .empty; + var n = node; + while (true) { + var in, var in_always, var out, var out_always, var header = .{ false, false, false, false, false }; + var al: ?[]const u8 = null; + for (caps) |c| if (c.node.eql(n)) switch (c.kind) { + .indent, .@"indent.always" => { + if (c.kind == .indent) in = true else in_always = true; + header = header or c.header; + }, + .outdent => out = true, + .@"outdent.always" => out_always = true, + .@"align" => al = c.ws, + else => {}, + }; + const first = startLine(n, nl); + const start = if (!header) first else if (n.parent()) |p| startLine(p, nl) else first; + const contains = start < target and target <= endLine(n, nl); + // a node that both indents and outdents cancels itself (:1137) + if (!((in or in_always) and (out or out_always))) { + if (in and contains and std.mem.indexOfScalar(usize, counted.items, start) == null) { + try counted.append(arena, start); + r.in += 1; + } + r.in += @intFromBool(in_always and contains); + if (first == target) r.out += @as(usize, @intFromBool(out)) + @intFromBool(out_always); + } + if (al != null and contains and r.@"align" == null) r.@"align" = al; + n = n.parent() orelse break; + } + // an alignment is a column, the levels it spans already in it (:1166) + if (r.@"align" != null) r.in = 0; + return r; +} + +/// query_indents (:560): what the query captures over [lo, hi), the matches +/// whose predicates hold. +fn query(arena: std.mem.Allocator, q: *ts.Query, root: ts.Node, text: []const u8, lo: u32, hi: u32, nl: ?u32) ![]const Cap { + var caps: std.ArrayList(Cap) = .empty; + const cursor = ts.QueryCursor.create(); + defer cursor.destroy(); + cursor.setMatchLimit(256); // tree_house's TREE_SITTER_MATCH_LIMIT + cursor.setByteRange(lo, hi) catch return caps.items; + cursor.exec(q, root); + while (cursor.nextMatch()) |m| { + const header = holds(q, m, text, nl) orelse continue; + var anchor: ?ts.Node = null; + for (m.captures) |c| if (anchor == null and kindOf(q, c.index) == .anchor) { + anchor = c.node; + }; + for (m.captures) |c| { + const kind = kindOf(q, c.index) orelse continue; + if (kind == .anchor) continue; + // an @align is to its @anchor's column, the whitespace before it kept as it is + const ws = if (kind != .@"align") "" else if (anchor) |a| text[a.startByte() - a.startPoint().column .. a.startByte()] else continue; + try caps.append(arena, .{ .node = c.node, .kind = kind, .header = header, .ws = ws }); + } + } + return caps.items; +} + +fn kindOf(q: *const ts.Query, index: u32) ?Kind { + return std.meta.stringToEnum(Kind, q.captureNameForId(index) orelse return null); +} + +/// Whether a match's predicates hold (IndentQueryPredicates, :304, and +/// #match? as tree-sitter has it), and whether it sets `scope "header"` +/// (:372); null when one does not hold. +fn holds(q: *const ts.Query, m: ts.Query.Match, text: []const u8, nl: ?u32) ?bool { + var header = false; + var steps = q.predicatesForPattern(m.pattern_index); + while (steps.len > 0) { + const end = for (steps, 0..) |s, i| { + if (s.type == .done) break i; + } else steps.len; + const pred = steps[0..end]; + steps = steps[@min(end + 1, steps.len)..]; + if (pred.len < 2) continue; + const name = q.stringValueForId(pred[0].value_id) orelse continue; + const negated = std.mem.startsWith(u8, name, "not-"); + const a = captured(m, pred[1]); + const b = if (pred.len > 2) captured(m, pred[2]) else null; + const arg = if (pred.len > 2 and pred[2].type == .string) q.stringValueForId(pred[2].value_id) orelse "" else ""; + const base = name[if (negated) 4 else 0..]; + if (std.mem.eql(u8, name, "set!")) { + const key = if (pred[1].type == .string) q.stringValueForId(pred[1].value_id) orelse "" else ""; + header = header or (std.mem.eql(u8, key, "scope") and std.mem.eql(u8, arg, "header")); + } else if (std.mem.eql(u8, name, "not-kind-eq?")) { + if (a != null and std.mem.eql(u8, a.?.kind(), arg)) return null; + } else if (std.mem.eql(u8, base, "same-line?")) { + if (a == null or b == null or (startLine(a.?, nl) == startLine(b.?, nl)) == negated) return null; + } else if (std.mem.eql(u8, base, "one-line?")) { + if (a == null or (startLine(a.?, nl) == endLine(a.?, nl)) == negated) return null; + } else if (std.mem.eql(u8, base, "match?")) { + const node = a orelse continue; + if (matches(text[node.startByte()..node.endByte()], arg) == negated) return null; + } + } + return header; +} + +/// The first node a match captured for a predicate's capture argument. +fn captured(m: ts.Query.Match, step: ts.Query.PredicateStep) ?ts.Node { + if (step.type != .capture) return null; + for (m.captures) |c| if (c.index == step.value_id) return c.node; + return null; +} + +/// A regex found in a node's text, `^` its start alone. +fn matches(subject: []const u8, pattern: []const u8) bool { + var re = regexp.Regex.compile(pattern) catch return false; + const last = if (std.mem.startsWith(u8, pattern, "^")) 0 else subject.len; + return (re.find(subject, 0, last, subject.len) catch null) != null; +} + +/// The node after a header that ends before `pos`, which a new line there +/// opens (candidate_body_for_new_line, :754). +fn bodyAfter(prec: ts.Node, pos: u32) ?ts.Node { + var n = prec; + while (n.parent()) |parent| : (n = parent) { + if (n.endByte() > pos) return null; + if (n.nextSibling()) |next| if (next.startByte() >= pos) return next; + } + return null; +} + +/// extend_nodes (:675): an @extend node ending on the line, or begun on a +/// line indented less, takes the line in, unless an @extend.prevent-once +/// below it says not this once. +fn extend(node: *ts.Node, from: ts.Node, caps: []const Cap, text: []const u8, line: usize, line_start: usize, width: usize) void { + var stop = false; + var n = from; + while (!n.eql(node.*)) { + var grow = false; + var captured_here = false; + for (caps) |c| if (c.node.eql(n)) switch (c.kind) { + .@"extend.prevent-once" => stop = true, + .extend => { + captured_here = true; + const below = columns(text[n.startByte() - n.startPoint().column ..], width) / width; + if (n.endPoint().row == line or columns(text[line_start..], width) / width > below) grow = true; + }, + else => {}, + }; + if (captured_here and stop) { + stop = false; + } else if (grow and !stop) { + node.* = n; + return; + } + n = n.parent() orelse return; + } +} + +/// The columns `s`'s leading blanks take, tabs to stops `width` apart +/// (indent_level_for_line, :200, a level `width` of them). +fn columns(s: []const u8, width: usize) usize { + var cols: usize = 0; + for (s) |ch| switch (ch) { + '\t' => cols += width - cols % width, + ' ' => cols += 1, + else => break, + }; + return cols; +} + +/// A node's first and last lines, a new line at `nl` counted in (:543). +fn startLine(n: ts.Node, nl: ?u32) usize { + return n.startPoint().row + @intFromBool(nl != null and n.startByte() >= nl.?); +} + +fn endLine(n: ts.Node, nl: ?u32) usize { + return n.endPoint().row + @intFromBool(nl != null and n.endByte() > nl.?); +} + +fn lineStart(text: []const u8, pos: usize) usize { + return if (std.mem.lastIndexOfScalar(u8, text[0..pos], '\n')) |i| i + 1 else 0; +} + +/// whitespace_with_same_width (:214): tabs kept, a space for anything else +/// (a character a column: wide ones are not told). +fn sameWidth(arena: std.mem.Allocator, s: []const u8) ![]const u8 { + var out: std.ArrayList(u8) = .empty; + for (s) |ch| if (ch & 0xc0 != 0x80) try out.append(arena, if (ch == '\t') '\t' else ' '); + return out.items; +} + +/// add_indent_level (:259): `n` levels more on `base`, or fewer, cut where +/// the columns left end. +fn leveled(arena: std.mem.Allocator, l: Lang, base: []const u8, n: isize) ![]const u8 { + const u = unit(l, base); + if (n >= 0) { + const out = try arena.alloc(u8, base.len + u.len * @as(usize, @intCast(n))); + @memcpy(out[0..base.len], base); + for (0..@intCast(n)) |i| @memcpy(out[base.len + i * u.len ..][0..u.len], u); + return out; + } + const w = levelWidth(l); + const want = columns(base, w) -| @as(usize, @intCast(-n)) * w; + var col: usize = 0; + var cut: usize = 0; + for (base, 0..) |ch, i| { + const cw = if (ch == '\t') w - col % w else 1; + if (col + cw > want) break; + col += cw; + cut = i + 1; + } + return base[0..cut]; +} + +/// The indent forNewline gives a line opened at `|` in `marked`. +fn expectIndent(path: []const u8, marked: []const u8, want: []const u8) !void { + const l = lang(path) orelse return error.SkipZigTest; + const cut = std.mem.indexOfScalar(u8, marked, '|').?; + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const text = try std.mem.concat(arena.allocator(), u8, &.{ marked[0..cut], marked[cut + 1 ..] }); + const tree = syntax.parseTree(path, text) orelse return error.NoTree; + defer syntax.destroyTree(tree); + const got = try forNewline(arena.allocator(), l, tree, text, std.mem.count(u8, text[0..cut], "\n"), cut); + try std.testing.expectEqualStrings(want, got orelse return error.NoIndent); +} + +test "indent: a new line takes helix's indent from the tree, each capture and predicate as the reference helix has it" { + // @indent and @outdent (zig, adapted) + try expectIndent("a.zig", "fn f() void {|\n}\n", " "); + try expectIndent("a.zig", "fn f() void {\n a();|}\n", ""); + try expectIndent("a.zig", "fn f() void {\n if (x) {\n a();|\n }\n}\n", " "); + // @extend and @extend.prevent-once (python): a function's last line + // ends its scope (278b24389's engine; 25.07.1 kept the level) + try expectIndent("a.py", "def f():\n x = 1|\n y = 2\n", " "); + try expectIndent("a.py", "def f():\n x = 1|\n", ""); + try expectIndent("a.py", "class A:\n def f(self):\n pass|\n", ""); + // scopes opening on one line count once (rust) + try expectIndent("a.rs", "fn g() {\n f(|);\n}\n", " "); + try expectIndent("a.rs", "fn g() {\n let x = foo(\n 1,\n )|\n .bar();\n}\n", " "); + // #not-kind-eq? and scope "header": a brace-less body (c) + try expectIndent("a.c", "int f() {\n if (x)|\n y();\n}\n", " "); + // @align to an @anchor (python); #same-line? and #not-match? (clojure) + try expectIndent("a.py", "f(a,|\n b)\n", " "); + try expectIndent("a.clj", "(defn f [x]|\n (g x))\n", " "); + try expectIndent("a.clj", "(foo bar|\n baz)\n", " "); + // @opaque: a string's line keeps its own indent (python) + try expectIndent("a.py", "x = \"\"\"\n a|\n\"\"\"\n", " "); + // the hybrid baseline: the line above's own tabs (go) + try expectIndent("a.go", "func f() {\n\tif x {|\n\t}\n}\n", "\t\t"); +} diff --git a/src/syntax.zig b/src/syntax.zig index 5fe557ae..0521966d 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -1279,6 +1279,7 @@ fn diffSynAt(styles: []const u8, text: []const u8, needle: []const u8, offset: u test { _ = diff; + _ = @import("indent.zig"); } test "every vendored textobject and indent query compiles against its grammar" { |
