feat: add support for CRLF EOL mode
This commit is contained in:
parent
9a633aa2a9
commit
593b202b16
8 changed files with 119 additions and 50 deletions
|
@ -206,6 +206,7 @@ pub const Editor = struct {
|
|||
whole_file: ?std.ArrayList(u8),
|
||||
bytes: usize = 0,
|
||||
chunks: usize = 0,
|
||||
eol_mode: Buffer.EolMode = .lf,
|
||||
} = null,
|
||||
matches: Match.List,
|
||||
match_token: usize = 0,
|
||||
|
@ -238,6 +239,7 @@ pub const Editor = struct {
|
|||
matches: usize = 0,
|
||||
cursels: usize = 0,
|
||||
dirty: bool = false,
|
||||
eol_mode: Buffer.EolMode = .lf,
|
||||
} = .{},
|
||||
|
||||
syntax: ?*syntax = null,
|
||||
|
@ -357,6 +359,10 @@ pub const Editor = struct {
|
|||
return if (self.buffer) |p| p.root else error.Stop;
|
||||
}
|
||||
|
||||
fn buf_eol_mode(self: *const Self) !Buffer.EolMode {
|
||||
return if (self.buffer) |p| p.file_eol_mode else error.Stop;
|
||||
}
|
||||
|
||||
fn buf_a(self: *const Self) !Allocator {
|
||||
return if (self.buffer) |p| p.allocator else error.Stop;
|
||||
}
|
||||
|
@ -406,7 +412,7 @@ pub const Editor = struct {
|
|||
const lang_override = tp.env.get().str("language");
|
||||
var content = std.ArrayList(u8).init(self.allocator);
|
||||
defer content.deinit();
|
||||
try new_buf.root.store(content.writer());
|
||||
try new_buf.root.store(content.writer(), new_buf.file_eol_mode);
|
||||
const syn = if (lang_override.len > 0)
|
||||
syntax.create_file_type(self.allocator, content.items, lang_override) catch null
|
||||
else
|
||||
|
@ -508,6 +514,11 @@ pub const Editor = struct {
|
|||
}
|
||||
|
||||
fn update_buf(self: *Self, root: Buffer.Root) !void {
|
||||
const b = self.buffer orelse return error.Stop;
|
||||
return self.update_buf_and_eol_mode(root, b.file_eol_mode);
|
||||
}
|
||||
|
||||
fn update_buf_and_eol_mode(self: *Self, root: Buffer.Root, eol_mode: Buffer.EolMode) !void {
|
||||
const b = self.buffer orelse return error.Stop;
|
||||
var sfa = std.heap.stackFallback(512, self.allocator);
|
||||
const allocator = sfa.get();
|
||||
|
@ -515,6 +526,7 @@ pub const Editor = struct {
|
|||
defer allocator.free(meta);
|
||||
try b.store_undo(meta);
|
||||
b.update(root);
|
||||
b.file_eol_mode = eol_mode;
|
||||
try self.send_editor_modified();
|
||||
}
|
||||
|
||||
|
@ -1114,11 +1126,15 @@ pub const Editor = struct {
|
|||
const dirty = if (self.buffer) |buf| buf.is_dirty() else false;
|
||||
|
||||
const root: ?Buffer.Root = self.buf_root() catch null;
|
||||
const eol_mode = self.buf_eol_mode() catch .lf;
|
||||
if (token_from(self.last.root) != token_from(root)) {
|
||||
try self.send_editor_update(self.last.root, root);
|
||||
try self.send_editor_update(self.last.root, root, eol_mode);
|
||||
self.lsp_version += 1;
|
||||
}
|
||||
|
||||
if (self.last.eol_mode != eol_mode)
|
||||
try self.send_editor_eol_mode(eol_mode);
|
||||
|
||||
if (self.last.dirty != dirty)
|
||||
try self.send_editor_dirty(dirty);
|
||||
|
||||
|
@ -1227,10 +1243,14 @@ pub const Editor = struct {
|
|||
return if (p) |p_| @intFromPtr(p_) else 0;
|
||||
}
|
||||
|
||||
fn send_editor_update(self: *const Self, old_root: ?Buffer.Root, new_root: ?Buffer.Root) !void {
|
||||
_ = try self.handlers.msg(.{ "E", "update", token_from(new_root), token_from(old_root) });
|
||||
fn send_editor_update(self: *const Self, old_root: ?Buffer.Root, new_root: ?Buffer.Root, eol_mode: Buffer.EolMode) !void {
|
||||
_ = try self.handlers.msg(.{ "E", "update", token_from(new_root), token_from(old_root), @intFromEnum(eol_mode) });
|
||||
if (self.syntax) |_| if (self.file_path) |file_path| if (old_root != null and new_root != null)
|
||||
project_manager.did_change(file_path, self.lsp_version, token_from(new_root), token_from(old_root)) catch {};
|
||||
project_manager.did_change(file_path, self.lsp_version, token_from(new_root), token_from(old_root), eol_mode) catch {};
|
||||
}
|
||||
|
||||
fn send_editor_eol_mode(self: *const Self, eol_mode: Buffer.EolMode) !void {
|
||||
_ = try self.handlers.msg(.{ "E", "eol_mode", @intFromEnum(eol_mode) });
|
||||
}
|
||||
|
||||
fn clamp_abs(self: *Self, abs: bool) void {
|
||||
|
@ -1448,7 +1468,8 @@ pub const Editor = struct {
|
|||
match.nudge_insert(nudge);
|
||||
if (self.syntax) |syn| {
|
||||
const root = self.buf_root() catch return;
|
||||
const start_byte = root.get_byte_pos(nudge.begin, self.plane.metrics()) catch return;
|
||||
const eol_mode = self.buf_eol_mode() catch return;
|
||||
const start_byte = root.get_byte_pos(nudge.begin, self.plane.metrics(), eol_mode) catch return;
|
||||
syn.edit(.{
|
||||
.start_byte = @intCast(start_byte),
|
||||
.old_end_byte = @intCast(start_byte),
|
||||
|
@ -1472,7 +1493,8 @@ pub const Editor = struct {
|
|||
};
|
||||
if (self.syntax) |syn| {
|
||||
const root = self.buf_root() catch return;
|
||||
const start_byte = root.get_byte_pos(nudge.begin, self.plane.metrics()) catch return;
|
||||
const eol_mode = self.buf_eol_mode() catch return;
|
||||
const start_byte = root.get_byte_pos(nudge.begin, self.plane.metrics(), eol_mode) catch return;
|
||||
syn.edit(.{
|
||||
.start_byte = @intCast(start_byte),
|
||||
.old_end_byte = @intCast(start_byte + size),
|
||||
|
@ -3002,6 +3024,7 @@ pub const Editor = struct {
|
|||
const frame = tracy.initZone(@src(), .{ .name = "editor update syntax" });
|
||||
defer frame.deinit();
|
||||
const root = try self.buf_root();
|
||||
const eol_mode = try self.buf_eol_mode();
|
||||
const token = @intFromPtr(root);
|
||||
if (root.lines() > root_mod.max_syntax_lines)
|
||||
return;
|
||||
|
@ -3011,7 +3034,7 @@ pub const Editor = struct {
|
|||
if (self.syntax_refresh_full) {
|
||||
var content = std.ArrayList(u8).init(self.allocator);
|
||||
defer content.deinit();
|
||||
try root.store(content.writer());
|
||||
try root.store(content.writer(), eol_mode);
|
||||
try syn.refresh_full(content.items);
|
||||
self.syntax_refresh_full = false;
|
||||
} else {
|
||||
|
@ -3021,7 +3044,7 @@ pub const Editor = struct {
|
|||
} else {
|
||||
var content = std.ArrayList(u8).init(self.allocator);
|
||||
defer content.deinit();
|
||||
try root.store(content.writer());
|
||||
try root.store(content.writer(), eol_mode);
|
||||
self.syntax = if (tp.env.get().is("no-syntax"))
|
||||
null
|
||||
else
|
||||
|
@ -3759,7 +3782,7 @@ pub const Editor = struct {
|
|||
self.cancel_all_selections();
|
||||
self.cancel_all_matches();
|
||||
if (state.whole_file) |buf| {
|
||||
state.work_root = try b.load_from_string(buf.items);
|
||||
state.work_root = try b.load_from_string(buf.items, &state.eol_mode);
|
||||
state.bytes = buf.items.len;
|
||||
state.chunks = 1;
|
||||
primary.cursor = state.old_primary.cursor;
|
||||
|
@ -3770,7 +3793,7 @@ pub const Editor = struct {
|
|||
if (state.old_primary_reversed) sel.reverse();
|
||||
primary.cursor = sel.end;
|
||||
}
|
||||
try self.update_buf(state.work_root);
|
||||
try self.update_buf_and_eol_mode(state.work_root, state.eol_mode);
|
||||
primary.cursor.clamp_to_buffer(state.work_root, self.plane.metrics());
|
||||
self.logger.print("filter: done (bytes:{d} chunks:{d})", .{ state.bytes, state.chunks });
|
||||
self.reset_syntax();
|
||||
|
@ -3844,6 +3867,14 @@ pub const Editor = struct {
|
|||
self.clamp();
|
||||
}
|
||||
pub const to_lower_meta = .{ .description = "Convert selection or word to lower case" };
|
||||
|
||||
pub fn toggle_eol_mode(self: *Self, _: Context) Result {
|
||||
if (self.buffer) |b| b.file_eol_mode = switch (b.file_eol_mode) {
|
||||
.lf => .crlf,
|
||||
.crlf => .lf,
|
||||
};
|
||||
}
|
||||
pub const toggle_eol_mode_meta = .{ .description = "Toggle end of line sequence" };
|
||||
};
|
||||
|
||||
pub fn create(allocator: Allocator, parent: Widget) !Widget {
|
||||
|
|
|
@ -306,7 +306,8 @@ fn diff_update(self: *Self) !void {
|
|||
const editor = self.editor;
|
||||
const new = editor.get_current_root() orelse return;
|
||||
const old = if (editor.buffer) |buffer| buffer.last_save orelse return else return;
|
||||
return self.diff.diff(diff_result, new, old);
|
||||
const eol_mode = if (editor.buffer) |buffer| buffer.file_eol_mode else return;
|
||||
return self.diff.diff(diff_result, new, old, eol_mode);
|
||||
}
|
||||
|
||||
fn diff_result(from: tp.pid_ref, edits: []diff.Edit) void {
|
||||
|
|
|
@ -2,6 +2,7 @@ const std = @import("std");
|
|||
const Allocator = std.mem.Allocator;
|
||||
const tp = @import("thespian");
|
||||
const tracy = @import("tracy");
|
||||
const Buffer = @import("Buffer");
|
||||
const root = @import("root");
|
||||
|
||||
const Plane = @import("renderer").Plane;
|
||||
|
@ -29,6 +30,7 @@ file_exists: bool,
|
|||
file_dirty: bool = false,
|
||||
detailed: bool = false,
|
||||
file: bool = false,
|
||||
eol_mode: Buffer.EolMode = .lf,
|
||||
|
||||
const project_icon = "";
|
||||
const Self = @This();
|
||||
|
@ -134,11 +136,16 @@ fn render_detailed(self: *Self, plane: *Plane, theme: *const Widget.Theme) void
|
|||
const project_name = tp.env.get().str("project");
|
||||
_ = plane.print("{s} ({s})", .{ self.name, project_name }) catch {};
|
||||
} else {
|
||||
const eol_mode = switch (self.eol_mode) {
|
||||
.lf => " [↩ = ␊]",
|
||||
.crlf => " [↩ = ␍␊]",
|
||||
};
|
||||
|
||||
_ = plane.putstr(if (!self.file_exists) "" else if (self.file_dirty) "" else "") catch {};
|
||||
_ = plane.print(" {s}:{d}:{d}", .{ self.name, self.line + 1, self.column + 1 }) catch {};
|
||||
_ = plane.print(" of {d} lines", .{self.lines}) catch {};
|
||||
if (self.file_type.len > 0)
|
||||
_ = plane.print(" ({s})", .{self.file_type}) catch {};
|
||||
_ = plane.print(" ({s}){s}", .{ self.file_type, eol_mode }) catch {};
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
@ -169,10 +176,13 @@ pub fn receive(self: *Self, _: *Button.State(Self), _: tp.pid_ref, m: tp.message
|
|||
var file_type: []const u8 = undefined;
|
||||
var file_icon: []const u8 = undefined;
|
||||
var file_dirty: bool = undefined;
|
||||
var eol_mode: Buffer.EolModeTag = @intFromEnum(Buffer.EolMode.lf);
|
||||
if (try m.match(.{ "E", "pos", tp.extract(&self.lines), tp.extract(&self.line), tp.extract(&self.column) }))
|
||||
return false;
|
||||
if (try m.match(.{ "E", "dirty", tp.extract(&file_dirty) })) {
|
||||
self.file_dirty = file_dirty;
|
||||
} else if (try m.match(.{ "E", "eol_mode", tp.extract(&eol_mode) })) {
|
||||
self.eol_mode = @enumFromInt(eol_mode);
|
||||
} else if (try m.match(.{ "E", "save", tp.extract(&file_path) })) {
|
||||
@memcpy(self.name_buf[0..file_path.len], file_path);
|
||||
self.name = self.name_buf[0..file_path.len];
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
const tp = @import("thespian");
|
||||
const Buffer = @import("Buffer");
|
||||
|
||||
const Plane = @import("renderer").Plane;
|
||||
|
||||
|
@ -13,6 +14,7 @@ lines: usize = 0,
|
|||
column: usize = 0,
|
||||
buf: [256]u8 = undefined,
|
||||
rendered: [:0]const u8 = "",
|
||||
eol_mode: Buffer.EolMode = .lf,
|
||||
|
||||
const Self = @This();
|
||||
|
||||
|
@ -47,14 +49,21 @@ pub fn render(self: *Self, btn: *Button.State(Self), theme: *const Widget.Theme)
|
|||
fn format(self: *Self) void {
|
||||
var fbs = std.io.fixedBufferStream(&self.buf);
|
||||
const writer = fbs.writer();
|
||||
std.fmt.format(writer, " Ln {d}, Col {d} ", .{ self.line + 1, self.column + 1 }) catch {};
|
||||
const eol_mode = switch (self.eol_mode) {
|
||||
.lf => "",
|
||||
.crlf => " [␍␊]",
|
||||
};
|
||||
std.fmt.format(writer, "{s} Ln {d}, Col {d} ", .{ eol_mode, self.line + 1, self.column + 1 }) catch {};
|
||||
self.rendered = @ptrCast(fbs.getWritten());
|
||||
self.buf[self.rendered.len] = 0;
|
||||
}
|
||||
|
||||
pub fn receive(self: *Self, _: *Button.State(Self), _: tp.pid_ref, m: tp.message) error{Exit}!bool {
|
||||
var eol_mode: Buffer.EolModeTag = @intFromEnum(Buffer.EolMode.lf);
|
||||
if (try m.match(.{ "E", "pos", tp.extract(&self.lines), tp.extract(&self.line), tp.extract(&self.column) })) {
|
||||
self.format();
|
||||
} else if (try m.match(.{ "E", "eol_mode", tp.extract(&eol_mode) })) {
|
||||
self.eol_mode = @enumFromInt(eol_mode);
|
||||
} else if (try m.match(.{ "E", "close" })) {
|
||||
self.lines = 0;
|
||||
self.line = 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue