From e4fe1973b231833e1f7d46ea27285ba0dec6277b Mon Sep 17 00:00:00 2001 From: Wanten Date: Wed, 9 Apr 2025 23:14:03 +0800 Subject: [PATCH 1/4] fix(editor): prevent cursor movement during IME preedit composition Modify the `replace_and_mark_text_in_range` method in the `Editor` to keep the cursor at the start of the preedit range during IME composition. Previously, the cursor would move to the end of the preedit text with each update, causing the IME candidate box to shift (e.g., when typing pinyin with Fcitx5 on Wayland). This change ensures the cursor and candidate box remain fixed until the composition is committed, improving the IME experience. Fixes #21004 --- crates/editor/src/editor.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 5539cea460ff25..5c00697acfb4de 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -19748,16 +19748,14 @@ impl EntityInputHandler for Editor { this.set_use_autoclose(use_autoclose); this.set_use_auto_surround(use_auto_surround); - if let Some(new_selected_range) = new_selected_range_utf16 { + if !text.is_empty() { let snapshot = this.buffer.read(cx).read(cx); let new_selected_ranges = marked_ranges .into_iter() .map(|marked_range| { - let insertion_start = marked_range.start.to_offset_utf16(&snapshot).0; - let new_start = OffsetUtf16(new_selected_range.start + insertion_start); - let new_end = OffsetUtf16(new_selected_range.end + insertion_start); - snapshot.clip_offset_utf16(new_start, Bias::Left) - ..snapshot.clip_offset_utf16(new_end, Bias::Right) + let start = marked_range.start.to_offset_utf16(&snapshot); + let end = marked_range.start.to_offset_utf16(&snapshot); + start..end }) .collect::>(); From 2e151ca4e702916f7a61767b6fdbc1da67c850e2 Mon Sep 17 00:00:00 2001 From: Wanten Date: Thu, 10 Apr 2025 21:09:02 +0800 Subject: [PATCH 2/4] Revert "fix(editor): prevent cursor movement during IME preedit composition" This reverts commit e4fe1973b231833e1f7d46ea27285ba0dec6277b. --- crates/editor/src/editor.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 5c00697acfb4de..5539cea460ff25 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -19748,14 +19748,16 @@ impl EntityInputHandler for Editor { this.set_use_autoclose(use_autoclose); this.set_use_auto_surround(use_auto_surround); - if !text.is_empty() { + if let Some(new_selected_range) = new_selected_range_utf16 { let snapshot = this.buffer.read(cx).read(cx); let new_selected_ranges = marked_ranges .into_iter() .map(|marked_range| { - let start = marked_range.start.to_offset_utf16(&snapshot); - let end = marked_range.start.to_offset_utf16(&snapshot); - start..end + let insertion_start = marked_range.start.to_offset_utf16(&snapshot).0; + let new_start = OffsetUtf16(new_selected_range.start + insertion_start); + let new_end = OffsetUtf16(new_selected_range.end + insertion_start); + snapshot.clip_offset_utf16(new_start, Bias::Left) + ..snapshot.clip_offset_utf16(new_end, Bias::Right) }) .collect::>(); From d6ccb7845fbc03521506be491288955f5b36dada Mon Sep 17 00:00:00 2001 From: Wanten Date: Fri, 11 Apr 2025 03:12:30 +0800 Subject: [PATCH 3/4] fix(editor): use marked_text_range for IME candidate box positioning on Wayland Replace `selected_text_range` with `marked_text_range` in `WaylandWindowStatePtr` to fix the IME candidate box movement issue on Linux Wayland. The previous implementation caused the candidate box to follow the cursor due to `selected_text_range` always returning the cursor's current position. Using `marked_text_range` ensures the candidate box remains fixed at the initial pre-edit position, improving usability for input methods like Fcitx5. Resolves #21004 --- crates/gpui/src/platform/linux/wayland/window.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/crates/gpui/src/platform/linux/wayland/window.rs b/crates/gpui/src/platform/linux/wayland/window.rs index 4178bea4a35c4c..dc7c8df59d9ce1 100644 --- a/crates/gpui/src/platform/linux/wayland/window.rs +++ b/crates/gpui/src/platform/linux/wayland/window.rs @@ -635,12 +635,10 @@ impl WaylandWindowStatePtr { let mut bounds: Option> = None; if let Some(mut input_handler) = state.input_handler.take() { drop(state); - if let Some(selection) = input_handler.selected_text_range(true) { - bounds = input_handler.bounds_for_range(if selection.reversed { - selection.range.start..selection.range.start - } else { - selection.range.end..selection.range.end - }); + if let Some(selection) = input_handler.marked_text_range() { + bounds = input_handler.bounds_for_range( + selection.start..selection.start + ); } self.state.borrow_mut().input_handler = Some(input_handler); } From a88fc34e6451899af19f98c5fc19cea722ac0ade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=B0=8F=E7=99=BD?= <364772080@qq.com> Date: Wed, 4 Jun 2025 14:57:10 +0800 Subject: [PATCH 4/4] fmt --- crates/gpui/src/platform/linux/wayland/window.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/gpui/src/platform/linux/wayland/window.rs b/crates/gpui/src/platform/linux/wayland/window.rs index 37a5e70743215b..6b968d27b71bfa 100644 --- a/crates/gpui/src/platform/linux/wayland/window.rs +++ b/crates/gpui/src/platform/linux/wayland/window.rs @@ -636,9 +636,7 @@ impl WaylandWindowStatePtr { if let Some(mut input_handler) = state.input_handler.take() { drop(state); if let Some(selection) = input_handler.marked_text_range() { - bounds = input_handler.bounds_for_range( - selection.start..selection.start - ); + bounds = input_handler.bounds_for_range(selection.start..selection.start); } self.state.borrow_mut().input_handler = Some(input_handler); }