From f538ac20c90e0470d81faf7a6d27735fddf6193f Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Thu, 1 Apr 2021 15:45:05 +1100 Subject: [PATCH] calculate start and end offset of each note part --- .../android/ui/notes/NoteContentTest.kt | 49 ++++++++++--------- .../orgzly/android/ui/notes/NoteContent.kt | 23 +++++---- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/app/src/androidTest/java/com/orgzly/android/ui/notes/NoteContentTest.kt b/app/src/androidTest/java/com/orgzly/android/ui/notes/NoteContentTest.kt index 25777267d..f3318c483 100644 --- a/app/src/androidTest/java/com/orgzly/android/ui/notes/NoteContentTest.kt +++ b/app/src/androidTest/java/com/orgzly/android/ui/notes/NoteContentTest.kt @@ -19,7 +19,7 @@ class NoteContentTest { @Test fun emptyLinesShouldStayInSingleSection() { - checkExpected("\n\n", listOf(TextNoteContent("\n\n"))) + checkExpected("\n\n", listOf(TextNoteContent("\n\n", 0, 1))) } @Test @@ -28,9 +28,9 @@ class NoteContentTest { | foo|bar""", listOf( - TextNoteContent("foo\n"), - TableNoteContent("|\n"), - TextNoteContent("\nfoo|bar") + TextNoteContent("foo\n", 0, 3), + TableNoteContent("|\n", 4, 5), + TextNoteContent("\nfoo|bar", 6, 13) )) } @@ -40,14 +40,14 @@ foo|bar""", listOf( |c|d| """, listOf(TableNoteContent("""|a|b| |c|d| -"""))) +""", 0, 11))) } @Test fun singleTableNoFinalNewline() { checkExpected("""|a|b| |c|d|""", listOf(TableNoteContent("""|a|b| -|c|d|"""))) +|c|d|""", 0, 10))) } @Test @@ -55,9 +55,9 @@ foo|bar""", listOf( checkExpected("""foo | bar""", listOf( - TextNoteContent("foo\n"), - TableNoteContent("|\n"), - TextNoteContent("bar") + TextNoteContent("foo\n", 0, 3), + TableNoteContent("|\n", 4, 5), + TextNoteContent("bar", 6, 8) )) } @@ -68,9 +68,9 @@ bar""", listOf( | bar """, listOf( - TextNoteContent("\n"), - TableNoteContent("|\n"), - TextNoteContent("bar\n") + TextNoteContent("\n", 0, 0), + TableNoteContent("|\n", 1, 2), + TextNoteContent("bar\n", 3, 6) )) } @@ -79,9 +79,9 @@ bar checkExpected("""|zoo| |zog|""", listOf( - TableNoteContent("|zoo|\n"), - TextNoteContent("\n"), - TableNoteContent("|zog|") + TableNoteContent("|zoo|\n",0,5), + TextNoteContent("\n",6,6), + TableNoteContent("|zog|",7,11) )) } @@ -91,9 +91,9 @@ bar | chops""", listOf( - TextNoteContent("foo\n"), - TableNoteContent("|\n"), - TextNoteContent("\nchops") + TextNoteContent("foo\n",0,3), + TableNoteContent("|\n",4,5), + TextNoteContent("\nchops",6,11) )) } @@ -109,11 +109,11 @@ text3c |table4| text5 """, listOf( - TextNoteContent("text1\n"), - TableNoteContent("|table2a|\n|table2b|\n"), - TextNoteContent("text3a\ntext3b\ntext3c\n"), - TableNoteContent("|table4|\n"), - TextNoteContent("text5\n") + TextNoteContent("text1\n",0,5), + TableNoteContent("|table2a|\n|table2b|\n",6,25), + TextNoteContent("text3a\ntext3b\ntext3c\n",26,46), + TableNoteContent("|table4|\n",47,55), + TextNoteContent("text5\n",56,61) )) } @@ -150,5 +150,8 @@ text5 assertEquals(input, roundTripped) + actual.forEach { + assertEquals(it.text, input.substring(it.startOffset, it.endOffset + 1)) + } } } diff --git a/app/src/main/java/com/orgzly/android/ui/notes/NoteContent.kt b/app/src/main/java/com/orgzly/android/ui/notes/NoteContent.kt index 0c4ab9f85..d61ab79e3 100644 --- a/app/src/main/java/com/orgzly/android/ui/notes/NoteContent.kt +++ b/app/src/main/java/com/orgzly/android/ui/notes/NoteContent.kt @@ -3,14 +3,12 @@ package com.orgzly.android.ui.notes /** * Represents a subsection of note content: either text, or a table */ -sealed class NoteContent { +sealed class NoteContent(val text: String, val startOffset: Int, val endOffset: Int) { - abstract val text: String - data class TextNoteContent(override val text: String) : NoteContent() { - } + data class TextNoteContent(val s_text: String, val s_startOffset: Int, val s_endOffset: Int) : NoteContent(s_text, s_startOffset, s_endOffset) - data class TableNoteContent(override val text: String) : NoteContent() { + data class TableNoteContent(val s_text: String, val s_startOffset: Int, val s_endOffset: Int) : NoteContent(s_text, s_startOffset, s_endOffset) { fun reformat() { // placeholder - but would fix all the spacing, missing cells, etc. Complicated @@ -55,12 +53,14 @@ sealed class NoteContent { } currentIsTable && !previousIsTable -> { currentTable = it + "\n" - list.add(TextNoteContent(currentText)) + val s_startOffset = getLastOffset(list) + list.add(TextNoteContent(currentText, s_startOffset, s_startOffset + currentText.length - 1)) currentText = "" } !currentIsTable && previousIsTable -> { currentText = it + "\n" - list.add(TableNoteContent(currentTable)) + val s_startOffset = getLastOffset(list) + list.add(TableNoteContent(currentTable, s_startOffset, s_startOffset + currentTable.length - 1)) currentTable = "" } !currentIsTable && !previousIsTable -> { @@ -71,18 +71,23 @@ sealed class NoteContent { } if (linesForParsing.isNotEmpty()) { + + val endOffsetAdjustment = if (missingLastNewline) 2 else 1 + if (previousIsTable) { list.add(TableNoteContent(if (missingLastNewline) { currentTable.dropLast(1) - } else currentTable)) + } else currentTable, getLastOffset(list), getLastOffset(list) + currentTable.length - endOffsetAdjustment)) } else { list.add(TextNoteContent(if (missingLastNewline) { currentText.dropLast(1) - } else currentText)) + } else currentText, getLastOffset(list), getLastOffset(list) + currentText.length - endOffsetAdjustment)) } } return list } + + private fun getLastOffset(list: MutableList) = if (list.isEmpty()) 0 else list.last().endOffset + 1 } } \ No newline at end of file