Skip to content

Commit

Permalink
Fix diff breaks when long paragraphs replaced with empty (#346)
Browse files Browse the repository at this point in the history
This happens because a change scanner is trying to access an element
from the previous paragraph. And if the new one is shorter it misses
the index resulting in a crash.

- `content if empty` test added
  • Loading branch information
obask authored Apr 7, 2024
1 parent 76331d9 commit ea1fa23
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class MarkdownProcessor(
outerLoop@ for ((i, spans) in previousIndexes.withIndex()) {
val (_, end) = spans
for (j in currFirstLine..end) {
if (newLines[j] != previousLines[j]) {
if (j < 0 || j >= newLines.size || newLines[j] != previousLines[j]) {
break@outerLoop
}
}
Expand All @@ -125,7 +125,8 @@ public class MarkdownProcessor(
outerLoop@ for ((i, spans) in previousIndexes.withIndex().reversed()) {
val (begin, _) = spans
for (j in begin until currLastLine) {
if (previousLines[j] != newLines[j + nLinesDelta]) {
val newIndex = j + nLinesDelta
if (newIndex < 0 || newIndex >= newLines.size || previousLines[j] != newLines[newIndex]) {
break@outerLoop
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.junit.Test

private val rawMarkdown = """
Paragraph 0
continue p0
# Header 1
Paragraph 2
* list item 3-1
Expand All @@ -32,8 +33,12 @@ private val rawMarkdown = """
Paragraph 7
Paragraph 8
continue p8
""".trimIndent()

@Suppress(
"LargeClass", // Detekt triggers on files > 600 lines
)
class MarkdownProcessorTest {

private val htmlRenderer = HtmlRenderer.builder().build()
Expand All @@ -45,6 +50,7 @@ class MarkdownProcessorTest {
val secondRun = processor.processWithQuickEdits(
"""
Paragraph 0
continue p0
# Header 1
Paragraph 2
Expand All @@ -57,7 +63,8 @@ class MarkdownProcessorTest {
assertNotSame(firstRun[2], secondRun[2])
assertHtmlEquals(
"""
<p>Paragraph 0</p>
<p>Paragraph 0
continue p0</p>
<h1>Header 1</h1>
<p>Paragraph 2</p>
<ul>
Expand Down Expand Up @@ -94,6 +101,7 @@ class MarkdownProcessorTest {
Paragraph 7
Paragraph 8
continue p8
""".trimIndent(),
)
assertHtmlEquals(
Expand All @@ -113,7 +121,8 @@ class MarkdownProcessorTest {
line 6-2
</code></pre>
<p>Paragraph 7</p>
<p>Paragraph 8</p>
<p>Paragraph 8
continue p8</p>
""".trimIndent(),
secondRun,
Expand All @@ -130,6 +139,7 @@ class MarkdownProcessorTest {
val secondRun = processor.processWithQuickEdits(
"""
Paragraph 0
continue p0
# Header 1
Paragraph 2
* list item 3-1
Expand All @@ -147,11 +157,13 @@ class MarkdownProcessorTest {
Paragraph 7
Paragraph *CHANGE*
continue p8
""".trimIndent(),
)
assertHtmlEquals(
"""
<p>Paragraph 0</p>
<p>Paragraph 0
continue p0</p>
<h1>Header 1</h1>
<p>Paragraph 2</p>
<ul>
Expand All @@ -166,7 +178,8 @@ class MarkdownProcessorTest {
line 6-2
</code></pre>
<p>Paragraph 7</p>
<p>Paragraph <em>CHANGE</em></p>
<p>Paragraph <em>CHANGE</em>
continue p8</p>
""".trimIndent(),
secondRun,
Expand All @@ -184,6 +197,7 @@ class MarkdownProcessorTest {
val secondRun = processor.processWithQuickEdits(
"""
Paragraph 0
continue p0
# Header 1
Paragraph 2
* list item 3-1
Expand All @@ -201,11 +215,13 @@ class MarkdownProcessorTest {
Paragraph 7
Paragraph 8
continue p8
""".trimIndent(),
)
assertHtmlEquals(
"""
<p>Paragraph 0</p>
<p>Paragraph 0
continue p0</p>
<h1>Header 1</h1>
<p>Paragraph 2</p>
<ul>
Expand All @@ -220,7 +236,8 @@ class MarkdownProcessorTest {
line 6-2
</code></pre>
<p>Paragraph 7</p>
<p>Paragraph 8</p>
<p>Paragraph 8
continue p8</p>
""".trimIndent(),
secondRun,
Expand All @@ -240,6 +257,7 @@ class MarkdownProcessorTest {
val secondRun = processor.processWithQuickEdits(
"""
Paragraph 0
continue p0
# Header 1
Paragraph 2
* list item 3-1
Expand All @@ -256,11 +274,13 @@ class MarkdownProcessorTest {
```
Paragraph 7
Paragraph 8
continue p8
""".trimIndent(),
)
assertHtmlEquals(
"""
<p>Paragraph 0</p>
<p>Paragraph 0
continue p0</p>
<h1>Header 1</h1>
<p>Paragraph 2</p>
<ul>
Expand All @@ -275,7 +295,8 @@ class MarkdownProcessorTest {
line 6-2
</code></pre>
<p>Paragraph 7
Paragraph 8</p>
Paragraph 8
continue p8</p>
""".trimIndent(),
secondRun,
Expand All @@ -292,6 +313,7 @@ class MarkdownProcessorTest {
val secondRun = processor.processWithQuickEdits(
"""
Paragraph 0
continue p0
# Header 1
Paragraph 2
* list item 3-1
Expand All @@ -308,11 +330,13 @@ class MarkdownProcessorTest {
Paragraph 7
Paragraph 8
continue p8
""".trimIndent(),
)
assertHtmlEquals(
"""
<p>Paragraph 0</p>
<p>Paragraph 0
continue p0</p>
<h1>Header 1</h1>
<p>Paragraph 2</p>
<ul>
Expand All @@ -327,7 +351,8 @@ class MarkdownProcessorTest {
line 6-2
</code></pre>
<p>Paragraph 7</p>
<p>Paragraph 8</p>
<p>Paragraph 8
continue p8</p>
""".trimIndent(),
secondRun,
Expand All @@ -345,6 +370,7 @@ class MarkdownProcessorTest {
val secondRun = processor.processWithQuickEdits(
"""
Paragraph 0
continue p0
# Header 1
Paragraph 2
* list item 3-1
Expand All @@ -357,11 +383,13 @@ class MarkdownProcessorTest {
Paragraph 7
Paragraph 8
continue p8
""".trimIndent(),
)
assertHtmlEquals(
"""
<p>Paragraph 0</p>
<p>Paragraph 0
continue p0</p>
<h1>Header 1</h1>
<p>Paragraph 2</p>
<ul>
Expand All @@ -373,7 +401,8 @@ class MarkdownProcessorTest {
line 6-2
</code></pre>
<p>Paragraph 7</p>
<p>Paragraph 8</p>
<p>Paragraph 8
continue p8</p>
""".trimIndent(),
secondRun,
Expand All @@ -391,6 +420,7 @@ class MarkdownProcessorTest {
val firstRun = processor.processWithQuickEdits(rawMarkdown)
val secondDocument = """
Paragraph 0
continue p0
# Header 1
Paragraph 2
* list item 3-1
Expand All @@ -412,13 +442,15 @@ class MarkdownProcessorTest {
Paragraph 7
Paragraph 8
continue p8
""".trimIndent()
val secondRun = processor.processWithQuickEdits(
secondDocument,
)
assertHtmlEquals(
"""
<p>Paragraph 0</p>
<p>Paragraph 0
continue p0</p>
<h1>Header 1</h1>
<p>Paragraph 2</p>
<ul>
Expand All @@ -434,7 +466,8 @@ class MarkdownProcessorTest {
line 6-2
</code></pre>
<p>Paragraph 7</p>
<p>Paragraph 8</p>
<p>Paragraph 8
continue p8</p>
""".trimIndent(),
secondRun,
Expand All @@ -454,7 +487,8 @@ class MarkdownProcessorTest {
val secondRun = processor.processWithQuickEdits(rawMarkdown)
assertHtmlEquals(
"""
<p>Paragraph 0</p>
<p>Paragraph 0
continue p0</p>
<h1>Header 1</h1>
<p>Paragraph 2</p>
<ul>
Expand All @@ -469,7 +503,8 @@ class MarkdownProcessorTest {
line 6-2
</code></pre>
<p>Paragraph 7</p>
<p>Paragraph 8</p>
<p>Paragraph 8
continue p8</p>
""".trimIndent(),
secondRun,
Expand All @@ -484,7 +519,8 @@ class MarkdownProcessorTest {
val secondRun = processor.processWithQuickEdits("\n" + rawMarkdown)
assertHtmlEquals(
"""
<p>Paragraph 0</p>
<p>Paragraph 0
continue p0</p>
<h1>Header 1</h1>
<p>Paragraph 2</p>
<ul>
Expand All @@ -499,7 +535,8 @@ class MarkdownProcessorTest {
line 6-2
</code></pre>
<p>Paragraph 7</p>
<p>Paragraph 8</p>
<p>Paragraph 8
continue p8</p>
""".trimIndent(),
secondRun,
Expand All @@ -508,6 +545,20 @@ class MarkdownProcessorTest {
assertSame(firstRun[1], secondRun[1])
}

/** Regression https://github.com/JetBrains/jewel/issues/344 */
@Test
fun `content if empty`() {
val processor = MarkdownProcessor()
processor.processWithQuickEdits(rawMarkdown)
val secondRun = processor.processWithQuickEdits("")
assertHtmlEquals(
"""
""".trimIndent(),
secondRun,
)
processor.processWithQuickEdits(rawMarkdown)
}

@Test
fun `chained changes`() {
val processor = MarkdownProcessor()
Expand Down

0 comments on commit ea1fa23

Please sign in to comment.