IME engine: the input method's byte-counted delete-surrounding deletes the characters those bytes cover, converted at the editor against its text around the caret

This commit is contained in:
aiongg
2026-11-16 21:20:27 +00:00
parent ddb920e3f2
commit 2b66d91e8b
9 changed files with 82 additions and 11 deletions
+43 -4
View File
@@ -4,6 +4,38 @@ use crate::intent::{EditCommand, Granularity, MoveMods, NavMove, TextPos};
use std::ops::Range;
use std::sync::Arc;
/// What an input method counts the text around the caret in.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SurroundingUnit {
/// Characters.
Chars,
/// Bytes of UTF-8 text.
Bytes,
}
impl SurroundingUnit {
/// The characters `n` of this unit cover at the end of `text`, the editor's text before the deletion. A character
/// the range only partly covers stays.
pub fn chars_before(self, n: usize, text: &str) -> usize {
match self {
SurroundingUnit::Chars => n,
SurroundingUnit::Bytes => {
let from = text.len().saturating_sub(n);
text.char_indices().filter(|(at, _)| *at >= from).count()
}
}
}
/// The characters `n` of this unit cover at the start of `text`, the editor's text after the deletion. A
/// character the range only partly covers stays.
pub fn chars_after(self, n: usize, text: &str) -> usize {
match self {
SurroundingUnit::Chars => n,
SurroundingUnit::Bytes => text.char_indices().take_while(|(at, c)| at + c.len_utf8() <= n).count(),
}
}
}
/// One input of an editor part.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
@@ -20,13 +52,15 @@ pub enum EditorInput {
/// How much to delete.
by: Granularity,
},
/// Characters around the caret, deleted for the IME.
/// Text around the caret or the marked text, deleted for the IME.
#[non_exhaustive]
DeleteSurrounding {
/// Characters before the caret.
/// How much lies before the caret, in `unit`.
before: usize,
/// Characters after the caret.
/// How much lies after the caret, in `unit`.
after: usize,
/// What `before` and `after` count.
unit: SurroundingUnit,
},
/// AccessKit `SetTextSelection`.
#[non_exhaustive]
@@ -64,7 +98,12 @@ impl EditorInput {
/// A `DeleteSurrounding` of `before` and `after` characters.
pub const fn delete_surrounding(before: usize, after: usize) -> Self {
EditorInput::DeleteSurrounding { before, after }
EditorInput::DeleteSurrounding { before, after, unit: SurroundingUnit::Chars }
}
/// A `DeleteSurrounding` of `before` and `after` bytes of UTF-8 text, as winit reports it.
pub const fn delete_surrounding_bytes(before: usize, after: usize) -> Self {
EditorInput::DeleteSurrounding { before, after, unit: SurroundingUnit::Bytes }
}
/// A `Preedit` of `text` with the IME's cursor.
+1 -1
View File
@@ -76,7 +76,7 @@ pub use digit_keys::{DigitEntry, DigitKeys};
pub use doc_key::DocKey;
pub use dock::DockSide;
pub use edit_history::EditHistory;
pub use editor::EditorInput;
pub use editor::{EditorInput, SurroundingUnit};
pub use flow::{Axis, Flow, Insets};
pub use focus::{CancelRule, KeyOwner, OwnerClass, PressFocus, ScopeKind};
pub use grid::{GridCursor, GridMeasure, resolve_columns};
+2 -1
View File
@@ -152,7 +152,8 @@ impl Draft {
self.insert(&text);
}
EditorInput::Delete { forward, by, .. } => self.delete(forward, by),
EditorInput::DeleteSurrounding { before, after, .. } => {
EditorInput::DeleteSurrounding { before, after, unit, .. } => {
let (before, after) = (unit.chars_before(before, &self.text[..self.caret]), unit.chars_after(after, &self.text[self.caret..]));
for _ in 0..before {
self.delete(false, Granularity::Unit);
}
+1 -1
View File
@@ -533,7 +533,7 @@ impl Component for TextEditor<'_> {
EditorInput::Tab => edits.done.changed |= edits.insert_text("\t", EditKind::Typed),
EditorInput::Move(step, mods) => edits.motion(step, mods),
EditorInput::Delete { forward, by, .. } => edits.done.changed |= edits.delete(forward, by),
EditorInput::DeleteSurrounding { before, after, .. } => edits.done.changed |= edits.delete_around(before, after),
EditorInput::DeleteSurrounding { before, after, unit, .. } => edits.done.changed |= edits.delete_around(before, after, unit),
EditorInput::Select { anchor, focus, .. } => {
let lines = lines_of(&mut *edits.host, fallback);
edits.end_composition();
+8 -3
View File
@@ -5,7 +5,7 @@ use std::hash::{Hash, Hasher};
use std::ops::Range;
use craft_behavior::kurbo::Point;
use craft_behavior::{Granularity, MoveMods, NavMove};
use craft_behavior::{Granularity, MoveMods, NavMove, SurroundingUnit};
use super::{EditKind, TextEditorHost, TextSelection};
use crate::quotes::{QuoteStyle, smart_quotes};
@@ -262,11 +262,16 @@ impl Edits<'_> {
}
}
/// An input method's delete-surrounding: the chars around the marked text, or the selection.
pub fn delete_around(&mut self, before: usize, after: usize) -> bool {
/// An input method's delete-surrounding: the text around the marked text, or the selection, counted in `unit`.
pub fn delete_around(&mut self, before: usize, after: usize, unit: SurroundingUnit) -> bool {
let chars = self.chars();
let range = self.session.preedit.clone().filter(|r| r.end <= chars).unwrap_or_else(|| self.clamped());
let (start, end) = (range.start, range.end);
let (before, after) = {
let text = self.host.text();
let byte = |at: usize| text.char_indices().nth(at).map_or(text.len(), |(byte, _)| byte);
(unit.chars_before(before, &text[..byte(start)]), unit.chars_after(after, &text[byte(end)..]))
};
let to = end.saturating_add(after).min(chars);
let from = start.saturating_sub(before);
if from >= start && end >= to {
+3 -1
View File
@@ -226,7 +226,9 @@ impl NotesEditor<'_> {
cursor.anchor = range.start;
true
}
EditorInput::DeleteSurrounding { before, after, .. } => {
EditorInput::DeleteSurrounding { before, after, unit, .. } => {
let byte = |at: usize| plain.char_indices().nth(at).map_or(plain.len(), |(byte, _)| byte);
let (before, after) = (unit.chars_before(before, &plain[..byte(selected.start)]), unit.chars_after(after, &plain[byte(selected.end)..]));
let (from, to) = (selected.start.saturating_sub(before), (selected.end + after).min(chars));
if from == selected.start && to == selected.end {
return false;
+15
View File
@@ -243,6 +243,21 @@ fn a_click_during_composition_keeps_the_marked_text_and_interrupts_the_compositi
assert_eq!(editor.commits.get(), 0, "a press in the text keeps the session open");
}
/// An input method that counts the text around the caret in bytes (winit on macOS and Wayland) deletes the characters
/// those bytes cover: "é" is two bytes, "€" three, and a character the range only partly covers stays.
#[trial]
fn the_input_methods_byte_counts_delete_the_characters_around_the_caret() {
let mut editor = session("xé€y", None);
editor.focus();
*editor.selection.borrow_mut() = TextSelection::caret_at(2);
editor.fixture.settle();
editor.fixture.frame(vec![Event::Ime(ImeEvent::DeleteSurroundingBytes { before_bytes: 1, after_bytes: 2 })]).settle();
assert_eq!(editor.text(), "xé€y", "a range that ends inside a character deletes nothing of it");
editor.fixture.frame(vec![Event::Ime(ImeEvent::DeleteSurroundingBytes { before_bytes: 2, after_bytes: 3 })]).settle();
assert_eq!(editor.text(), "xy", "two bytes before cover \"é\", three after cover \"€\"");
assert_eq!(*editor.selection.borrow(), TextSelection::caret_at(1), "the caret stays between the neighbours");
}
/// Cmd+. cancels a session with edits, though Esc commits (the default); while a composition is open it does nothing,
/// and a session without edits gives no `Cancel`. A press frame that runs a second pass cancels once.
#[trial]
@@ -15,6 +15,11 @@ pub(crate) enum Raw {
before: usize,
after: usize,
},
/// The same, counted in bytes of UTF-8 text (winit on macOS and Wayland).
DeleteSurroundingBytes {
before: usize,
after: usize,
},
/// The OS finished the reset with this request id.
ResetCompleted(egui::ImeResetId),
/// The integration dropped the reset request with this id; no completion follows.
@@ -34,6 +39,9 @@ pub(crate) fn take_events(events: &mut Vec<Event>) -> Vec<Raw> {
ImeEvent::DeleteSurrounding { before_chars, after_chars } => {
taken.push(Raw::DeleteSurrounding { before: *before_chars, after: *after_chars })
}
ImeEvent::DeleteSurroundingBytes { before_bytes, after_bytes } => {
taken.push(Raw::DeleteSurroundingBytes { before: *before_bytes, after: *after_bytes })
}
_ => {}
}
false
+1
View File
@@ -125,6 +125,7 @@ pub(crate) fn begin(ctx: &Context, owner: Option<Id>) {
engine.edits.push(EditorInput::preedit(text, cursor));
}
os::Raw::DeleteSurrounding { before, after } => engine.edits.push(EditorInput::delete_surrounding(before, after)),
os::Raw::DeleteSurroundingBytes { before, after } => engine.edits.push(EditorInput::delete_surrounding_bytes(before, after)),
os::Raw::Commit(text) => {
engine.composing = false;
engine.preedit.clear();