Merge branch 'master' into zig-0.14

This commit is contained in:
CJ van den Berg 2025-02-11 13:32:11 +01:00
commit 0cd48c5eaa
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
6 changed files with 97 additions and 36 deletions

View file

@ -50,6 +50,10 @@ curr_history: ?*UndoNode = null,
mtime: i64,
utime: i64,
file_type_name: ?[]const u8 = null,
file_type_icon: ?[]const u8 = null,
file_type_color: ?u24 = null,
pub const EolMode = enum { lf, crlf };
pub const EolModeTag = @typeInfo(EolMode).@"enum".tag_type;
@ -58,6 +62,7 @@ const UndoNode = struct {
next: ?*UndoNode = null,
branches: ?*UndoBranch = null,
meta: []const u8,
file_eol_mode: EolMode,
};
const UndoBranch = struct {
@ -1247,6 +1252,7 @@ pub fn reset_to_last_saved(self: *Self) void {
if (self.last_save) |last_save| {
self.store_undo(&[_]u8{}) catch {};
self.root = last_save;
self.file_eol_mode = self.last_save_eol_mode;
self.mtime = std.time.milliTimestamp();
}
}
@ -1385,6 +1391,7 @@ fn create_undo(self: *const Self, root: Root, meta_: []const u8) error{OutOfMemo
h.* = UndoNode{
.root = root,
.meta = meta,
.file_eol_mode = self.file_eol_mode,
};
return h;
}
@ -1420,6 +1427,7 @@ pub fn undo(self: *Self, meta: []const u8) error{Stop}![]const u8 {
self.undo_history = h.next;
self.curr_history = h;
self.root = h.root;
self.file_eol_mode = h.file_eol_mode;
self.push_redo(r);
self.mtime = std.time.milliTimestamp();
return h.meta;
@ -1432,6 +1440,7 @@ pub fn redo(self: *Self) error{Stop}![]const u8 {
self.redo_history = h.next;
self.curr_history = h;
self.root = h.root;
self.file_eol_mode = h.file_eol_mode;
self.push_undo(u);
self.mtime = std.time.milliTimestamp();
return h.meta;

View file

@ -542,13 +542,14 @@ pub const Editor = struct {
return self.open_buffer(file_path, try self.buffer_manager.open_scratch(file_path, content), file_type);
}
fn open_buffer(self: *Self, file_path: []const u8, new_buf: *Buffer, file_type: ?[]const u8) !void {
fn open_buffer(self: *Self, file_path: []const u8, new_buf: *Buffer, file_type_: ?[]const u8) !void {
errdefer self.buffer_manager.retire(new_buf, null);
self.cancel_all_selections();
self.get_primary().reset();
self.file_path = try self.allocator.dupe(u8, file_path);
if (self.buffer) |_| try self.close();
self.buffer = new_buf;
const file_type = file_type_ orelse new_buf.file_type_name;
if (new_buf.root.lines() > root_mod.max_syntax_lines) {
self.logger.print("large file threshold {d} lines < file size {d} lines", .{
@ -584,6 +585,11 @@ pub const Editor = struct {
const ftn = if (self.syntax) |syn| syn.file_type.name else "text";
const fti = if (self.syntax) |syn| syn.file_type.icon else "🖹";
const ftc = if (self.syntax) |syn| syn.file_type.color else 0x000000;
if (self.buffer) |buffer| {
buffer.file_type_name = ftn;
buffer.file_type_icon = fti;
buffer.file_type_color = ftc;
}
if (self.buffer) |buffer| if (buffer.get_meta()) |meta|
try self.extract_state(meta, .none);

View file

@ -7,6 +7,7 @@ const command = @import("command");
const tui = @import("../../tui.zig");
pub const Type = @import("palette.zig").Create(@This());
const module_name = @typeName(@This());
const Widget = @import("../../Widget.zig");
pub const label = "Switch buffers";
pub const name = " buffer";
@ -16,7 +17,9 @@ const hidden_indicator = "-";
pub const Entry = struct {
label: []const u8,
hint: []const u8,
icon: []const u8,
color: ?u24,
indicator: []const u8,
};
pub fn load_entries(palette: *Type) !usize {
@ -24,15 +27,20 @@ pub fn load_entries(palette: *Type) !usize {
const buffers = try buffer_manager.list_most_recently_used(palette.allocator);
defer palette.allocator.free(buffers);
for (buffers) |buffer| {
const hint = if (buffer.is_dirty())
const indicator = if (buffer.is_dirty())
dirty_indicator
else if (buffer.is_hidden())
hidden_indicator
else
"";
(try palette.entries.addOne()).* = .{ .label = buffer.file_path, .hint = hint };
(try palette.entries.addOne()).* = .{
.label = buffer.file_path,
.icon = buffer.file_type_icon orelse "",
.color = buffer.file_type_color,
.indicator = indicator,
};
}
return if (palette.entries.items.len == 0) label.len else 2;
return if (palette.entries.items.len == 0) label.len else 4;
}
pub fn clear_entries(palette: *Type) void {
@ -44,12 +52,58 @@ pub fn add_menu_entry(palette: *Type, entry: *Entry, matches: ?[]const usize) !v
defer value.deinit();
const writer = value.writer();
try cbor.writeValue(writer, entry.label);
try cbor.writeValue(writer, entry.hint);
try cbor.writeValue(writer, entry.icon);
try cbor.writeValue(writer, entry.color);
try cbor.writeValue(writer, entry.indicator);
try cbor.writeValue(writer, matches orelse &[_]usize{});
try palette.menu.add_item_with_handler(value.items, select);
palette.items += 1;
}
pub fn on_render_menu(_: *Type, button: *Type.ButtonState, theme: *const Widget.Theme, selected: bool) bool {
const style_base = theme.editor_widget;
const style_label = if (button.active) theme.editor_cursor else if (button.hover or selected) theme.editor_selection else theme.editor_widget;
const style_hint = if (tui.find_scope_style(theme, "entity.name")) |sty| sty.style else style_label;
button.plane.set_base_style(style_base);
button.plane.erase();
button.plane.home();
button.plane.set_style(style_label);
if (button.active or button.hover or selected) {
button.plane.fill(" ");
button.plane.home();
}
button.plane.set_style(style_hint);
const pointer = if (selected) "" else " ";
_ = button.plane.print("{s}", .{pointer}) catch {};
var iter = button.opts.label;
var file_path_: []const u8 = undefined;
var icon: []const u8 = undefined;
var color: u24 = undefined;
if (!(cbor.matchString(&iter, &file_path_) catch false)) @panic("invalid buffer file path");
if (!(cbor.matchString(&iter, &icon) catch false)) @panic("invalid buffer file type icon");
if (!(cbor.matchInt(u24, &iter, &color) catch false)) @panic("invalid buffer file type color");
tui.render_file_icon(&button.plane, icon, color);
button.plane.set_style(style_label);
_ = button.plane.print(" {s} ", .{file_path_}) catch {};
var indicator: []const u8 = undefined;
if (!(cbor.matchString(&iter, &indicator) catch false))
indicator = "";
button.plane.set_style(style_hint);
_ = button.plane.print_aligned_right(0, "{s} ", .{indicator}) catch {};
var index: usize = 0;
var len = cbor.decodeArrayHeader(&iter) catch return false;
while (len > 0) : (len -= 1) {
if (cbor.matchValue(&iter, cbor.extract(&index)) catch break) {
tui.render_match_cell(&button.plane, 0, index + 4, theme) catch break;
} else break;
}
return false;
}
fn select(menu: **Type.MenuState, button: *Type.ButtonState) void {
var file_path: []const u8 = undefined;
var iter = button.opts.label;

View file

@ -90,7 +90,7 @@ pub fn on_render_menu(_: *Type, button: *Type.ButtonState, theme: *const Widget.
if (!(cbor.matchString(&iter, &description_) catch false)) @panic("invalid file_type description");
if (!(cbor.matchString(&iter, &icon) catch false)) @panic("invalid file_type icon");
if (!(cbor.matchInt(u24, &iter, &color) catch false)) @panic("invalid file_type color");
render_file_icon(button, icon, color);
tui.render_file_icon(&button.plane, icon, color);
button.plane.set_style(style_label);
_ = button.plane.print(" {s} ", .{description_}) catch {};
@ -104,31 +104,12 @@ pub fn on_render_menu(_: *Type, button: *Type.ButtonState, theme: *const Widget.
var len = cbor.decodeArrayHeader(&iter) catch return false;
while (len > 0) : (len -= 1) {
if (cbor.matchValue(&iter, cbor.extract(&index)) catch break) {
render_match_cell(button, 0, index + 4, theme) catch break;
tui.render_match_cell(&button.plane, 0, index + 4, theme) catch break;
} else break;
}
return false;
}
fn render_match_cell(button: *Type.ButtonState, y: usize, x: usize, theme: *const Widget.Theme) !void {
button.plane.cursor_move_yx(@intCast(y), @intCast(x)) catch return;
var cell = button.plane.cell_init();
_ = button.plane.at_cursor_cell(&cell) catch return;
cell.set_style(theme.editor_match);
_ = button.plane.putc(&cell) catch {};
}
fn render_file_icon(button: *Type.ButtonState, icon: []const u8, color: u24) void {
var cell = button.plane.cell_init();
_ = button.plane.at_cursor_cell(&cell) catch return;
if (!(color == 0xFFFFFF or color == 0x000000 or color == 0x000001)) {
cell.set_fg_rgb(@intCast(color)) catch {};
}
_ = button.plane.cell_load(&cell, icon) catch {};
_ = button.plane.putc(&cell) catch {};
button.plane.cursor_move_rel(0, 1) catch {};
}
fn select(menu: **Type.MenuState, button: *Type.ButtonState) void {
var description_: []const u8 = undefined;
var icon: []const u8 = undefined;

View file

@ -139,20 +139,12 @@ pub fn Create(options: type) type {
var len = cbor.decodeArrayHeader(&iter) catch return false;
while (len > 0) : (len -= 1) {
if (cbor.matchValue(&iter, cbor.extract(&index)) catch break) {
render_cell(&button.plane, 0, index + 1, theme.editor_match) catch break;
tui.render_match_cell(&button.plane, 0, index + 1, theme) catch break;
} else break;
}
return false;
}
fn render_cell(plane: *Plane, y: usize, x: usize, style: Widget.Theme.Style) !void {
plane.cursor_move_yx(@intCast(y), @intCast(x)) catch return;
var cell = plane.cell_init();
_ = plane.at_cursor_cell(&cell) catch return;
cell.set_style(style);
_ = plane.putc(&cell) catch {};
}
fn on_resize_menu(self: *Self, _: *Menu.State(*Self), _: Widget.Box) void {
self.do_resize();
// self.start_query(0) catch {};

View file

@ -1289,3 +1289,22 @@ pub fn message(comptime fmt: anytype, args: anytype) void {
var buf: [256]u8 = undefined;
tp.self_pid().send(.{ "message", std.fmt.bufPrint(&buf, fmt, args) catch @panic("too large") }) catch {};
}
pub fn render_file_icon(self: *renderer.Plane, icon: []const u8, color: u24) void { var cell = self.cell_init();
_ = self.at_cursor_cell(&cell) catch return;
if (!(color == 0xFFFFFF or color == 0x000000 or color == 0x000001)) {
cell.set_fg_rgb(@intCast(color)) catch {};
}
_ = self.cell_load(&cell, icon) catch {};
_ = self.putc(&cell) catch {};
self.cursor_move_rel(0, 1) catch {};
}
pub fn render_match_cell(self: *renderer.Plane, y: usize, x: usize, theme_: *const Widget.Theme) !void {
self.cursor_move_yx(@intCast(y), @intCast(x)) catch return;
var cell = self.cell_init();
_ = self.at_cursor_cell(&cell) catch return;
cell.set_style(theme_.editor_match);
_ = self.putc(&cell) catch {};
}