From f6def74b296521bbf3e79fa059c97626b3c2f550 Mon Sep 17 00:00:00 2001 From: SparkyTD <45818400+SparkyTD@users.noreply.github.com> Date: Wed, 30 Apr 2025 21:57:34 +0300 Subject: [PATCH] impr: Fix word bound selection and 'MoveHome' behavior in the pattern editor (#2193) ### Problem description This PR addresses two small issues regarding the cursor in the pattern editor (TextEditor.cpp): 1. It was not possible to move the cursor to the start of a line, if it contained leading white space characters. With my fix, the editor will behave more like other code editors. Pressing Home once will jump to the first non-whitespace character (as it did before), but pressing it again will jump to column 0. Subsequent presses will alternate between the two positions. 2. When expanding a selection with Ctrl+Shift+{Left/Right}, the new selection position would skip an additional word, resulting in the cursor landing "inside" the selection. This PR fixes this bug. ### Implementation description 1. To fix the first issue, I simply added a condition in `TextEditor::MoveHome` to check if the cursor is already on the first meaningful character, or on one of the whitespaces preceding it, in which case the jump offset is set to 0. If we're already on column 0, then jump forwards to the first non-whitespace character. 2. This bug was happening because the word boundary jump calculations were essentially happening twice. Once in `TextEditor::MoveLeft`/`TextEditor::MoveRight`, and then a second time in `TextEditor::SetSelection`, leading to the selection skipping an additional word. I fixed this by replacing the ternary operator with just `SelectionMode::Normal`. --------- Co-authored-by: paxcut <53811119+paxcut@users.noreply.github.com> --- .../imgui/ColorTextEditor/source/TextEditor.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp b/lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp index 301ed796c..9f96ed676 100644 --- a/lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp +++ b/lib/third_party/imgui/ColorTextEditor/source/TextEditor.cpp @@ -1790,7 +1790,7 @@ void TextEditor::MoveLeft(int aAmount, bool aSelect, bool aWordMode) { } } else mInteractiveStart = mInteractiveEnd = mState.mCursorPosition; - SetSelection(mInteractiveStart, mInteractiveEnd, aSelect && aWordMode ? SelectionMode::Word : SelectionMode::Normal); + SetSelection(mInteractiveStart, mInteractiveEnd, SelectionMode::Normal); EnsureCursorVisible(); } @@ -1846,7 +1846,7 @@ void TextEditor::MoveRight(int aAmount, bool aSelect, bool aWordMode) { } } else mInteractiveStart = mInteractiveEnd = mState.mCursorPosition; - SetSelection(mInteractiveStart, mInteractiveEnd, aSelect && aWordMode ? SelectionMode::Word : SelectionMode::Normal); + SetSelection(mInteractiveStart, mInteractiveEnd, SelectionMode::Normal); EnsureCursorVisible(); } @@ -1883,9 +1883,19 @@ void TextEditor::MoveHome(bool aSelect) { ResetCursorBlinkTime(); auto oldPos = mState.mCursorPosition; auto &line = mLines[mState.mCursorPosition.mLine]; + if (line.size() == 0) + return; + + auto limit = oldPos.mColumn != 0 ? oldPos.mColumn : line.size(); + auto home=0; - while (isspace(line[home].mChar)) + while (home < limit && isspace(line[home].mChar)) home++; + + if (home == oldPos.mColumn) { + home = 0; + } + SetCursorPosition(Coordinates(mState.mCursorPosition.mLine, home)); if (mState.mCursorPosition != oldPos) { if (aSelect) {