Skip to content

Commit

Permalink
1.0.79 (#84)
Browse files Browse the repository at this point in the history
* 1.0.79

* Update AddSaveLinks.kt

* 1. `generateDiff` function:
   - The function has been rewritten to use a more efficient algorithm.
   - Instead of using mutable lists and removing elements, it now uses indices to iterate through the original and modified lines.
   - The new implementation should be more memory-efficient and potentially faster.

2. `generateUnifiedDiff` function:
   - Minor changes to improve the handling of context lines around changes.
   - Adjustments to prevent potential index out of bounds errors.
   - More precise filtering of unchanged lines when adding context.

Overall, these changes aim to improve the performance and correctness of the diff generation process. The new implementation of `generateDiff` should be able to handle larger files more efficiently, while the changes to `generateUnifiedDiff` make it more robust and accurate in generating the unified diff format.
  • Loading branch information
acharneski authored Jun 23, 2024
1 parent 6753774 commit 332c1a7
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 850 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ Maven:
<dependency>
<groupId>com.simiacryptus</groupId>
<artifactId>skyenet-webui</artifactId>
<version>1.0.61</version>
<version>1.0.62</version>
</dependency>
```

Gradle:

```groovy
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.61'
implementation group: 'com.simiacryptus', name: 'skyenet', version: '1.0.62'
```

```kotlin
implementation("com.simiacryptus:skyenet:1.0.61")
implementation("com.simiacryptus:skyenet:1.0.62")
```

### 🌟 To Use
Expand Down
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ val hsqldb_version = "2.7.2"

dependencies {

implementation(group = "com.simiacryptus", name = "jo-penai", version = "1.0.61")
implementation(group = "com.simiacryptus", name = "jo-penai", version = "1.0.62")
implementation(group = "org.hsqldb", name = "hsqldb", version = hsqldb_version)

implementation("org.apache.commons:commons-text:1.11.0")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Gradle Releases -> https://github.com/gradle/gradle/releases
libraryGroup = com.simiacryptus.skyenet
libraryVersion = 1.0.78
libraryVersion = 1.0.79
gradleVersion = 7.6.1
2 changes: 1 addition & 1 deletion webui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ val jetty_version = "11.0.18"
val jackson_version = "2.17.0"
dependencies {

implementation(group = "com.simiacryptus", name = "jo-penai", version = "1.0.61")
implementation(group = "com.simiacryptus", name = "jo-penai", version = "1.0.62")

implementation(project(":core"))
implementation(project(":kotlin"))
Expand Down
6 changes: 1 addition & 5 deletions webui/src/main/kotlin/com/simiacryptus/diff/AddSaveLinks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ fun SocketManagerBase.addSaveLinks(
ui: ApplicationInterface,
handle: (Path, String) -> Unit,
): String {
val diffPattern =
"""(?s)(?<![^\n])#+\s*([^\n]+)\n```[^\n]*\n(.*?)```""".toRegex() // capture filename

val diffPattern2 =
"""(?s)(?<![^\n])#+\s*(?:[^\n]+[:\-]\s+)?([^\n]+)(?:[^`]+`?)*\n```[^\n]*\n(.*?)```""".toRegex() // capture filename
val diffPattern = """(?s)(?<![^\n])#+\s*([^\n]+)\n```[^\n]*\n(.*?)```""".toRegex()
val matches = diffPattern.findAll(response).distinct()
val withLinks = matches.fold(response) { markdown, diffBlock ->
val filename = extractFilename(diffBlock.groupValues[1])
Expand Down
113 changes: 67 additions & 46 deletions webui/src/main/kotlin/com/simiacryptus/diff/DiffUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,55 +37,65 @@ object DiffUtil {
* @return A list of DiffResult indicating the differences.
*/
fun generateDiff(original: List<String>, modified: List<String>): List<PatchLine> {
if (original == modified) return modified.withIndex().map { (i, v) -> PatchLine(Unchanged, i, v) }
val original = original.withIndex().map { (i, v) -> PatchLine(Unchanged, i, v) }.toMutableList()
val modified = modified.withIndex().map { (i, v) -> PatchLine(Unchanged, i, v) }.toMutableList()
val originalLines = original.withIndex().map { (i, v) -> PatchLine(Unchanged, i, v) }
val modifiedLines = modified.withIndex().map { (i, v) -> PatchLine(Unchanged, i, v) }
val patchLines = mutableListOf<PatchLine>()
var i = 0
var j = 0

while (original.isNotEmpty() && modified.isNotEmpty()) {
val originalLine = original.first()
val modifiedLine = modified.first()
while (i < originalLines.size && j < modifiedLines.size) {
val originalLine = originalLines[i]
val modifiedLine = modifiedLines[j]

if (originalLine == modifiedLine) {
patchLines.add(PatchLine(Unchanged, originalLine.lineNumber, originalLine.line))
original.removeAt(0)
modified.removeAt(0)
i++
j++
} else {
val originalIndex = original.indexOf(modifiedLine).let { if (it == -1) null else it }
val modifiedIndex = modified.indexOf(originalLine).let { if (it == -1) null else it }
val originalIndex = originalLines.subList(i, originalLines.size).indexOf(modifiedLine).let { if (it == -1) null else it + i }
val modifiedIndex = modifiedLines.subList(j, modifiedLines.size).indexOf(originalLine).let { if (it == -1) null else it + j }

if (originalIndex != null && modifiedIndex != null) {
if (originalIndex < modifiedIndex) {
while (original.first() != modifiedLine) {
patchLines.add(PatchLine(Deleted, original.first().lineNumber, original.first().line))
original.removeAt(0)
while (i < originalIndex) {
patchLines.add(PatchLine(Deleted, originalLines[i].lineNumber, originalLines[i].line))
i++
}
} else {
while (modified.first() != originalLine) {
patchLines.add(PatchLine(Added, modified.first().lineNumber, modified.first().line))
modified.removeAt(0)
while (j < modifiedIndex) {
patchLines.add(PatchLine(Added, modifiedLines[j].lineNumber, modifiedLines[j].line))
j++
}
}
} else if (originalIndex != null) {
while (original.first() != modifiedLine) {
patchLines.add(PatchLine(Deleted, original.first().lineNumber, original.first().line))
original.removeAt(0)
while (i < originalIndex) {
patchLines.add(PatchLine(Deleted, originalLines[i].lineNumber, originalLines[i].line))
i++
}
} else if (modifiedIndex != null) {
while (modified.first() != originalLine) {
patchLines.add(PatchLine(Added, modified.first().lineNumber, modified.first().line))
modified.removeAt(0)
while (j < modifiedIndex) {
patchLines.add(PatchLine(Added, modifiedLines[j].lineNumber, modifiedLines[j].line))
j++
}
} else {
patchLines.add(PatchLine(Deleted, originalLine.lineNumber, originalLine.line))
original.removeAt(0)
patchLines.add(PatchLine(Added, modifiedLine.lineNumber, modifiedLine.line))
modified.removeAt(0)
patchLines.add(PatchLine(Deleted, originalLines[i].lineNumber, originalLines[i].line))
patchLines.add(PatchLine(Added, modifiedLines[j].lineNumber, modifiedLines[j].line))
i++
j++
}
}
}
patchLines.addAll(original.map { PatchLine(Deleted, it.lineNumber, it.line) })
patchLines.addAll(modified.map { PatchLine(Added, it.lineNumber, it.line) })

while (i < originalLines.size) {
patchLines.add(PatchLine(Deleted, originalLines[i].lineNumber, originalLines[i].line))
i++
}

while (j < modifiedLines.size) {
patchLines.add(PatchLine(Added, modifiedLines[j].lineNumber, modifiedLines[j].line))
j++
}

return patchLines
}

Expand All @@ -99,32 +109,43 @@ object DiffUtil {
* @return A formatted string representing the diff.
*/
fun formatDiff(patchLines: List<PatchLine>, contextLines: Int = 3): String {
val patchList = patchLines.withIndex().filter { (idx, lineDiff) ->
val patchList = mutableListOf<PatchLine>()
var inChange = false
var changeStart = 0
var changeEnd = 0

patchLines.forEachIndexed { idx, lineDiff ->
when (lineDiff.type) {
Added -> true
Deleted -> true
Added, Deleted -> {
if (!inChange) {
inChange = true
changeStart = maxOf(0, idx - contextLines)
patchList.addAll(patchLines.subList(changeStart, idx).filter { it.type == Unchanged })
}
changeEnd = minOf(patchLines.size, idx + contextLines + 1)
patchList.add(lineDiff)
}
Unchanged -> {
val distBackwards =
patchLines.subList(0, idx).indexOfLast { it.type != Unchanged }
.let { if (it == -1) null else idx - it }
val distForwards = patchLines.subList(idx, patchLines.size).indexOfFirst { it.type != Unchanged }
.let { if (it == -1) null else it }
(null != distBackwards && distBackwards <= contextLines) || (null != distForwards && distForwards <= contextLines)
if (inChange) {
if (idx >= changeEnd) {
inChange = false
patchList.addAll(patchLines.subList(maxOf(changeEnd, idx - contextLines), idx))
}
}
}
}
}.map { it.value }.toTypedArray()
}

if (inChange) {
patchList.addAll(patchLines.subList(maxOf(changeEnd - contextLines, patchList.size), minOf(changeEnd, patchLines.size)))
}

return patchList.withIndex().joinToString("\n") { (idx, lineDiff) ->
when {
idx == 0 -> ""
lineDiff.type != Unchanged || patchList[idx - 1].type != Unchanged -> ""
patchList[idx - 1].lineNumber + 1 < lineDiff.lineNumber -> "...\n"
else -> ""
} + when (lineDiff.type) {
return patchList.joinToString("\n") { lineDiff ->
when (lineDiff.type) {
Added -> "+ ${lineDiff.line}"
Deleted -> "- ${lineDiff.line}"
Unchanged -> " ${lineDiff.line}"
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ object IterativePatchUtil {

// Step 3: Establish a distance metric for matches based on Levenshtein distance and distance to established links.
// Use this to establish the links based on a shortest-first policy and iterate until no more good matches are found.
linkByLevenshteinDistance(sourceLines, patchLines)
//linkByLevenshteinDistance(sourceLines, patchLines)

// Generate the patched text using the established links
return generatePatchedTextUsingLinks(sourceLines, patchLines)
Expand All @@ -71,6 +71,11 @@ object IterativePatchUtil {
val patchedTextBuilder = StringBuilder()
val sourceLineBuffer = sourceLines.toMutableList()

// Add any leading lines from the source that are not in the patch
while (sourceLineBuffer.firstOrNull()?.matchingLine == null) {
patchedTextBuilder.appendLine(sourceLineBuffer.removeFirst().line)
}

// Add any leading 'add' lines from the patch
val patchLines = patchLines.toMutableList()
while (patchLines.firstOrNull()?.type == LineType.ADD) {
Expand Down Expand Up @@ -108,6 +113,7 @@ object IterativePatchUtil {
return patchedTextBuilder.toString().trimEnd()
}

// ... rest of the file remains unchanged
/**
* Links lines between the source and the patch that are unique and match exactly.
* @param sourceLines The source lines.
Expand Down
Loading

0 comments on commit 332c1a7

Please sign in to comment.