Skip to content
Merged

wip #38

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
1 change: 1 addition & 0 deletions README.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
updateTabs();
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ open class FuzzyPatchMatcher(
* @return The normalized string.
*/
open fun normalizeLine(line: String): String {
return line.trimEnd().replace("\\s{2,}".toRegex(), " ")
return line.trim().replace("\\s{2,}".toRegex(), " ")
}

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

LineRecord(
LineRecord(
index = index, line = run {
when {
content.startsWith("+++") || content.startsWith("---") || content.startsWith("@@") -> null
content.startsWith("+ ") && !content.startsWith("+ ") -> content.substring(2)
content.startsWith("- ") && !content.startsWith("- ") -> content.substring(2)
content.startsWith("+") -> content.substring(1)
content.startsWith("-") -> content.substring(1)
else -> content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ class FuzzyPatchMatcherTest {
fun testCases() = listOf(
"/patch_exact_match.json",
"/patch_add_line.json",
"/patch_append_line.json",
"/patch_prepend_line.json",
"/patch_modify_line.json",
"/yaml_min_repro.json",
"/patch_remove_line.json",
// "/patch_add_2_lines_variant_2.json",
// "/patch_add_2_lines_variant_3.json",
"/patch_from_data_1.json",
"/patch_from_data_2.json",
"/yaml_1.json"
"/patch_add_2_lines_variant_2.json",
"/patch_add_2_lines_variant_3.json",
"/patch_inner_block.json",
"/patch_append_to_empty_file.json",
)
}

Expand Down
7 changes: 1 addition & 6 deletions core/src/test/kotlin/com/simiacryptus/diff/PatchTestCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@ data class PatchTestCase(
val errors: String?
) {
companion object {

fun test(resourceName: String, patcher: PatchProcessor) {
fun normalize(text: String) = text.trim().replace("\r\n", "\n")
val stream = patcher.javaClass.getResourceAsStream(resourceName)
?: throw IllegalArgumentException("Resource not found: $resourceName")
val testCase: PatchTestCase = JsonUtil.fromJson(String(stream.readAllBytes()), PatchTestCase::class.java)
if (!testCase.isValid) {
// Skip invalid test cases
return
}
if (!testCase.isValid) return
val result = patcher.applyPatch(testCase.originalCode, testCase.diff)
Assertions.assertEquals(normalize(testCase.newCode), normalize(result))
}

}
}
8 changes: 8 additions & 0 deletions core/src/test/resources/patch_append_line.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"filename": "test_append_line.txt",
"originalCode": "line1\nline2\nline3",
"diff": "line1\nline2\nline3\n+newLine",
"newCode": "line1\nline2\nline3\nnewLine",
"isValid": true,
"errors": ""
}
8 changes: 8 additions & 0 deletions core/src/test/resources/patch_append_to_empty_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"filename": "test_append_to_empty.txt",
"originalCode": "",
"diff": "+newLine",
"newCode": "newLine",
"isValid": true,
"errors": ""
}
8 changes: 0 additions & 8 deletions core/src/test/resources/patch_from_data_1.json

This file was deleted.

8 changes: 0 additions & 8 deletions core/src/test/resources/patch_from_data_2.json

This file was deleted.

8 changes: 8 additions & 0 deletions core/src/test/resources/patch_inner_block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"filename": "test_minimized.js",
"originalCode": "function foo() {\n var x = 1;\n}",
"diff": "var x = 1;\n+ var y = 2;\n}",
"newCode": "function foo() {\n var x = 1;\n var y = 2;\n}",
"isValid": true,
"errors": ""
}
8 changes: 8 additions & 0 deletions core/src/test/resources/patch_prepend_line.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"filename": "test_prepend_line.txt",
"originalCode": "line1\nline2\nline3",
"diff": "+newLine\nline1\nline2\nline3",
"newCode": "newLine\nline1\nline2\nline3",
"isValid": true,
"errors": ""
}
Loading
Loading