editor: Stabilize IME candidate box position during pre-edit on Wayland
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
Closes #ISSUE
Release Notes:
- N/A or Added/Fixed/Improved ...
We require contributors to sign our Contributor License Agreement, and we don't have @WantenMN on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.
@cla-bot check
We require contributors to sign our Contributor License Agreement, and we don't have @WantenMN on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.
The cla-bot has been summoned, and re-checked this pull request!
@cla-bot check
The cla-bot has been summoned, and re-checked this pull request!
I think the code changes were made in the wrong place. While everything works fine on Windows and macOS, the issue appears to be specifically with the Linux implementation. Your current changes affect behavior across all platforms when they should likely target Linux specifically.
Windows:
https://github.com/user-attachments/assets/8f8a9c22-cbb2-4a4c-97fd-a14f83ba5415
I think the code changes were made in the wrong place. While everything works fine on Windows and macOS, the issue appears to be specifically with the Linux implementation. Your current changes affect behavior across all platforms when they should likely target Linux specifically.
Windows: 2025-04-09.235340.mp4
You're right, I'll revert the changes and work on a better approach to fix the issue.
I believe that the cursor moving continuously while typing is expected behavior and shouldn’t be addressed with a hack. The issue of the IME candidate box shifting with the cursor is a problem with the input method itself, not the editor. Instead of modifying the editor’s code, it would be more appropriate to submit a pull request to the IME to ensure the candidate box remains fixed at its initial position.
I’ve updated the fix for this issue in my latest commit (d6ccb78).
I realized the issue was with selected_text_range—it’s the function causing the candidate box to follow the cursor on Wayland. Switching to marked_text_range ensures it stays fixed at the pre-edit start position
One thing I noticed: selected_text_range always returns a range where start and end are the same (the cursor position), so I’m not sure what selection.reversed is supposed to do in this case—or how reversed even gets triggered.
It’s unclear to me why get_ime_area relied on selected_text_range in the first place. I think marked_text_range is a much better fit here, and this change should fully resolve the moving candidate box issue on Linux Wayland.
Here are some screenshots:
-
Editor
-
AI Assistant Panel
-
Command Palette
-
Project Panel creating a new file
@WantenMN Hello, looks like this PR requires a rebase on main
Hey @JunkuiZhang last pr merge broke the ci. I have a pr open: https://github.com/zed-industries/zed/pull/32049. I believe this pr will be fixed once we have this on main.
@imumesh18 Thanks for the fix!
Still requires a rebase I think, tests failed even that pr was merged
Thanks!