feat(buffer): restore previous file type when switching buffers

This commit is contained in:
CJ van den Berg 2025-02-11 11:31:16 +01:00
parent f627a59f4d
commit 02ba05c500
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 27 additions and 18 deletions

View file

@ -50,6 +50,10 @@ curr_history: ?*UndoNode = null,
mtime: i64, mtime: i64,
utime: 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 EolMode = enum { lf, crlf };
pub const EolModeTag = @typeInfo(EolMode).Enum.tag_type; pub const EolModeTag = @typeInfo(EolMode).Enum.tag_type;

View file

@ -541,13 +541,14 @@ pub const Editor = struct {
return self.open_buffer(file_path, try self.buffer_manager.open_scratch(file_path, content), file_type); 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); errdefer self.buffer_manager.retire(new_buf, null);
self.cancel_all_selections(); self.cancel_all_selections();
self.get_primary().reset(); self.get_primary().reset();
self.file_path = try self.allocator.dupe(u8, file_path); self.file_path = try self.allocator.dupe(u8, file_path);
if (self.buffer) |_| try self.close(); if (self.buffer) |_| try self.close();
self.buffer = new_buf; 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) { if (new_buf.root.lines() > root_mod.max_syntax_lines) {
self.logger.print("large file threshold {d} lines < file size {d} lines", .{ self.logger.print("large file threshold {d} lines < file size {d} lines", .{
@ -583,6 +584,11 @@ pub const Editor = struct {
const ftn = if (self.syntax) |syn| syn.file_type.name else "text"; const ftn = if (self.syntax) |syn| syn.file_type.name else "text";
const fti = if (self.syntax) |syn| syn.file_type.icon else "🖹"; const fti = if (self.syntax) |syn| syn.file_type.icon else "🖹";
const ftc = if (self.syntax) |syn| syn.file_type.color else 0x000000; 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| if (self.buffer) |buffer| if (buffer.get_meta()) |meta|
try self.extract_state(meta, .none); try self.extract_state(meta, .none);
@ -1949,12 +1955,12 @@ pub const Editor = struct {
} }
fn is_word_boundary_left_vim(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { fn is_word_boundary_left_vim(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool {
if(is_white_space_at_cursor(root, cursor, metrics)) return false; if (is_white_space_at_cursor(root, cursor, metrics)) return false;
var next = cursor.*; var next = cursor.*;
next.move_left(root, metrics) catch return true; next.move_left(root, metrics) catch return true;
const next_is_white_space = is_white_space_at_cursor(root, &next, metrics); const next_is_white_space = is_white_space_at_cursor(root, &next, metrics);
if(next_is_white_space) return true; if (next_is_white_space) return true;
const curr_is_non_word = is_non_word_char_at_cursor(root, cursor, metrics); const curr_is_non_word = is_non_word_char_at_cursor(root, cursor, metrics);
const next_is_non_word = is_non_word_char_at_cursor(root, &next, metrics); const next_is_non_word = is_non_word_char_at_cursor(root, &next, metrics);
@ -1987,12 +1993,12 @@ pub const Editor = struct {
} }
fn is_word_boundary_right_vim(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool { fn is_word_boundary_right_vim(root: Buffer.Root, cursor: *const Cursor, metrics: Buffer.Metrics) bool {
if(is_white_space_at_cursor(root, cursor, metrics)) return false; if (is_white_space_at_cursor(root, cursor, metrics)) return false;
var next = cursor.*; var next = cursor.*;
next.move_right(root, metrics) catch return true; next.move_right(root, metrics) catch return true;
const next_is_white_space = is_white_space_at_cursor(root, &next, metrics); const next_is_white_space = is_white_space_at_cursor(root, &next, metrics);
if(next_is_white_space) return true; if (next_is_white_space) return true;
const curr_is_non_word = is_non_word_char_at_cursor(root, cursor, metrics); const curr_is_non_word = is_non_word_char_at_cursor(root, cursor, metrics);
const next_is_non_word = is_non_word_char_at_cursor(root, &next, metrics); const next_is_non_word = is_non_word_char_at_cursor(root, &next, metrics);
@ -2427,7 +2433,7 @@ pub const Editor = struct {
if (all_stop) if (all_stop)
return error.Stop; return error.Stop;
return .{text.items, root}; return .{ text.items, root };
} }
pub fn cut_internal_vim(self: *Self, _: Context) Result { pub fn cut_internal_vim(self: *Self, _: Context) Result {
@ -2632,10 +2638,10 @@ pub const Editor = struct {
const b = try self.buf_for_update(); const b = try self.buf_for_update();
var root = b.root; var root = b.root;
if(std.mem.eql(u8, text[text.len-1..], "\n")) text = text[0..text.len-1]; if (std.mem.eql(u8, text[text.len - 1 ..], "\n")) text = text[0 .. text.len - 1];
if (std.mem.indexOfScalar(u8, text, '\n')) |idx| { if (std.mem.indexOfScalar(u8, text, '\n')) |idx| {
if(idx == 0) { if (idx == 0) {
for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| { for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
try move_cursor_end(root, &cursel.cursor, self.metrics); try move_cursor_end(root, &cursel.cursor, self.metrics);
root = try self.insert(root, cursel, "\n", b.allocator); root = try self.insert(root, cursel, "\n", b.allocator);
@ -2677,7 +2683,7 @@ pub const Editor = struct {
pub fn cut_forward_internal(self: *Self, _: Context) Result { pub fn cut_forward_internal(self: *Self, _: Context) Result {
const b = try self.buf_for_update(); const b = try self.buf_for_update();
const text, const root= try self.cut_to(move_cursor_right, b.root); const text, const root = try self.cut_to(move_cursor_right, b.root);
self.set_clipboard_internal(text); self.set_clipboard_internal(text);
try self.update_buf(root); try self.update_buf(root);
self.clamp(); self.clamp();
@ -2702,7 +2708,7 @@ pub const Editor = struct {
pub fn cut_word_left_vim(self: *Self, _: Context) Result { pub fn cut_word_left_vim(self: *Self, _: Context) Result {
const b = try self.buf_for_update(); const b = try self.buf_for_update();
const text, const root= try self.cut_to(move_cursor_word_left_vim, b.root); const text, const root = try self.cut_to(move_cursor_word_left_vim, b.root);
self.set_clipboard_internal(text); self.set_clipboard_internal(text);
try self.update_buf(root); try self.update_buf(root);
self.clamp(); self.clamp();
@ -2719,7 +2725,7 @@ pub const Editor = struct {
pub fn cut_word_right_vim(self: *Self, _: Context) Result { pub fn cut_word_right_vim(self: *Self, _: Context) Result {
const b = try self.buf_for_update(); const b = try self.buf_for_update();
const text, const root= try self.cut_to(move_cursor_word_right_vim, b.root); const text, const root = try self.cut_to(move_cursor_word_right_vim, b.root);
self.set_clipboard_internal(text); self.set_clipboard_internal(text);
try self.update_buf(root); try self.update_buf(root);
self.clamp(); self.clamp();
@ -2870,7 +2876,6 @@ pub const Editor = struct {
} }
pub const move_word_left_vim_meta = .{ .description = "Move cursor left by word (vim)" }; pub const move_word_left_vim_meta = .{ .description = "Move cursor left by word (vim)" };
pub fn move_word_right(self: *Self, _: Context) Result { pub fn move_word_right(self: *Self, _: Context) Result {
const root = try self.buf_root(); const root = try self.buf_root();
self.with_cursors_const(root, move_cursor_word_right) catch {}; self.with_cursors_const(root, move_cursor_word_right) catch {};