diff --git a/crates/behavior/src/editor.rs b/crates/behavior/src/editor.rs index 4c696ef76..135055200 100644 --- a/crates/behavior/src/editor.rs +++ b/crates/behavior/src/editor.rs @@ -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. diff --git a/crates/behavior/src/lib.rs b/crates/behavior/src/lib.rs index fb1859f2b..8f227e08c 100644 --- a/crates/behavior/src/lib.rs +++ b/crates/behavior/src/lib.rs @@ -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}; diff --git a/crates/table/src/ui/grid/editor.rs b/crates/table/src/ui/grid/editor.rs index 0b6c7e675..569871b9e 100644 --- a/crates/table/src/ui/grid/editor.rs +++ b/crates/table/src/ui/grid/editor.rs @@ -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); } diff --git a/crates/text/src/ui/editor.rs b/crates/text/src/ui/editor.rs index 0bdd6eea1..efdd48af2 100644 --- a/crates/text/src/ui/editor.rs +++ b/crates/text/src/ui/editor.rs @@ -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(); diff --git a/crates/text/src/ui/editor/edits.rs b/crates/text/src/ui/editor/edits.rs index 5c200ef5c..c16530d8c 100644 --- a/crates/text/src/ui/editor/edits.rs +++ b/crates/text/src/ui/editor/edits.rs @@ -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 { diff --git a/crates/text/src/ui/notes.rs b/crates/text/src/ui/notes.rs index 11e7f4027..4450cc817 100644 --- a/crates/text/src/ui/notes.rs +++ b/crates/text/src/ui/notes.rs @@ -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; diff --git a/crates/text/tests/it/editor.rs b/crates/text/tests/it/editor.rs index 5d8b02558..abd349df4 100644 --- a/crates/text/tests/it/editor.rs +++ b/crates/text/tests/it/editor.rs @@ -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] diff --git a/crates/ui-contract/src/runtime/egui/ime.rs b/crates/ui-contract/src/runtime/egui/ime.rs index 26a2ddd59..6dc043913 100644 --- a/crates/ui-contract/src/runtime/egui/ime.rs +++ b/crates/ui-contract/src/runtime/egui/ime.rs @@ -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) -> Vec { 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 diff --git a/crates/ui-contract/src/runtime/ime.rs b/crates/ui-contract/src/runtime/ime.rs index 6c845d5a6..e95f1343e 100644 --- a/crates/ui-contract/src/runtime/ime.rs +++ b/crates/ui-contract/src/runtime/ime.rs @@ -125,6 +125,7 @@ pub(crate) fn begin(ctx: &Context, owner: Option) { 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();