From 58e10885242fc66739ef65bec9e0ed3e3c2c41ef Mon Sep 17 00:00:00 2001 From: serso Date: Fri, 4 Mar 2016 23:09:26 +0100 Subject: [PATCH] Fix erase grouping separator issue --- .../org/solovyev/android/calculator/Editor.java | 17 ++++++++++++----- .../android/calculator/math/MathType.java | 4 +++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/Editor.java b/app/src/main/java/org/solovyev/android/calculator/Editor.java index c24dc3c8..90f02f70 100644 --- a/app/src/main/java/org/solovyev/android/calculator/Editor.java +++ b/app/src/main/java/org/solovyev/android/calculator/Editor.java @@ -25,13 +25,12 @@ package org.solovyev.android.calculator; import android.app.Application; import android.content.SharedPreferences; import android.text.TextUtils; - import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; - import org.solovyev.android.Check; import org.solovyev.android.calculator.history.HistoryState; import org.solovyev.android.calculator.history.RecentHistory; +import org.solovyev.android.calculator.math.MathType; import org.solovyev.android.calculator.text.TextProcessorEditorResult; import org.solovyev.android.calculator.view.EditorTextProcessor; @@ -53,6 +52,8 @@ public class Editor { private EditorState state = EditorState.empty(); @Inject Bus bus; + @Inject + Engine engine; @Inject public Editor(@Nonnull Application application, @Nonnull SharedPreferences preferences, @Nonnull Engine engine) { @@ -176,9 +177,15 @@ public class Editor { if (selection <= 0 || text.length() <= 0 || selection > text.length()) { return state; } - final String newText = text.substring(0, selection - 1) + text.substring(selection, - text.length()); - return onTextChanged(EditorState.create(newText, selection - 1)); + int removeStart = selection - 1; + if (MathType.getType(text, selection - 1, false, engine).type == MathType.grouping_separator) { + // we shouldn't remove just separator as it will be re-added after the evaluation is done. Remove the digit + // before + removeStart -= 1; + } + + final String newText = text.substring(0, removeStart) + text.substring(selection, text.length()); + return onTextChanged(EditorState.create(newText, removeStart)); } @Nonnull diff --git a/app/src/main/java/org/solovyev/android/calculator/math/MathType.java b/app/src/main/java/org/solovyev/android/calculator/math/MathType.java index f47784bd..db21a989 100644 --- a/app/src/main/java/org/solovyev/android/calculator/math/MathType.java +++ b/app/src/main/java/org/solovyev/android/calculator/math/MathType.java @@ -312,7 +312,9 @@ public enum MathType { } if (mathType == MathType.grouping_separator) { - if (i + 1 < text.length() && getType(text, i + 1, hexMode, result, engine).type == MathType.digit) { + if (i + 1 < text.length() && + getType(text, i + 1, hexMode, result, engine).type == MathType.digit && + i - 1 >= 0 && getType(text, i - 1, hexMode, result, engine).type == MathType.digit) { return result.set(mathType, s); } continue;