Skip to content

fix: insert IME composing text at the offset reported by the delta - #1250

Open
Ultra-Logos wants to merge 1 commit into
AppFlowy-IO:mainfrom
Ultra-Logos:fix/ime-composing-insert-offset
Open

Ultra-Logos wants to merge 1 commit into
AppFlowy-IO:mainfrom
Ultra-Logos:fix/ime-composing-insert-offset

Conversation

@Ultra-Logos

Copy link
Copy Markdown

Problem

While an IME composition is active, onInsert inserts the new text at the editor selection instead of at TextEditingDeltaInsertion.insertionOffset.

Some IMEs report the selection at the start of the preedit while composing spans the whole preedit. fcitx5 on Linux (Rime engine) does exactly this:

value
engine text " 这是"
selection 1 (start of the preedit)
composing [1, 3]
delta insertionOffset 2

The editor inserts at offset 0, so the node text becomes 是这. It no longer matches the platform IME model, KeyboardServiceWidget._attachTextInputService therefore pushes a corrected value through setEditingState, and the following updates drop, reorder or duplicate characters in the document.

Same failure mode as #696 (Sogou on Windows): characters arrive out of order and keep leaking into the document while the IME caret stays at the beginning of the preedit.

Fix

Insert at the offset reported by the delta while composing, and keep afterSelection at the position reported by the IME — moving the selection would trigger the same setEditingState resync.

final insertOffset = insertion.composing.isCollapsed
    ? selection.startIndex
    : insertion.insertionOffset;

Regular typing is untouched: collapsed composing ranges still insert at the selection, and for IMEs that keep the caret at the end of the preedit both offsets are identical.

Tests

New widget test in test/new/service/ime/delta_input_on_insert_impl_test.dart replays an fcitx5 preedit sequence (" 这" → " 这是", caret always at the preedit start) and asserts the node text is 这是:

  • without the fix: Expected: 这是 / Actual: 是这
  • with the fix: passes

The existing delta input insert - widget test cases still pass.

Fixes #696

Some IMEs (fcitx5 on Linux) report the selection at the start of the preedit while the composing range spans the whole preedit. Inserting at the editor selection then prepends every new character to the preedit, the editor text drifts from the platform IME model and the corrective setEditingState drops or duplicates characters.
@CLAassistant

CLAassistant commented Nov 17, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Input characters are out of order when using some IME

2 participants