mmanela / diffplex

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

Add helper functions #50

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.

Sample

Following is a sample of previous (current) vs new (in PR).

// Old (current).
var differ = new Differ();
var sideBySideBuilder = new SideBySideBuilder(differ);
var result = sideBySideBuilder.BuildDiffModel(oldText, newText);
// New (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

The IDiffer argument of constructors of SideBySideDiffBuilder and InlineDiffBuilder can be null now. A default Differ instance will be used when it is null.

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.

kingcean commented 4 years ago

In fact this pull request only contains the last 4 commits of the above list.

mmanela commented 4 years ago

@kingcean Are any of these changes "breaking" changes?

kingcean commented 4 years ago

The only breaking change (maybe you don't think it is a breaking change) is that the following result is different.

var diffBuilder = new SideBySideDiffBuilder(null);

The new (in PR) implementation is equivalent to following.

var diffBuilder = new SideBySideDiffBuilder(Differ.Instance); // Differ.Instance = new Differ();

The old (current) implementation throws an exception.

throw new ArgumentNullException("differ");

And so does InlineDiffBuilder.

mmanela commented 4 years ago

@kingcean Can you re-merge and push since there are conflicts it seems

kingcean commented 4 years ago

OK.

kingcean commented 4 years ago

This PR has transferred to PR #52 now.