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.
This commit is contained in:
Moulberry 2025-11-04 16:54:31 +08:00 committed by GitHub
parent 893e323692
commit e7aa8983dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1060,6 +1060,12 @@ impl InputState {
window: &mut Window,
cx: &mut Context<Self>,
) {
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<Self>,
) {
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<Self>,
) {
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<Self>,
) {
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))),