feat: add mouse bindings for jump_back/forward and goto_definition

This commit is contained in:
CJ van den Berg 2024-04-09 18:22:45 +02:00
parent 695b0dbff5
commit 4b2c033c5d
3 changed files with 35 additions and 5 deletions

View file

@ -58,7 +58,7 @@ cycle style of editing.
- Ctrl-Home, Ctrl-End =>
Move to the beginning/end of the file
- Alt-Left, Alt-Right =>
- Alt-Left, Alt-Right, MouseBack, MouseForward =>
Jump to previous/next location in the location history
- Ctrl-f =>
@ -244,7 +244,10 @@ cycle style of editing.
### Language Server Commands
- F12 =>
Goto definition
Goto definition of symbol at cursor
- Alt-MouseLeft =>
Goto definition of symbol at click
### Debugging Commands

View file

@ -196,6 +196,7 @@ pub const Editor = struct {
handlers: EventHandler.List,
scroll_dest: usize = 0,
fast_scroll: bool = false,
jump_mode: bool = false,
animation_step: usize = 0,
animation_frame_rate: i64,
@ -1540,9 +1541,12 @@ pub const Editor = struct {
const primary = self.get_primary();
primary.selection = null;
self.selection_mode = .char;
try self.send_editor_jump_source();
const root = self.buf_root() catch return;
primary.cursor.move_abs(root, &self.view, @intCast(y), @intCast(x)) catch return;
self.clamp_mouse();
try self.send_editor_jump_destination();
if (self.jump_mode) try self.goto_definition(.{});
}
pub fn primary_double_click(self: *Self, y: c_int, x: c_int) tp.result {
@ -2709,6 +2713,16 @@ pub const Editor = struct {
self.fast_scroll = false;
}
pub fn enable_jump_mode(self: *Self, _: command.Context) tp.result {
self.jump_mode = true;
tui.current().request_mouse_cursor_pointer(true);
}
pub fn disable_jump_mode(self: *Self, _: command.Context) tp.result {
self.jump_mode = false;
tui.current().request_mouse_cursor_text(true);
}
fn update_syntax(self: *Self) !void {
const frame = tracy.initZone(@src(), .{ .name = "editor update syntax" });
defer frame.deinit();
@ -3348,7 +3362,10 @@ pub const EditorWidget = struct {
} else if (try m.match(.{ "A", tp.more })) {
self.editor.add_match(m) catch {};
} else if (try m.match(.{ "H", tp.extract(&self.hover) })) {
tui.current().request_mouse_cursor_text(self.hover);
if (self.editor.jump_mode)
tui.current().request_mouse_cursor_pointer(self.hover)
else
tui.current().request_mouse_cursor_text(self.hover);
} else if (try m.match(.{ "show_whitespace", tp.extract(&self.editor.show_whitespace) })) {
_ = "";
} else {
@ -3365,6 +3382,8 @@ pub const EditorWidget = struct {
nc.key.BUTTON3 => &mouse_click_button3,
nc.key.BUTTON4 => &mouse_click_button4,
nc.key.BUTTON5 => &mouse_click_button5,
nc.key.BUTTON8 => &mouse_click_button8, //back
nc.key.BUTTON9 => &mouse_click_button9, //forward
else => return,
})(self, y, x, ypx, xpx);
self.last_btn = btn;
@ -3433,6 +3452,14 @@ pub const EditorWidget = struct {
try self.editor.scroll_down_pagedown(.{});
}
fn mouse_click_button8(_: *Self, _: c_int, _: c_int, _: c_int, _: c_int) tp.result {
try command.executeName("jump_back", .{});
}
fn mouse_click_button9(_: *Self, _: c_int, _: c_int, _: c_int, _: c_int) tp.result {
try command.executeName("jump_forward", .{});
}
pub fn handle_resize(self: *Self, pos: Widget.Box) void {
self.plane.move_yx(@intCast(pos.y), @intCast(pos.x)) catch return;
self.plane.resize_simple(@intCast(pos.h), @intCast(pos.w)) catch return;

View file

@ -72,7 +72,7 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) tp.result {
if (self.leader) |_| return self.mapFollower(keynormal, egc, modifiers);
switch (keypress) {
key.LCTRL, key.RCTRL => return self.cmd("enable_fast_scroll", .{}),
key.LALT, key.RALT => return self.cmd("enable_fast_scroll", .{}),
key.LALT, key.RALT => return self.cmd("enable_jump_mode", .{}),
else => {},
}
return switch (modifiers) {
@ -237,7 +237,7 @@ fn mapFollower(self: *Self, keypress: u32, _: u32, modifiers: u32) tp.result {
fn mapRelease(self: *Self, keypress: u32, _: u32, _: u32) tp.result {
return switch (keypress) {
key.LCTRL, key.RCTRL => self.cmd("disable_fast_scroll", .{}),
key.LALT, key.RALT => self.cmd("disable_fast_scroll", .{}),
key.LALT, key.RALT => self.cmd("disable_jump_mode", .{}),
else => {},
};
}