From 0910900a9f26a85dc3dfd20d01dc14b4492bf14e Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Fri, 10 Oct 2025 11:59:02 +0200 Subject: [PATCH] feat: add dump_document_tree command --- src/tui/editor.zig | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 211e62a..cff09a6 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -4863,6 +4863,51 @@ pub const Editor = struct { } pub const dump_current_line_tree_meta: Meta = .{ .description = "Debug: dump current line (tree)" }; + pub fn dump_document_tree(self: *Self, _: Context) Result { + const syn = self.syntax orelse return error.Stop; + const tree = syn.tree orelse return error.Stop; + const ast_ = tree.getRootNode().asSExpressionString(); + defer syntax.Node.freeSExpressionString(ast_); + var scratch_name: std.Io.Writer.Allocating = .init(self.allocator); + defer scratch_name.deinit(); + + var ast: std.Io.Writer.Allocating = .init(self.allocator); + defer ast.deinit(); + var iter = ast_; + var indt: usize = 0; + while (iter.len > 0) : (iter = iter[1..]) { + const char = iter[0]; + switch (char) { + '(' => { + if (indt > 0) { + try ast.writer.writeByte('\n'); + for (0..indt) |_| try ast.writer.writeAll(" "); + } + indt += 1; + try ast.writer.writeByte(char); + }, + ')' => { + indt -= 1; + try ast.writer.writeByte(char); + }, + ' ' => { + if (iter.len > 1 and iter[1] == '(') + continue; + try ast.writer.writeByte(char); + }, + else => try ast.writer.writeByte(char), + } + } + + try if (self.file_path) |file_path| + scratch_name.writer.print("*ast: {s}*", .{file_path}) + else + scratch_name.writer.print("*ast*", .{}); + try command.executeName("open_scratch_buffer", command.fmt(.{ scratch_name.written(), ast.written(), "scheme" })); + tp.self_pid().send(.{ "cmd", "navigate", .{ .file = scratch_name.written() } }) catch return; + } + pub const dump_document_tree_meta: Meta = .{ .description = "Debug: dump current document tree" }; + pub fn undo(self: *Self, _: Context) Result { try self.restore_undo(); self.clamp();