Skip to content

Commit a3d541e

Browse files
authored
wip (#38)
* wip * wip * wip
1 parent 833baab commit a3d541e

File tree

15 files changed

+79
-58
lines changed

15 files changed

+79
-58
lines changed

README.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
updateTabs();

core/src/main/kotlin/com/simiacryptus/cognotik/diff/FuzzyPatchMatcher.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ open class FuzzyPatchMatcher(
376376
* @return The normalized string.
377377
*/
378378
open fun normalizeLine(line: String): String {
379-
return line.trimEnd().replace("\\s{2,}".toRegex(), " ")
379+
return line.trim().replace("\\s{2,}".toRegex(), " ")
380380
}
381381

382382
/**
@@ -487,7 +487,8 @@ open class FuzzyPatchMatcher(
487487
}
488488
// Only add unmatched ADD lines if we had at least some context match
489489
// Otherwise, the patch likely doesn't apply to this file
490-
if (lastMatchedPatchIndex >= 0) {
490+
val isSourceEmpty = sourceLines.isEmpty() || (sourceLines.size == 1 && sourceLines[0].line.isNullOrEmpty())
491+
if (lastMatchedPatchIndex >= 0 || isSourceEmpty) {
491492
patchLines.filter { it.type == ADD && !usedPatchLines.contains(it) }.forEach { line ->
492493
log.debug("Added patch line: {}", line)
493494
patchedText.add(line.line ?: "")
@@ -785,14 +786,17 @@ open class FuzzyPatchMatcher(
785786
val trimmedLine = line.trimStart()
786787
val content = when {
787788
line.startsWith(" ") -> line.substring(2)
789+
line.startsWith(" ") -> line.substring(1)
788790
trimmedLine.startsWith("+") || trimmedLine.startsWith("-") -> trimmedLine
789791
else -> line
790792
}
791793

792-
LineRecord(
794+
LineRecord(
793795
index = index, line = run {
794796
when {
795797
content.startsWith("+++") || content.startsWith("---") || content.startsWith("@@") -> null
798+
content.startsWith("+ ") && !content.startsWith("+ ") -> content.substring(2)
799+
content.startsWith("- ") && !content.startsWith("- ") -> content.substring(2)
796800
content.startsWith("+") -> content.substring(1)
797801
content.startsWith("-") -> content.substring(1)
798802
else -> content

core/src/test/kotlin/com/simiacryptus/diff/FuzzyPatchMatcherTest.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ class FuzzyPatchMatcherTest {
1212
fun testCases() = listOf(
1313
"/patch_exact_match.json",
1414
"/patch_add_line.json",
15+
"/patch_append_line.json",
16+
"/patch_prepend_line.json",
1517
"/patch_modify_line.json",
18+
"/yaml_min_repro.json",
1619
"/patch_remove_line.json",
17-
// "/patch_add_2_lines_variant_2.json",
18-
// "/patch_add_2_lines_variant_3.json",
19-
"/patch_from_data_1.json",
20-
"/patch_from_data_2.json",
21-
"/yaml_1.json"
20+
"/patch_add_2_lines_variant_2.json",
21+
"/patch_add_2_lines_variant_3.json",
22+
"/patch_inner_block.json",
23+
"/patch_append_to_empty_file.json",
2224
)
2325
}
2426

core/src/test/kotlin/com/simiacryptus/diff/PatchTestCase.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,14 @@ data class PatchTestCase(
1313
val errors: String?
1414
) {
1515
companion object {
16-
1716
fun test(resourceName: String, patcher: PatchProcessor) {
1817
fun normalize(text: String) = text.trim().replace("\r\n", "\n")
1918
val stream = patcher.javaClass.getResourceAsStream(resourceName)
2019
?: throw IllegalArgumentException("Resource not found: $resourceName")
2120
val testCase: PatchTestCase = JsonUtil.fromJson(String(stream.readAllBytes()), PatchTestCase::class.java)
22-
if (!testCase.isValid) {
23-
// Skip invalid test cases
24-
return
25-
}
21+
if (!testCase.isValid) return
2622
val result = patcher.applyPatch(testCase.originalCode, testCase.diff)
2723
Assertions.assertEquals(normalize(testCase.newCode), normalize(result))
2824
}
29-
3025
}
3126
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"filename": "test_append_line.txt",
3+
"originalCode": "line1\nline2\nline3",
4+
"diff": "line1\nline2\nline3\n+newLine",
5+
"newCode": "line1\nline2\nline3\nnewLine",
6+
"isValid": true,
7+
"errors": ""
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"filename": "test_append_to_empty.txt",
3+
"originalCode": "",
4+
"diff": "+newLine",
5+
"newCode": "newLine",
6+
"isValid": true,
7+
"errors": ""
8+
}

core/src/test/resources/patch_from_data_1.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

core/src/test/resources/patch_from_data_2.json

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"filename": "test_minimized.js",
3+
"originalCode": "function foo() {\n var x = 1;\n}",
4+
"diff": "var x = 1;\n+ var y = 2;\n}",
5+
"newCode": "function foo() {\n var x = 1;\n var y = 2;\n}",
6+
"isValid": true,
7+
"errors": ""
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"filename": "test_prepend_line.txt",
3+
"originalCode": "line1\nline2\nline3",
4+
"diff": "+newLine\nline1\nline2\nline3",
5+
"newCode": "newLine\nline1\nline2\nline3",
6+
"isValid": true,
7+
"errors": ""
8+
}

0 commit comments

Comments
 (0)