Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diff lines when deciding what blocks to update #336

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import org.commonmark.parser.IncludeSourceSpans
import org.commonmark.parser.Parser
import org.commonmark.renderer.text.TextContentRenderer
import org.intellij.lang.annotations.Language
import org.jetbrains.annotations.TestOnly
import org.jetbrains.annotations.VisibleForTesting
import org.jetbrains.jewel.foundation.ExperimentalJewelApi
import org.jetbrains.jewel.markdown.InlineMarkdown
import org.jetbrains.jewel.markdown.MarkdownBlock
Expand Down Expand Up @@ -56,12 +58,13 @@ public class MarkdownProcessor(
public constructor(vararg extensions: MarkdownProcessorExtension) : this(extensions.toList())

private val commonMarkParser = Parser.builder()
.extensions(extensions.map { it.parserExtension })
.also {
.let { builder ->
builder.extensions(extensions.map(MarkdownProcessorExtension::parserExtension))
if (optimizeEdits) {
it.includeSourceSpans(IncludeSourceSpans.BLOCKS)
builder.includeSourceSpans(IncludeSourceSpans.BLOCKS)
}
}.build()
builder.build()
}

private val textContentRenderer =
TextContentRenderer.builder()
Expand All @@ -72,6 +75,9 @@ public class MarkdownProcessor(

private var currentState = State(emptyList(), emptyList(), emptyList())

@TestOnly
internal fun getCurrentIndexesInTest() = currentState.indexes

/**
* Parses a Markdown document, translating from CommonMark 0.31.2
* to a list of [MarkdownBlock]. Inline Markdown in leaf nodes
Expand Down Expand Up @@ -101,11 +107,17 @@ public class MarkdownProcessor(
* @see DefaultInlineMarkdownRenderer
*/
public fun processMarkdownDocument(@Language("Markdown") rawMarkdown: String): List<MarkdownBlock> {
if (!optimizeEdits) {
return textToBlocks(rawMarkdown).mapNotNull { child ->
child.tryProcessMarkdownBlock()
}
return if (!optimizeEdits) {
textToBlocks(rawMarkdown)
} else {
processWithQuickEdits(rawMarkdown)
}.mapNotNull { child ->
child.tryProcessMarkdownBlock()
}
}

@VisibleForTesting
internal fun processWithQuickEdits(@Language("Markdown") rawMarkdown: String): List<Block> {
val (previousLines, previousBlocks, previousIndexes) = currentState
val newLines = rawMarkdown.lines()
val nLinesDelta = newLines.size - previousLines.size
Expand Down Expand Up @@ -143,6 +155,10 @@ public class MarkdownProcessor(
currLastBlock = i
currLastLine = begin
}
if (firstLine > lastLine + nLinesDelta) {
// no change
return previousBlocks
}
val updatedText = newLines.subList(firstLine, lastLine + nLinesDelta).joinToString("\n", postfix = "\n")
val updatedBlocks: List<Block> = textToBlocks(updatedText)
val updatedIndexes =
Expand All @@ -163,12 +179,9 @@ public class MarkdownProcessor(
updatedBlocks +
previousBlocks.subList(lastBlock, previousBlocks.size)
)
val result = newBlocks.mapNotNull { child ->
child.tryProcessMarkdownBlock()
}
val newIndexes = previousIndexes.subList(0, firstBlock) + updatedIndexes + suffixIndexes
currentState = State(newLines, newBlocks, newIndexes)
return result
return newBlocks
}

private fun textToBlocks(strings: String): List<Block> {
Expand Down
Loading