From 5015bc231b8426708cf26a6b9ea2a03ef1917843 Mon Sep 17 00:00:00 2001 From: Ruben Dashyan Date: Wed, 5 Jun 2019 17:38:22 +0200 Subject: [PATCH] [apps/code/editor_controller] Fix backspace event handling If there are only spaces on the left of the cursor, then a backspace should remove two spaces (or one if there is only one of it). The number of spaces was miscomputed. --- apps/code/editor_controller.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/code/editor_controller.cpp b/apps/code/editor_controller.cpp index 854793fc9..00b4f562a 100644 --- a/apps/code/editor_controller.cpp +++ b/apps/code/editor_controller.cpp @@ -70,9 +70,19 @@ bool EditorController::textAreaDidReceiveEvent(TextArea * textArea, Ion::Events: /* If the cursor is on the left of the text of a line, backspace one * indentation space at a time. */ const char * text = textArea->text(); - const char * firstNonSpace = UTF8Helper::NotCodePointSearch(text, ' ', true, textArea->cursorLocation()); + const char * cursorLocation = textArea->cursorLocation(); + const char * firstNonSpace = UTF8Helper::NotCodePointSearch(text, ' ', true, cursorLocation); assert(firstNonSpace >= text); - if (UTF8Helper::CodePointIs(firstNonSpace, '\n') && ((text - firstNonSpace)/UTF8Decoder::CharSizeOfCodePoint(' ')) >= k_indentationSpacesNumber) { + bool cursorIsPrecededOnTheLineBySpacesOnly = false; + size_t numberOfSpaces = cursorLocation - firstNonSpace; + if (UTF8Helper::CodePointIs(firstNonSpace, '\n')) { + cursorIsPrecededOnTheLineBySpacesOnly = true; + numberOfSpaces -= UTF8Decoder::CharSizeOfCodePoint('\n'); + } else if (firstNonSpace == text) { + cursorIsPrecededOnTheLineBySpacesOnly = true; + } + numberOfSpaces = numberOfSpaces / UTF8Decoder::CharSizeOfCodePoint(' '); + if (cursorIsPrecededOnTheLineBySpacesOnly && numberOfSpaces >= k_indentationSpacesNumber) { for (int i = 0; i < k_indentationSpacesNumber; i++) { textArea->removeCodePoint(); }