feat: let mini modes inherit gutter line number modes

This commit is contained in:
CJ van den Berg 2025-02-20 20:51:09 +01:00
parent eb569157a8
commit ec3bba2aff
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 30 additions and 12 deletions

View file

@ -78,7 +78,7 @@ pub const Mode = struct {
mode: []const u8, mode: []const u8,
name: []const u8 = "", name: []const u8 = "",
line_numbers: LineNumbers = .absolute, line_numbers: LineNumbers = .inherit,
keybind_hints: *const KeybindHints, keybind_hints: *const KeybindHints,
cursor_shape: ?CursorShape = null, cursor_shape: ?CursorShape = null,
selection_style: SelectionStyle, selection_style: SelectionStyle,
@ -365,7 +365,7 @@ const BindingSet = struct {
syntax: KeySyntax = .flow, syntax: KeySyntax = .flow,
on_match_failure: OnMatchFailure = .ignore, on_match_failure: OnMatchFailure = .ignore,
name: []const u8, name: []const u8,
line_numbers: LineNumbers = .absolute, line_numbers: LineNumbers = .inherit,
cursor_shape: ?CursorShape = null, cursor_shape: ?CursorShape = null,
selection_style: SelectionStyle, selection_style: SelectionStyle,
insert_command: []const u8 = "", insert_command: []const u8 = "",
@ -383,7 +383,7 @@ const BindingSet = struct {
syntax: KeySyntax = .flow, syntax: KeySyntax = .flow,
on_match_failure: OnMatchFailure = .insert, on_match_failure: OnMatchFailure = .insert,
name: ?[]const u8 = null, name: ?[]const u8 = null,
line_numbers: LineNumbers = .absolute, line_numbers: LineNumbers = .inherit,
cursor: ?CursorShape = null, cursor: ?CursorShape = null,
inherit: ?[]const u8 = null, inherit: ?[]const u8 = null,
selection: ?SelectionStyle = null, selection: ?SelectionStyle = null,
@ -663,6 +663,7 @@ const BindingSet = struct {
}; };
pub const LineNumbers = enum { pub const LineNumbers = enum {
inherit,
absolute, absolute,
relative, relative,
}; };

View file

@ -27,7 +27,7 @@ view_rows: u32 = 1,
view_top: u32 = 1, view_top: u32 = 1,
line: usize = 0, line: usize = 0,
linenum: bool, linenum: bool,
relative: bool, mode: ?LineNumberMode = null,
render_style: DigitStyle, render_style: DigitStyle,
highlight: bool, highlight: bool,
symbols: bool, symbols: bool,
@ -48,7 +48,7 @@ pub fn create(allocator: Allocator, parent: Widget, event_source: Widget, editor
.plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent.plane.*), .plane = try Plane.init(&(Widget.Box{}).opts(@typeName(Self)), parent.plane.*),
.parent = parent, .parent = parent,
.linenum = tui.config().gutter_line_numbers, .linenum = tui.config().gutter_line_numbers,
.relative = tui.config().gutter_line_numbers_relative, .mode = if (tui.config().gutter_line_numbers_relative) .relative else null,
.render_style = tui.config().gutter_line_numbers_style, .render_style = tui.config().gutter_line_numbers_style,
.highlight = tui.config().highlight_current_line_gutter, .highlight = tui.config().highlight_current_line_gutter,
.symbols = tui.config().gutter_symbols, .symbols = tui.config().gutter_symbols,
@ -113,7 +113,7 @@ pub fn receive(self: *Self, _: tp.pid_ref, m: tp.message) error{Exit}!bool {
fn update_width(self: *Self) void { fn update_width(self: *Self) void {
if (!self.linenum) return; if (!self.linenum) return;
const width = int_width(self.lines); const width = int_width(self.lines);
self.width = if (self.relative and width > 4) 4 else @max(width, 2); self.width = if (self.mode == .relative and width > 4) 4 else @max(width, 2);
self.width += if (self.symbols) 3 else 1; self.width += if (self.symbols) 3 else 1;
} }
@ -125,6 +125,24 @@ inline fn get_width(self: *Self) usize {
return if (self.linenum) self.width else if (self.symbols) 3 else 1; return if (self.linenum) self.width else if (self.symbols) 3 else 1;
} }
fn get_numbering_mode(self: *const Self) LineNumberMode {
return self.mode orelse switch (if (tui.input_mode()) |mode| mode.line_numbers else .absolute) {
.relative => .relative,
.inherit => if (tui.input_mode_outer()) |mode| from_mode_enum(mode.line_numbers) else .absolute,
.absolute => .absolute,
};
}
const LineNumberMode = enum { relative, absolute };
fn from_mode_enum(mode: anytype) LineNumberMode {
return switch (mode) {
.relative => .relative,
.inherit => .absolute,
.absolute => .absolute,
};
}
pub fn render(self: *Self, theme: *const Widget.Theme) bool { pub fn render(self: *Self, theme: *const Widget.Theme) bool {
const frame = tracy.initZone(@src(), .{ .name = "gutter render" }); const frame = tracy.initZone(@src(), .{ .name = "gutter render" });
defer frame.deinit(); defer frame.deinit();
@ -134,11 +152,10 @@ pub fn render(self: *Self, theme: *const Widget.Theme) bool {
self.plane.set_style(theme.editor_gutter); self.plane.set_style(theme.editor_gutter);
_ = self.plane.fill(" "); _ = self.plane.fill(" ");
if (self.linenum) { if (self.linenum) {
const relative = self.relative or if (tui.input_mode()) |mode| mode.line_numbers == .relative else false; switch (self.get_numbering_mode()) {
if (relative) .relative => self.render_relative(theme),
self.render_relative(theme) .absolute => self.render_linear(theme),
else }
self.render_linear(theme);
} else { } else {
self.render_none(theme); self.render_none(theme);
} }

View file

@ -590,7 +590,7 @@ const cmds = struct {
if (self.widgets.get("editor_gutter")) |gutter_widget| { if (self.widgets.get("editor_gutter")) |gutter_widget| {
const gutter = gutter_widget.dynamic_cast(@import("editor_gutter.zig")) orelse return; const gutter = gutter_widget.dynamic_cast(@import("editor_gutter.zig")) orelse return;
gutter.linenum = ln; gutter.linenum = ln;
gutter.relative = lnr; gutter.mode = if(lnr) .relative else null;
} }
} }
pub const gutter_mode_next_meta = .{ .description = "Next gutter mode" }; pub const gutter_mode_next_meta = .{ .description = "Next gutter mode" };