go-diff icon indicating copy to clipboard operation
go-diff copied to clipboard

Line-diff is incorrect, missing delete operations

Open dhbrojas opened this issue 11 months ago • 1 comments

The following code should produce "delete" operations.

func TestDiffMatchPatchSemantics(t *testing.T) {
	aLines := []string{
		"        .lock()\n",
		"        .unwrap()\n",
		"        .iter()\n",
		"        .rev()\n",
		"        .fold(vec![], |mut acc, event| {\n",
		"            // Group contiguous to the same\n",
		"            acc.push(ext::WorkspaceEvent::TextDocumentChange {\n",
		"                uri: \"\".into(),\n",
		"                hunks: vec![],\n",
		"            });\n",
		"            acc\n",
	}
	bLines := []string{
		"        .lock()\n",
		"        .unwrap()\n",
		"        .iter()\n",
		"        .rev()\n",
		"        .fold(vec![], |mut acc, event| {\n",
		"            // Group contiguous to the same uri\n",
		"            if let Some(last) = acc.last_mut() {\n",
		"                if last.uri == event.uri {\n",
		"                    last.hunks.extend(event.hunks);\n",
		"                    return acc;\n",
		"                }\n",
		"            }\n",
		"            acc.push(ext::WorkspaceEvent::TextDocumentChange {\n",
		"                uri: event.uri.clone(),\n",
		"                hunks: event.hunks,\n",
		"            });\n",
		"            acc\n",
	}
	a := strings.Join(aLines, "")
	b := strings.Join(bLines, "")

	dmp := diffmatchpatch.New()
	charsA, charsB, lineArray := dmp.DiffLinesToChars(a, b)
	diff := dmp.DiffMain(charsA, charsB, false)
	diff = dmp.DiffCharsToLines(diff, lineArray)

	for i, op := range diff {
		fmt.Println(fmt.Sprintf("(%d)%s%s", i, op.Type.String(), op.Text))
	}
}

However, the output is the following and does not contain such delete operations.

(0)Equal        .lock()
        .unwrap()
        .iter()
        .rev()
        .fold(vec![], |mut acc, event| {
(1)Insert            // Group contiguous to the same uri
            if let Some(last) = acc.last_mut() {
                if last.uri == event.uri {
                    last.hunks.extend(event.hunks);
        .lock()
(2)Equal            // Group contiguous to the same
(3)Insert        .lock()
(4)Equal            acc.push(ext::WorkspaceEvent::TextDocumentChange {
(5)Insert        .lock()
(6)Equal                uri: "".into(),
(7)Insert            acc.push(ext::WorkspaceEvent::TextDocumentChange {
        .lock()
(8)Equal                hunks: vec![],
(9)Insert                hunks: event.hunks,
(10)Equal            });
            acc

Am I using the API wrong?

dhbrojas avatar Mar 16 '25 16:03 dhbrojas

Works on 1.1.0

dhbrojas avatar Mar 16 '25 16:03 dhbrojas