//! 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, null) 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"); }