Compare commits

...

3 commits

3 changed files with 53 additions and 17 deletions

View file

@ -1184,6 +1184,13 @@ pub fn load_from_string_and_update(self: *Self, file_path: []const u8, s: []cons
self.mtime = std.time.milliTimestamp();
}
pub fn reset_from_string_and_update(self: *Self, s: []const u8) LoadFromStringError!void {
self.root = try self.load_from_string(s, &self.file_eol_mode, &self.file_utf8_sanitized);
self.last_save = self.root;
self.last_save_eol_mode = self.file_eol_mode;
self.mtime = std.time.milliTimestamp();
}
pub const LoadFromFileError = error{
OutOfMemory,
Unexpected,
@ -1367,6 +1374,10 @@ pub fn mark_clean(self: *Self) void {
self.last_save = self.root;
}
pub fn mark_dirty(self: *Self) void {
self.last_save = null;
}
pub fn is_hidden(self: *const Self) bool {
return self.hidden;
}

View file

@ -722,15 +722,6 @@ pub const Editor = struct {
self.update_event() catch {};
}
fn save_as(self: *Self, file_path: []const u8) !void {
if (self.buffer) |b_mut| try b_mut.store_to_file_and_clean(file_path);
if (self.file_path) |old_file_path| self.allocator.free(old_file_path);
self.file_path = try self.allocator.dupe(u8, file_path);
try self.send_editor_save(self.file_path.?);
self.last.dirty = false;
self.update_event() catch {};
}
pub fn push_cursor(self: *Self) !void {
const primary = self.cursels.getLastOrNull() orelse CurSel{} orelse CurSel{};
(try self.cursels.addOne(self.allocator)).* = primary;
@ -4950,14 +4941,6 @@ pub const Editor = struct {
}
pub const save_file_without_formatting_meta: Meta = .{ .description = "Save file without formatting" };
pub fn save_file_as(self: *Self, ctx: Context) Result {
var file_path: []const u8 = undefined;
if (ctx.args.match(.{tp.extract(&file_path)}) catch false) {
try self.save_as(file_path);
} else return error.InvalidSafeFileAsArgument;
}
pub const save_file_as_meta: Meta = .{ .arguments = &.{.string} };
pub fn close_file(self: *Self, _: Context) Result {
const buffer_ = self.buffer;
if (buffer_) |buffer| if (buffer.is_dirty())

View file

@ -564,6 +564,48 @@ const cmds = struct {
}
pub const create_new_file_meta: Meta = .{ .description = "New file" };
pub fn save_file_as(self: *Self, ctx: Ctx) Result {
var file_path: []const u8 = undefined;
if (!(ctx.args.match(.{tp.extract(&file_path)}) catch false))
return error.InvalidSafeFileAsArgument;
if (self.get_active_editor()) |editor| {
const buffer = editor.buffer orelse return;
var content = std.ArrayListUnmanaged(u8).empty;
defer content.deinit(self.allocator);
try buffer.root.store(content.writer(self.allocator), buffer.file_eol_mode);
var existing = false;
if (self.buffer_manager.get_buffer_for_file(file_path)) |new_buffer| {
if (new_buffer.is_dirty())
return tp.exit("save as would overwrite unsaved changes");
if (buffer == new_buffer)
return tp.exit("same file");
existing = true;
}
try self.create_editor();
try command.executeName("open_scratch_buffer", command.fmt(.{
file_path,
"",
buffer.file_type_name,
}));
if (self.get_active_editor()) |new_editor| {
const new_buffer = new_editor.buffer orelse return;
if (existing) new_editor.update_buf(new_buffer.root) catch {}; // store an undo point
try new_buffer.reset_from_string_and_update(content.items);
new_buffer.mark_not_ephemeral();
new_buffer.mark_dirty();
new_editor.clamp();
new_editor.update_buf(new_buffer.root) catch {};
tui.need_render();
}
try command.executeName("save_file", .{});
if (buffer.is_ephemeral() and !buffer.is_dirty())
_ = self.buffer_manager.close_buffer(buffer);
}
}
pub const save_file_as_meta: Meta = .{ .arguments = &.{.string} };
pub fn delete_buffer(self: *Self, ctx: Ctx) Result {
var file_path: []const u8 = undefined;
if (!(ctx.args.match(.{tp.extract(&file_path)}) catch false))