From 5d87108b3d93b4cf22eb53b9ec880fbd807912f4 Mon Sep 17 00:00:00 2001 From: Yousef Khadadeh Date: Sun, 28 Sep 2025 20:53:47 +0300 Subject: [PATCH] Make moving between words more aware of whitespaces --- src/CollabTextBox/CollabTextBox.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/CollabTextBox/CollabTextBox.cpp b/src/CollabTextBox/CollabTextBox.cpp index 811dd39..efa8ac1 100644 --- a/src/CollabTextBox/CollabTextBox.cpp +++ b/src/CollabTextBox/CollabTextBox.cpp @@ -409,10 +409,19 @@ TextPosition Editor::move(Movement move, TextPosition pos) const { break; case Movement::kWordLeft: { + for(;;) { + TextPosition prevCharPos = this->move(Movement::kLeft, pos); + if(prevCharPos != TextPosition{0, 0} && std::isspace(fLines[prevCharPos.fParagraphIndex].fText[prevCharPos.fTextByteIndex])) + pos = prevCharPos; + else + break; + } + if (pos.fTextByteIndex == 0) { pos = this->move(Movement::kLeft, pos); break; } + const std::vector& words = fLines[pos.fParagraphIndex].fWordBoundaries; assert(words.size() == fLines[pos.fParagraphIndex].fText.size()); do { @@ -432,6 +441,14 @@ TextPosition Editor::move(Movement move, TextPosition pos) const { do { ++pos.fTextByteIndex; } while (pos.fTextByteIndex < text.size() && !words[pos.fTextByteIndex]); + + for(;;) { + const std::string& text = fLines[pos.fParagraphIndex].fText; + if(pos.fTextByteIndex != text.size() && std::isspace(text[pos.fTextByteIndex])) + pos = this->move(Movement::kRight, pos); + else + break; + } } break;