Skip to content

Commit

Permalink
calculate start and end offset of each note part
Browse files Browse the repository at this point in the history
  • Loading branch information
artbristol committed Apr 1, 2021
1 parent 8081ff4 commit f538ac2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
))
}

Expand All @@ -40,24 +40,24 @@ 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
fun singleLineTextTableText() {
checkExpected("""foo
|
bar""", listOf(
TextNoteContent("foo\n"),
TableNoteContent("|\n"),
TextNoteContent("bar")
TextNoteContent("foo\n", 0, 3),
TableNoteContent("|\n", 4, 5),
TextNoteContent("bar", 6, 8)
))
}

Expand All @@ -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)
))
}

Expand All @@ -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)
))
}

Expand All @@ -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)
))
}

Expand All @@ -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)
))
}

Expand Down Expand Up @@ -150,5 +150,8 @@ text5

assertEquals(input, roundTripped)

actual.forEach {
assertEquals(it.text, input.substring(it.startOffset, it.endOffset + 1))
}
}
}
23 changes: 14 additions & 9 deletions app/src/main/java/com/orgzly/android/ui/notes/NoteContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 -> {
Expand All @@ -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<NoteContent>) = if (list.isEmpty()) 0 else list.last().endOffset + 1
}
}

0 comments on commit f538ac2

Please sign in to comment.