feat: render ast nodes in inspector view even when there are no highlights

This commit is contained in:
CJ van den Berg 2025-09-29 21:00:00 +02:00
parent ce6d16b656
commit 27c01b940a
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 53 additions and 28 deletions

View file

@ -6,8 +6,8 @@
.dependencies = .{
.syntax = .{
.url = "git+https://github.com/neurocyte/flow-syntax?ref=zig-0.14#c9f6934ec7823223590a9ae44a581c0e385462d1",
.hash = "flow_syntax-0.1.0-X8jOoXQSAQAn5N_-91mSxpH3WJTOHonPaUbkePpBlV6o",
.url = "git+https://github.com/neurocyte/flow-syntax?ref=zig-0.14#9f430faf6c31cab2be13ebb5406f6a7476b7eb61",
.hash = "flow_syntax-0.1.0-X8jOoYsSAQCAPWeJEamG_eyMOYZFN_ZWZAdKnnGtUhBi",
},
.flags = .{
.url = "https://github.com/n0s4/flags/archive/372501d1576b5723829bcba98e41361132c7b618.tar.gz",

View file

@ -187,7 +187,7 @@ pub const CurSel = struct {
};
}
fn selection_from_node(node: syntax.Node, root: Buffer.Root, metrics: Buffer.Metrics) error{NotFound}!Selection {
pub fn selection_from_node(node: syntax.Node, root: Buffer.Root, metrics: Buffer.Metrics) error{NotFound}!Selection {
return selection_from_range(node.getRange(), root, metrics);
}

View file

@ -73,7 +73,8 @@ fn inspect_location(self: *Self, row: usize, col: usize) void {
const syn = self.editor.syntax orelse return;
const root = (self.editor.buffer orelse return).root;
const col_pos = root.get_line_width_to_pos(row, col, self.editor.metrics) catch return;
syn.highlights_at_point(self, dump_highlight, .{ .row = @intCast(row), .column = @intCast(col_pos) });
if (!syn.highlights_at_point(self, dump_highlight, .{ .row = @intCast(row), .column = @intCast(col_pos) }))
self.ast_at_point(syn, row, col_pos, root);
}
fn get_buffer_text(self: *Self, buf: []u8, sel: Buffer.Selection) ?[]const u8 {
@ -81,9 +82,57 @@ fn get_buffer_text(self: *Self, buf: []u8, sel: Buffer.Selection) ?[]const u8 {
return root.get_range(sel, buf, null, null, self.plane.metrics(self.editor.tab_width)) catch return null;
}
fn ast_at_point(self: *Self, syn: anytype, row: usize, col_pos: usize, root: Buffer.Root) void {
const node = syn.node_at_point_range(.{
.start_point = .{
.row = @intCast(row),
.column = @intCast(col_pos),
},
.end_point = .{
.row = @intCast(row),
.column = @intCast(col_pos),
},
.start_byte = 0,
.end_byte = 0,
}) catch return;
if (node.isNull()) return;
const sel = ed.CurSel.selection_from_node(node, root, self.editor.metrics) catch return;
self.dump_ast_node(sel, &node);
}
fn dump_highlight(self: *Self, range: syntax.Range, scope: []const u8, id: u32, _: usize, ast_node: *const syntax.Node) error{Stop}!void {
const sel = self.pos_cache.range_to_selection(range, self.editor.get_current_root() orelse return, self.editor.metrics) orelse return;
self.dump_ast_node(sel, ast_node);
var buf: [1024]u8 = undefined;
const text = self.get_buffer_text(&buf, sel) orelse "";
if (self.editor.style_lookup(self.theme, scope, id)) |token| {
if (text.len > 14) {
_ = self.plane.print("scope: {s} -> \"{s}...\" matched: {s}", .{
scope,
text[0..15],
Widget.scopes[token.id],
}) catch {};
} else {
_ = self.plane.print("scope: {s} -> \"{s}\" matched: {s}", .{
scope,
text,
Widget.scopes[token.id],
}) catch {};
}
self.show_color("fg", token.style.fg);
self.show_color("bg", token.style.bg);
self.show_font(token.style.fs);
_ = self.plane.print("\n", .{}) catch {};
return;
}
_ = self.plane.print("scope: {s} -> \"{s}\"\n", .{ scope, text }) catch return;
}
fn dump_ast_node(self: *Self, sel: Buffer.Selection, ast_node: *const syntax.Node) void {
var update_match: enum { no, add, set } = .no;
var match = ed.Match.from_selection(sel);
if (self.theme) |theme| match.style = .{ .bg = theme.editor_gutter_modified.fg };
@ -124,30 +173,6 @@ fn dump_highlight(self: *Self, range: syntax.Range, scope: []const u8, id: u32,
}
}
self.last_node = @intFromPtr(ast_node);
var buf: [1024]u8 = undefined;
const text = self.get_buffer_text(&buf, sel) orelse "";
if (self.editor.style_lookup(self.theme, scope, id)) |token| {
if (text.len > 14) {
_ = self.plane.print("scope: {s} -> \"{s}...\" matched: {s}", .{
scope,
text[0..15],
Widget.scopes[token.id],
}) catch {};
} else {
_ = self.plane.print("scope: {s} -> \"{s}\" matched: {s}", .{
scope,
text,
Widget.scopes[token.id],
}) catch {};
}
self.show_color("fg", token.style.fg);
self.show_color("bg", token.style.bg);
self.show_font(token.style.fs);
_ = self.plane.print("\n", .{}) catch {};
return;
}
_ = self.plane.print("scope: {s} -> \"{s}\"\n", .{ scope, text }) catch return;
}
fn show_color(self: *Self, tag: []const u8, c_: ?Widget.Theme.Color) void {