From 14a3e6928c4b00010530f0a91e3f1c756b436ceb Mon Sep 17 00:00:00 2001 From: Stefan Niedermann Date: Thu, 22 Jul 2021 13:30:25 +0200 Subject: [PATCH] =?UTF-8?q?#1019=20=E2=86=AA=EF=B8=8F=20Auto=20continuatio?= =?UTF-8?q?n=20should=20continue=20a=20list=20at=20the=20same=20indention?= =?UTF-8?q?=20level?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stefan Niedermann --- .../AutoContinuationTextWatcher.java | 12 ++++++--- .../AutoContinuationTextWatcherTest.java | 25 +++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/markdown/src/main/java/it/niedermann/android/markdown/markwon/textwatcher/AutoContinuationTextWatcher.java b/markdown/src/main/java/it/niedermann/android/markdown/markwon/textwatcher/AutoContinuationTextWatcher.java index eff85f08..a30f8e43 100644 --- a/markdown/src/main/java/it/niedermann/android/markdown/markwon/textwatcher/AutoContinuationTextWatcher.java +++ b/markdown/src/main/java/it/niedermann/android/markdown/markwon/textwatcher/AutoContinuationTextWatcher.java @@ -91,20 +91,26 @@ public class AutoContinuationTextWatcher extends InterceptorTextWatcher { isInsert = false; sequenceStart = startOfLine; } else { + // TODO use Java 11 String::repeat + final StringBuilder builder = new StringBuilder(); + for (int i = 0; i < line.indexOf(line.trim()); i++) { + builder.append(" "); + } for (EListType listType : EListType.values()) { final boolean isCheckboxList = lineStartsWithCheckbox(line, listType); final boolean isPlainList = !isCheckboxList && line.startsWith(listType.listSymbolWithTrailingSpace); if (isPlainList || isCheckboxList) { - customText = isPlainList ? listType.listSymbolWithTrailingSpace : listType.checkboxUncheckedWithTrailingSpace; + builder.append(isPlainList ? listType.listSymbolWithTrailingSpace : listType.checkboxUncheckedWithTrailingSpace); + customText = builder; isInsert = true; sequenceStart = start + count; return; } } - + final Optional orderedListNumber = getOrderedListNumber(line); if (orderedListNumber.isPresent()) { - customText = (orderedListNumber.get() + 1) + ". "; + customText = builder.append(orderedListNumber.get() + 1).append(". "); isInsert = true; sequenceStart = start + count; } diff --git a/markdown/src/test/java/it/niedermann/android/markdown/markwon/textwatcher/AutoContinuationTextWatcherTest.java b/markdown/src/test/java/it/niedermann/android/markdown/markwon/textwatcher/AutoContinuationTextWatcherTest.java index 9b28bf3e..4069d224 100644 --- a/markdown/src/test/java/it/niedermann/android/markdown/markwon/textwatcher/AutoContinuationTextWatcherTest.java +++ b/markdown/src/test/java/it/niedermann/android/markdown/markwon/textwatcher/AutoContinuationTextWatcherTest.java @@ -1,6 +1,5 @@ package it.niedermann.android.markdown.markwon.textwatcher; -import android.os.Looper; import android.view.KeyEvent; import android.widget.EditText; @@ -9,7 +8,6 @@ import androidx.test.core.app.ApplicationProvider; import junit.framework.TestCase; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -19,8 +17,6 @@ import org.robolectric.RobolectricTestRunner; import it.niedermann.android.markdown.markwon.MarkwonMarkdownEditor; import it.niedermann.android.markdown.model.EListType; -import static org.robolectric.Shadows.shadowOf; - @RunWith(RobolectricTestRunner.class) public class AutoContinuationTextWatcherTest extends TestCase { @@ -86,10 +82,25 @@ public class AutoContinuationTextWatcherTest extends TestCase { } @Test - public void shouldContinueNestedLists() { - this.editText.setText("- [ ] Parent\n - [x] Child"); + public void shouldContinueNestedListsAtTheSameIndention() { + this.editText.setText("- [ ] Parent\n - [x] Child\n - [ ] Third"); pressEnter(26); - assertText("- [ ] Parent\n - [x] Child\n- [ ] ", 33); + assertText("- [ ] Parent\n - [x] Child\n - [ ] \n - [ ] Third", 35); + + this.editText.setText("- [ ] Parent\n - [x] Child\n - [ ] Third"); + pressEnter(42); + assertText("- [ ] Parent\n - [x] Child\n - [ ] Third\n - [ ] ", 53); + } + + @Test + public void shouldRemoveAutoContinuedListItemWhenPressingEnterMultipleTimes() { + this.editText.setText("- Foo"); + pressEnter(5); + assertText("- Foo\n- ", 8); + pressEnter(8); + assertText("- Foo\n\n", 7); + pressEnter(7); + assertText("- Foo\n\n\n", 8); } @Test