From f8ac6eef75b6797178990c57000c5b0cbc651694 Mon Sep 17 00:00:00 2001 From: Ho Chun Lau <39511811+kylelau519@users.noreply.github.com> Date: Mon, 21 Apr 2025 16:39:17 +0100 Subject: [PATCH] terminal: Add right-click in terminal to create a new selection if none is present (#29131) This PR adds functionality to right click in terminal create new selection if none present. The selection is identical with double click a text in terminal, plus the logic is moved from the double-click in the terminal::mouse_down. Closes #28237 Release Notes: - Adds functionality to right click in terminal create new selection if none present --- crates/terminal/src/terminal.rs | 12 ++++++++++++ crates/terminal_view/src/terminal_view.rs | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/crates/terminal/src/terminal.rs b/crates/terminal/src/terminal.rs index b2dac30d0276e658773b4fcc1d81cbd23ac480d8..6b562aec231f836c37d76a96ed8ce1409fdb3443 100644 --- a/crates/terminal/src/terminal.rs +++ b/crates/terminal/src/terminal.rs @@ -1547,6 +1547,18 @@ impl Terminal { } } + pub fn select_word_at_event_position(&mut self, e: &MouseDownEvent) { + let position = e.position - self.last_content.terminal_bounds.bounds.origin; + let (point, side) = grid_point_and_side( + position, + self.last_content.terminal_bounds, + self.last_content.display_offset, + ); + let selection = Selection::new(SelectionType::Semantic, point, side); + self.events + .push_back(InternalEvent::SetSelection(Some((selection, point)))); + } + pub fn mouse_drag( &mut self, e: &MouseMoveEvent, diff --git a/crates/terminal_view/src/terminal_view.rs b/crates/terminal_view/src/terminal_view.rs index ee25792f35e7606c5f95d0181d1aebbe14e66fbf..c2dbc72b1fe2a4be0b72a61fc9d6da6b89f07994 100644 --- a/crates/terminal_view/src/terminal_view.rs +++ b/crates/terminal_view/src/terminal_view.rs @@ -1278,6 +1278,11 @@ impl Render for TerminalView { MouseButton::Right, cx.listener(|this, event: &MouseDownEvent, window, cx| { if !this.terminal.read(cx).mouse_mode(event.modifiers.shift) { + if this.terminal.read(cx).last_content.selection.is_none() { + this.terminal.update(cx, |terminal, _| { + terminal.select_word_at_event_position(event); + }); + }; this.deploy_context_menu(event.position, window, cx); cx.notify(); }