diff --git a/src/buffer/View.zig b/src/buffer/View.zig index 8608c4b..c6579ad 100644 --- a/src/buffer/View.zig +++ b/src/buffer/View.zig @@ -92,22 +92,23 @@ inline fn to_cursor_bottom(self: *const Self, root: Buffer.Root) Cursor { return .{ .row = bottom, .col = 0 }; } -fn clamp_row(self: *Self, cursor: *const Cursor, abs: bool) void { - const min_border_distance: usize = if (abs) scroll_cursor_min_border_distance_mouse else scroll_cursor_min_border_distance; - if (cursor.row < min_border_distance) { +fn clamp_row(self: *Self, cursor: *const Cursor, abs: bool, bottom_offset: usize) void { + const top_min_border_distance: usize = if (abs) scroll_cursor_min_border_distance_mouse else scroll_cursor_min_border_distance; + const bottom_min_border_distance: usize = top_min_border_distance + bottom_offset; + if (cursor.row < top_min_border_distance) { self.row = 0; return; } - if (self.row > 0 and cursor.row >= min_border_distance) { - if (cursor.row < self.row + min_border_distance) { - self.row = cursor.row - min_border_distance; + if (self.row > 0 and cursor.row >= top_min_border_distance) { + if (cursor.row < self.row + top_min_border_distance) { + self.row = cursor.row - top_min_border_distance; return; } } if (cursor.row < self.row) { self.row = 0; - } else if (cursor.row > self.row + self.rows - min_border_distance) { - self.row = cursor.row + min_border_distance - self.rows; + } else if (cursor.row > self.row + self.rows - bottom_min_border_distance) { + self.row = cursor.row + bottom_min_border_distance - self.rows; } } @@ -120,7 +121,12 @@ fn clamp_col(self: *Self, cursor: *const Cursor, _: bool) void { } pub fn clamp(self: *Self, cursor: *const Cursor, abs: bool) void { - self.clamp_row(cursor, abs); + self.clamp_row(cursor, abs, 0); + self.clamp_col(cursor, abs); +} + +pub fn clamp_offset(self: *Self, cursor: *const Cursor, abs: bool, offset: usize) void { + self.clamp_row(cursor, abs, offset); self.clamp_col(cursor, abs); } diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 4d5d2ab..c41b6d9 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -1914,17 +1914,25 @@ pub const Editor = struct { _ = try self.handlers.msg(.{ "E", "eol_mode", eol_mode, utf8_sanitized, indent_mode }); } - fn clamp_abs(self: *Self, abs: bool) void { + fn clamp_abs_offset(self: *Self, abs: bool, offset: usize) void { var dest: View = self.view; - dest.clamp(&self.get_primary().cursor, abs); + dest.clamp_offset(&self.get_primary().cursor, abs, offset); self.update_scroll_dest_abs(dest.row); self.view.col = dest.col; } + fn clamp_abs(self: *Self, abs: bool) void { + self.clamp_abs_offset(abs, 0); + } + pub inline fn clamp(self: *Self) void { self.clamp_abs(false); } + inline fn clamp_offset(self: *Self, offset: usize) void { + self.clamp_abs_offset(false, offset); + } + fn clamp_mouse(self: *Self) void { self.clamp_abs(true); } @@ -6005,11 +6013,14 @@ pub const Editor = struct { pub const references_meta: Meta = .{ .description = "Language: Find all references" }; pub fn completion(self: *Self, _: Context) Result { + const mv = tui.mainview() orelse return; const file_path = self.file_path orelse return; const root = self.buf_root() catch return; const primary = self.get_primary(); const col = try root.get_line_width_to_pos(primary.cursor.row, primary.cursor.col, self.metrics); self.completions.clearRetainingCapacity(); + if (!mv.is_any_panel_view_showing()) + self.clamp_offset(mv.get_panel_height()); return project_manager.completion(file_path, primary.cursor.row, col); } pub const completion_meta: Meta = .{ .description = "Language: Show completions at cursor" }; diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index fbc94f7..f77291e 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -276,6 +276,10 @@ fn bottom_bar_primary_drag(self: *Self, y: usize) tp.result { } } +pub fn get_panel_height(self: *Self) usize { + return self.panel_height orelse self.box().h / 5; +} + fn toggle_panel_view(self: *Self, view: anytype, mode: enum { toggle, enable, disable }) !void { if (self.panels) |panels| { if (self.get_panel(@typeName(view))) |w| { @@ -291,7 +295,7 @@ fn toggle_panel_view(self: *Self, view: anytype, mode: enum { toggle, enable, di try panels.add(try view.create(self.allocator, self.widgets.plane)); } } else if (mode != .disable) { - const panels = try WidgetList.createH(self.allocator, self.widgets.plane, "panel", .{ .static = self.panel_height orelse self.box().h / 5 }); + const panels = try WidgetList.createH(self.allocator, self.widgets.plane, "panel", .{ .static = self.get_panel_height() }); try self.widgets.add(panels.widget()); try panels.add(try view.create(self.allocator, self.widgets.plane)); self.panels = panels; @@ -315,6 +319,10 @@ fn is_panel_view_showing(self: *Self, comptime view: type) bool { return self.get_panel_view(view) != null; } +pub fn is_any_panel_view_showing(self: *Self) bool { + return self.panels != null; +} + fn close_all_panel_views(self: *Self) void { if (self.panels) |panels| { self.widgets.remove(panels.widget());