mmanela / diffplex

DiffPlex is Netstandard 1.0+ C# library to generate textual diffs.
Apache License 2.0
996 stars 183 forks source link

Add helper functions #52

Closed kingcean closed 4 years ago

kingcean commented 4 years ago

Add some helper functions and singletons to make the usage of APIs more friendly. It is very useful for most of scenario.

This PR is as same as PR 50.

Sample

Following is a sample of Before vs Now (in PR).

// Before
var differ = new Differ();
var sideBySideBuilder = new SideBySideBuilder(differ);
var result = sideBySideBuilder.BuildDiffModel(oldText, newText);
// Now (in PR)
var result = SideBySideBuilder.Diff(oldText, newText);

And of course, this pull request keeps the old style to allow advanced customization and usages.

Breaking changes

Call the constructors SideBySideDiffBuilder(IDiffer differ) and InlineDiffBuilder(IDiffer differ) with an argument null is no longer throw an exception but use a default Differ instance to fill automatically instead.

var diffBuilder = new SideBySideDiffBuilder(null);

// Before: It throws an exception.
// throw new ArgumentNullException("differ");

// Now (in PR): It is equivalent to following.
// var diffBuilder = new SideBySideDiffBuilder(Differ.Instance);

Helper functions

Singletons

WPF controls

A benefit of the helper functions and singletons is that I add a new WPF control DiffViewer which we can set the old text and new text directly instead of an instance of SideBySideDiffModel or DiffPaneModel.

<diffplex:DiffViewer />

The helper functions and the singletons will also decrease the memory cost for the scenario user create more instances of Differ class, diff builders and chunks.