feat: add support for {row}:{column} syntax in goto mini mode

This commit is contained in:
CJ van den Berg 2025-09-17 20:39:45 +02:00
parent 59921d8e10
commit 933126e2a0
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 58 additions and 24 deletions

View file

@ -2,11 +2,16 @@ const fmt = @import("std").fmt;
const command = @import("command"); const command = @import("command");
const tui = @import("../../tui.zig"); const tui = @import("../../tui.zig");
const Cursor = @import("../../editor.zig").Cursor;
pub const Type = @import("numeric_input.zig").Create(@This()); pub const Type = @import("numeric_input.zig").Create(@This());
pub const create = Type.create; pub const create = Type.create;
pub const ValueType = @import("../../editor.zig").Cursor; pub const ValueType = struct {
cursor: Cursor = .{},
part: enum { row, col } = .row,
};
pub const Separator = ':';
pub fn name(_: *Type) []const u8 { pub fn name(_: *Type) []const u8 {
return "goto"; return "goto";
@ -14,35 +19,64 @@ pub fn name(_: *Type) []const u8 {
pub fn start(_: *Type) ValueType { pub fn start(_: *Type) ValueType {
const editor = tui.get_active_editor() orelse return .{}; const editor = tui.get_active_editor() orelse return .{};
return editor.get_primary().cursor; return .{ .cursor = editor.get_primary().cursor };
} }
pub fn process_digit(self: *Type, digit: u8) void { pub fn process_digit(self: *Type, digit: u8) void {
switch (digit) { const part = if (self.input) |input| input.part else .row;
0 => { switch (part) {
if (self.input) |*x| x.row = x.row * 10; .row => switch (digit) {
0 => {
if (self.input) |*input| input.cursor.row = input.cursor.row * 10;
},
1...9 => {
if (self.input) |*input| {
input.cursor.row = input.cursor.row * 10 + digit;
} else {
self.input = .{ .cursor = .{ .row = digit } };
}
},
else => unreachable,
}, },
1...9 => { .col => if (self.input) |*input| {
if (self.input) |*x| { input.cursor.col = input.cursor.col * 10 + digit;
x.row = x.row * 10 + digit;
} else {
self.input = .{ .row = digit };
}
}, },
else => unreachable,
} }
} }
pub fn process_separator(self: *Type) void {
if (self.input) |*input| switch (input.part) {
.row => input.part = .col,
else => {},
};
}
pub fn delete(self: *Type, input: *ValueType) void { pub fn delete(self: *Type, input: *ValueType) void {
const newval = if (input.row < 10) 0 else input.row / 10; switch (input.part) {
if (newval == 0) self.input = null else input.row = newval; .row => {
const newval = if (input.cursor.row < 10) 0 else input.cursor.row / 10;
if (newval == 0) self.input = null else input.cursor.row = newval;
},
.col => {
const newval = if (input.cursor.col < 10) 0 else input.cursor.col / 10;
if (newval == 0) {
input.part = .row;
input.cursor.col = 0;
} else input.cursor.col = newval;
},
}
} }
pub fn format_value(_: *Type, input: ?ValueType, buf: []u8) []const u8 { pub fn format_value(_: *Type, input: ?ValueType, buf: []u8) []const u8 {
return if (input) |value| return if (input) |value| blk: {
(fmt.bufPrint(buf, "{d}", .{value.row}) catch "") switch (value.part) {
else .row => break :blk fmt.bufPrint(buf, "{d}", .{value.cursor.row}) catch "",
""; .col => if (value.cursor.col == 0)
break :blk fmt.bufPrint(buf, "{d}:", .{value.cursor.row}) catch ""
else
break :blk fmt.bufPrint(buf, "{d}:{d}", .{ value.cursor.row, value.cursor.col }) catch "",
}
} else "";
} }
pub const preview = goto; pub const preview = goto;
@ -50,9 +84,9 @@ pub const apply = goto;
pub const cancel = goto; pub const cancel = goto;
fn goto(self: *Type, _: command.Context) void { fn goto(self: *Type, _: command.Context) void {
if (self.input) |input| { send_goto(if (self.input) |input| input.cursor else self.start.cursor);
command.executeName("goto_line", command.fmt(.{input.row})) catch {}; }
} else {
command.executeName("goto_line_and_column", command.fmt(.{ self.start.row, self.start.col })) catch {}; fn send_goto(cursor: Cursor) void {
} command.executeName("goto_line_and_column", command.fmt(.{ cursor.row, cursor.col })) catch {};
} }

View file

@ -73,7 +73,7 @@ pub fn Create(options: type) type {
const process_digit_ = if (@hasDecl(options, "process_digit")) options.process_digit else process_digit; const process_digit_ = if (@hasDecl(options, "process_digit")) options.process_digit else process_digit;
if (@hasDecl(options, "Separator")) { if (@hasDecl(options, "Separator")) {
switch (char) { switch (char) {
'0'...'9' => process_digit_(@intCast(char - '0')), '0'...'9' => process_digit_(self, @intCast(char - '0')),
options.Separator => options.process_separator(self), options.Separator => options.process_separator(self),
else => {}, else => {},
} }