diff --git a/src/core/text/edit.rs b/src/core/text/edit.rs index 7f113e1..3d9de7e 100644 --- a/src/core/text/edit.rs +++ b/src/core/text/edit.rs @@ -202,18 +202,35 @@ impl<'a> TextEditCtx<'a> { self.text.selection.clear(); } - pub fn motion(&mut self, motion: Motion) { + pub fn motion(&mut self, motion: Motion, select: bool) { if let TextSelection::Pos(cursor) = self.text.selection - && let Some(cursor) = self.buf_motion(cursor, motion) + && let Some(new) = self.buf_motion(cursor, motion) { - self.text.selection = TextSelection::Pos(cursor); + if select { + self.text.selection = TextSelection::Span { + start: cursor, + end: new, + }; + } else { + self.text.selection = TextSelection::Pos(new); + } } else if let TextSelection::Span { start, end } = self.text.selection { - let (start, end) = sort_cursors(start, end); - let sel = &mut self.text.selection; - match motion { - Motion::Left | Motion::LeftWord => *sel = TextSelection::Pos(start), - Motion::Right | Motion::RightWord => *sel = TextSelection::Pos(end), - _ => (), + if select { + if let Some(cursor) = self.buf_motion(end, motion) { + self.text.selection = TextSelection::Span { start, end: cursor }; + } + } else { + let (start, end) = sort_cursors(start, end); + let sel = &mut self.text.selection; + match motion { + Motion::Left | Motion::LeftWord => *sel = TextSelection::Pos(start), + Motion::Right | Motion::RightWord => *sel = TextSelection::Pos(end), + _ => { + if let Some(cursor) = self.buf_motion(end, motion) { + self.text.selection = TextSelection::Pos(cursor); + } + } + } } } } @@ -279,7 +296,7 @@ impl<'a> TextEditCtx<'a> { edit_line(line, line_text); if mov { for _ in 0..text.chars().count() { - self.motion(Motion::Right); + self.motion(Motion::Right, false); } } } @@ -302,7 +319,7 @@ impl<'a> TextEditCtx<'a> { && let TextSelection::Pos(cursor) = &mut self.text.selection && (cursor.index != 0 || cursor.line != 0) { - self.motion(if word { Motion::LeftWord } else { Motion::Left }); + self.motion(if word { Motion::LeftWord } else { Motion::Left }, false); self.delete(word); } } @@ -444,20 +461,20 @@ impl<'a> TextEditCtx<'a> { } NamedKey::ArrowRight => { if modifiers.control { - self.motion(Motion::RightWord) + self.motion(Motion::RightWord, modifiers.shift) } else { - self.motion(Motion::Right) + self.motion(Motion::Right, modifiers.shift) } } NamedKey::ArrowLeft => { if modifiers.control { - self.motion(Motion::LeftWord) + self.motion(Motion::LeftWord, modifiers.shift) } else { - self.motion(Motion::Left) + self.motion(Motion::Left, modifiers.shift) } } - NamedKey::ArrowUp => self.motion(Motion::Up), - NamedKey::ArrowDown => self.motion(Motion::Down), + NamedKey::ArrowUp => self.motion(Motion::Up, modifiers.shift), + NamedKey::ArrowDown => self.motion(Motion::Down, modifiers.shift), NamedKey::Escape => { self.deselect(); return TextInputResult::Unfocus;