diff --git a/src/syntax/src/syntax.zig b/src/syntax/src/syntax.zig index e85d859..41a3e18 100644 --- a/src/syntax/src/syntax.zig +++ b/src/syntax/src/syntax.zig @@ -58,8 +58,15 @@ pub fn destroy(self: *Self) void { self.allocator.destroy(self); } +pub fn reset(self: *Self) void { + if (self.tree) |tree| { + tree.destroy(); + self.tree = null; + } +} + pub fn refresh_full(self: *Self, content: []const u8) !void { - if (self.tree) |tree| tree.destroy(); + self.reset(); self.tree = try self.parser.parseString(null, content); } diff --git a/src/tui/editor.zig b/src/tui/editor.zig index c85773c..b4c1fb5 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -3065,7 +3065,11 @@ pub const Editor = struct { if (self.syntax_token == token) return; if (self.syntax) |syn| { - if (self.syntax_no_render) return; + if (self.syntax_no_render) { + syn.reset(); + self.syntax_token = 0; + return; + } if (!self.syntax_refresh_update) self.syntax_refresh_full = true; if (self.syntax_refresh_full) { @@ -3081,7 +3085,6 @@ pub const Editor = struct { } else { try syn.refresh_from_buffer(root, self.metrics); } - self.syntax_token = token; } else { var content = std.ArrayList(u8).init(self.allocator); defer content.deinit(); @@ -3093,6 +3096,7 @@ pub const Editor = struct { if (self.syntax_no_render) return; if (self.syntax) |syn| try syn.refresh_full(content.items); } + self.syntax_token = token; } fn reset_syntax(self: *Self) void { @@ -3930,6 +3934,15 @@ pub const Editor = struct { } } pub const toggle_eol_mode_meta = .{ .description = "Toggle end of line sequence" }; + + pub fn toggle_syntax_highlighting(self: *Self, _: Context) Result { + self.syntax_no_render = !self.syntax_no_render; + if (self.syntax_no_render) { + if (self.syntax) |syn| syn.reset(); + self.syntax_token = 0; + } + } + pub const toggle_syntax_highlighting_meta = .{ .description = "Toggle syntax highlighting" }; }; pub fn create(allocator: Allocator, parent: Widget) !Widget { diff --git a/src/tui/mode/input/flow.zig b/src/tui/mode/input/flow.zig index 28cfb2c..a22f8aa 100644 --- a/src/tui/mode/input/flow.zig +++ b/src/tui/mode/input/flow.zig @@ -182,6 +182,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void { }, mod.SHIFT => switch (keypress) { key.F03 => self.cmd("goto_prev_match", .{}), + key.F10 => self.cmd("toggle_syntax_highlighting", .{}), key.F12 => self.cmd("references", .{}), key.LEFT => self.cmd("select_left", .{}), key.RIGHT => self.cmd("select_right", .{}), @@ -426,6 +427,7 @@ const hints = tui.KeybindHints.initComptime(.{ .{ "toggle_inspector_view", "F5, C-F5, C-S-i" }, .{ "toggle_panel", "C-j, F11" }, .{ "toggle_whitespace_mode", "C-F10" }, + .{ "toggle_syntax_highlighting", "S-F10" }, .{ "to_lower", "A-l" }, .{ "to_upper", "A-u" }, .{ "undo", "C-z" }, diff --git a/src/tui/mode/input/vim/insert.zig b/src/tui/mode/input/vim/insert.zig index cbf33fe..4746e93 100644 --- a/src/tui/mode/input/vim/insert.zig +++ b/src/tui/mode/input/vim/insert.zig @@ -176,6 +176,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void { }, mod.SHIFT => switch (keypress) { key.F03 => self.cmd("goto_prev_match", .{}), + key.F10 => self.cmd("toggle_syntax_highlighting", .{}), key.LEFT => self.cmd("select_left", .{}), key.RIGHT => self.cmd("select_right", .{}), key.UP => self.cmd("select_up", .{}), diff --git a/src/tui/mode/input/vim/normal.zig b/src/tui/mode/input/vim/normal.zig index f5b2346..6785d47 100644 --- a/src/tui/mode/input/vim/normal.zig +++ b/src/tui/mode/input/vim/normal.zig @@ -181,6 +181,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void { }, mod.SHIFT => switch (keypress) { key.F03 => self.cmd("goto_prev_match", .{}), + key.F10 => self.cmd("toggle_syntax_highlighting", .{}), key.LEFT => self.cmd("select_left", .{}), key.RIGHT => self.cmd("select_right", .{}), key.UP => self.cmd("select_up", .{}), @@ -611,6 +612,7 @@ const hints = tui.KeybindHints.initComptime(.{ .{ "toggle_inspector_view", "F5, C-F5, C-S-i" }, .{ "toggle_panel", "C-j, F11" }, .{ "toggle_whitespace_mode", "C-F10" }, + .{ "toggle_syntax_highlighting", "S-F10" }, .{ "to_lower", "A-l" }, .{ "to_upper", "A-u" }, .{ "undo", "C-z" }, diff --git a/src/tui/mode/input/vim/visual.zig b/src/tui/mode/input/vim/visual.zig index 7c94954..7f9a17b 100644 --- a/src/tui/mode/input/vim/visual.zig +++ b/src/tui/mode/input/vim/visual.zig @@ -179,6 +179,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void { }, mod.SHIFT => switch (keypress) { key.F03 => self.cmd("goto_prev_match", .{}), + key.F10 => self.cmd("toggle_syntax_highlighting", .{}), key.LEFT => self.cmd("select_left", .{}), key.RIGHT => self.cmd("select_right", .{}), key.UP => self.cmd("select_up", .{}), @@ -559,6 +560,7 @@ const hints = tui.KeybindHints.initComptime(.{ .{ "toggle_inspector_view", "F5, C-F5, C-S-i" }, .{ "toggle_panel", "C-j, F11" }, .{ "toggle_whitespace_mode", "C-F10" }, + .{ "toggle_syntax_highlighting", "S-F10" }, .{ "to_lower", "A-l" }, .{ "to_upper", "A-u" }, .{ "undo", "C-z" },