Recursive AI

Migrate from DiffPlex to OneDiff in C#

Replace DiffPlex’s inline builder with Diff.Lines, then port the result handling, side-by-side views, unified patches, and three-way merges.

OneDiff, a C# text diff and merge library, is free and open source from Recursive AI, under MIT or Apache-2.0. NuGet · GitHub

There’s no compatibility layer: port the code that reads each diff result.

dotnet add package RecursiveAI.OneDiff --version 1.0.1

OneDiff version of DiffPlex’s InlineDiffBuilder example (the original is below).

Program.csOneDiff 1.0.1 · .NET 10
using OneDiff;
 
const string before = "start\ntimeout=10\nend";
const string after = "start\ntimeout=20\nend";
var diff = Diff.Lines(before, after);
foreach (var line in diff.Lines)
{
    var prefix = line.Kind switch
    {
        EditKind.Delete => "- ", EditKind.Insert => "+ ", _ => "  "
    };
    Console.WriteLine(prefix + line.Text);
}
Output
  start
- timeout=10
+ timeout=20
  end

Port the inline line diff

Run the examples in separate .NET 10 console projects. The original below uses DiffPlex 1.9.0 and the inline builder pattern from its README. It sets ignoreWhiteSpace: false explicitly.

dotnet add package DiffPlex --version 1.9.0

Program.csDiffPlex 1.9.0 · .NET 10
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;
 
const string before = "start\ntimeout=10\nend";
const string after = "start\ntimeout=20\nend";
var diff = InlineDiffBuilder.Diff(before, after, ignoreWhiteSpace: false);
foreach (var line in diff.Lines)
{
    var prefix = line.Type switch
    {
        ChangeType.Deleted => "- ", ChangeType.Inserted => "+ ", _ => "  "
    };
    Console.WriteLine(prefix + line.Text);
}
Output
  start
- timeout=10
+ timeout=20
  end

Map the text APIs and result types

These are replacements for the same tasks, not promises of identical token boundaries or output. The pinned OneDiff mapping covers additional chunkers and options.

DiffPlex 1.9.0OneDiff 1.0.1
InlineDiffBuilder.Diff → DiffPaneModel.LinesDiff.Lines → TextDiff.Lines. DiffRun.Text is the line text; DiffRun.Kind is Equal, Delete, or Insert.
DiffPiece.Position, DiffPaneModel.HasDifferencesRead OldLine and NewLine separately: both are one-based and the missing side is null. Use !diff.Script.IsIdentical for differences.
Differ.CreateLineDiffs with whitespace significantDiff.Lines. For low-level changes use TextDiff.Script, whose ranges index tokens. Line tokens include the terminator, so CRLF and LF differ.
Differ.CreateWordDiffs with caller-supplied separatorsDiff.Words uses its own token boundaries. To retain your separators, split both strings yourself and pass the chunks to Diff.Sequences<string>.
SideBySideDiffBuilder.Diff → two pane listsDiff.SideBySide → Rows. Use Left and Right; missing cells are null, not Imaginary entries. DiffPiece.SubPieces maps to SideBySideCell.Parts for word highlights.
UnidiffRenderer.GenerateUnidiff with both ignore flags falseDiff.Unified takes both texts, names, and context size. It has no ignore-case or ignore-whitespace flags or overload taking a precomputed TextDiff.
ThreeWayDiffer.CreateMerge with line chunking and both ignore flags falseDiff.Merge(baseText, ours: oldText, theirs: newText) returns TextMerge. Read !merge.HasConflicts instead of IsSuccessful, and use merge.Conflicts for details rather than ConflictBlocks. merge.Text is a string, not a list of pieces, and includes markers for unresolved conflicts.

Compare a side-by-side row, unified patch, and merge

Each version prints the two cells on the changed row, a patch with one context line, and the number of conflicts when two edits change the same base line. The DiffPlex calls use the unified renderer and three-way differ; the result members below are from the released 1.9.0 package.

Program.csDiffPlex 1.9.0 · .NET 10
using DiffPlex;
using DiffPlex.Chunkers;
using DiffPlex.DiffBuilder;
using DiffPlex.Renderer;
 
const string before = "start\ntimeout=10\nend\n";
const string ours = "start\ntimeout=20\nend\n";
const string theirs = "start\ntimeout=30\nend\n";
 
var view = SideBySideDiffBuilder.Diff(before, ours, false, false);
Console.WriteLine($"{view.OldText.Lines[1].Text} | {view.NewText.Lines[1].Text}");
Console.WriteLine(UnidiffRenderer.GenerateUnidiff(
    before, ours, "before.txt", "after.txt", false, false, 1).TrimEnd());
var merge = new ThreeWayDiffer().CreateMerge(
    before, ours, theirs, false, false, LineChunker.Instance);
Console.WriteLine($"Conflicts: {merge.ConflictBlocks.Count}");
Program.csOneDiff 1.0.1 · .NET 10
using OneDiff;
 
const string before = "start\ntimeout=10\nend\n";
const string ours = "start\ntimeout=20\nend\n";
const string theirs = "start\ntimeout=30\nend\n";
 
var view = Diff.SideBySide(before, ours);
Console.WriteLine($"{view.Rows[1].Left?.Text} | {view.Rows[1].Right?.Text}");
Console.WriteLine(Diff.Unified(before, ours, "before.txt", "after.txt", 1).TrimEnd());
var merge = Diff.Merge(before, ours: ours, theirs: theirs);
Console.WriteLine($"Conflicts: {merge.ConflictCount}");
Output from both programs
timeout=10 | timeout=20
--- before.txt
+++ after.txt
@@ -1,3 +1,3 @@
 start
-timeout=10
+timeout=20
 end
Conflicts: 1

Check behavior before removing DiffPlex

  • Line endings and comparison rules: test LF, CRLF, missing final newlines, and whitespace-only edits. For a display that ignores whitespace, start with Diff.LinesUsing and TokenComparer.IgnoreWhiteSpaceChanges or IgnoreAllWhiteSpace from OneDiff.Tokenization. Check which rule matches your input; neither is an automatic flag translation.
  • Patches: compare the patched result with the intended new text, not just the patch’s spelling. For a normalized display rather than a patch you can apply, render a comparer-based line diff with Diff.ToMarkdown or Diff.ToTerminal.
  • Merges: keep the base, old, and new inputs in that order. Test adjacent edits too: OneDiff treats changes on adjacent lines as a conflict. Check the conflict result before saving merged text.

See the OneDiff getting-started guide and the unified patch application example for the next steps.