From e7aa8983dcef483a3d6d292329e8d285827f10a5 Mon Sep 17 00:00:00 2001 From: Moulberry Date: Tue, 4 Nov 2025 16:54:31 +0800 Subject: [PATCH] input: Delete selected text with modifier keys (#1497) In most applications, when text is selected and backspace/delete is pressed, the selection is simply deleted. Currently, gpui-component will ignore the selected text and delete the previous/next word when Ctrl is pressed. This can be very confusing especially if you do ctrl+a then ctrl+backspace, expecting the input to be cleared. This PR makes it so that if there is an active selection, the active selection is deleted before doing any extra processing. --- crates/ui/src/input/state.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/ui/src/input/state.rs b/crates/ui/src/input/state.rs index d040ab1c..fbb8dfa7 100644 --- a/crates/ui/src/input/state.rs +++ b/crates/ui/src/input/state.rs @@ -1060,6 +1060,12 @@ impl InputState { window: &mut Window, cx: &mut Context, ) { + if !self.selected_range.is_empty() { + self.replace_text_in_range(None, "", window, cx); + self.pause_blink_cursor(cx); + return; + } + let mut offset = self.start_of_line(); if offset == self.cursor() { offset = offset.saturating_sub(1); @@ -1070,7 +1076,6 @@ impl InputState { window, cx, ); - self.pause_blink_cursor(cx); } @@ -1080,6 +1085,12 @@ impl InputState { window: &mut Window, cx: &mut Context, ) { + if !self.selected_range.is_empty() { + self.replace_text_in_range(None, "", window, cx); + self.pause_blink_cursor(cx); + return; + } + let mut offset = self.end_of_line(); if offset == self.cursor() { offset = (offset + 1).clamp(0, self.text.len()); @@ -1099,6 +1110,12 @@ impl InputState { window: &mut Window, cx: &mut Context, ) { + if !self.selected_range.is_empty() { + self.replace_text_in_range(None, "", window, cx); + self.pause_blink_cursor(cx); + return; + } + let offset = self.previous_start_of_word(); self.replace_text_in_range_silent( Some(self.range_to_utf16(&(offset..self.cursor()))), @@ -1115,6 +1132,12 @@ impl InputState { window: &mut Window, cx: &mut Context, ) { + if !self.selected_range.is_empty() { + self.replace_text_in_range(None, "", window, cx); + self.pause_blink_cursor(cx); + return; + } + let offset = self.next_end_of_word(); self.replace_text_in_range_silent( Some(self.range_to_utf16(&(self.cursor()..offset))),