DiffPlex 1.7.2
dotnet add package DiffPlex --version 1.7.2
NuGet\Install-Package DiffPlex -Version 1.7.2
<PackageReference Include="DiffPlex" Version="1.7.2" />
paket add DiffPlex --version 1.7.2
#r "nuget: DiffPlex, 1.7.2"
// Install DiffPlex as a Cake Addin #addin nuget:?package=DiffPlex&version=1.7.2 // Install DiffPlex as a Cake Tool #tool nuget:?package=DiffPlex&version=1.7.2
DiffPlex
DiffPlex is C# library to generate textual diffs. It targets netstandard1.0+
.
About the API
The DiffPlex library currently exposes two interfaces for generating diffs:
IDiffer
(implemented by theDiffer
class) - This is the core diffing class. It exposes the low level functions to generate differences between texts.ISidebySideDiffer
(implemented by theSideBySideDiffer
class) - This is a higher level interface. It consumes theIDiffer
interface and generates aSideBySideDiffModel
. This is a model which is suited for displaying the differences of two pieces of text in a side by side view.
Examples
For examples of how to use the API please see the the following projects contained in the DiffPlex solution.
For use of the IDiffer
interface see:
SidebySideDiffer.cs
contained in theDiffPlex
Project.UnidiffFormater.cs
contained in theDiffPlex.ConsoleRunner
project.
For use of the ISidebySideDiffer
interface see:
DiffController.cs
and associated MVC views in theWebDiffer
projectTextBoxDiffRenderer.cs
in theSilverlightDiffer
project
Sample code
var diff = InlineDiffBuilder.Diff(before, after);
var savedColor = Console.ForegroundColor;
foreach (var line in diff.Lines)
{
switch (line.Type)
{
case ChangeType.Inserted:
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("+ ");
break;
case ChangeType.Deleted:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("- ");
break;
default:
Console.ForegroundColor = ConsoleColor.Gray; // compromise for dark or light background
Console.Write(" ");
break;
}
Console.WriteLine(line.Text);
}
Console.ForegroundColor = savedColor;
IDiffer Interface
/// <summary>
/// Provides methods for generate differences between texts
/// </summary>
public interface IDiffer
{
/// <summary>
/// Create a diff by comparing text line by line
/// </summary>
/// <param name="oldText">The old text.</param>
/// <param name="newText">The new text.</param>
/// <param name="ignoreWhiteSpace">if set to <c>true</c> will ignore white space when determining if lines are the same.</param>
/// <returns>A DiffResult object which details the differences</returns>
DiffResult CreateLineDiffs(string oldText, string newText, bool ignoreWhiteSpace);
/// <summary>
/// Create a diff by comparing text character by character
/// </summary>
/// <param name="oldText">The old text.</param>
/// <param name="newText">The new text.</param>
/// <param name="ignoreWhitespace">if set to <c>true</c> will treat all whitespace characters are empty strings.</param>
/// <returns>A DiffResult object which details the differences</returns>
DiffResult CreateCharacterDiffs(string oldText, string newText, bool ignoreWhitespace);
/// <summary>
/// Create a diff by comparing text word by word
/// </summary>
/// <param name="oldText">The old text.</param>
/// <param name="newText">The new text.</param>
/// <param name="ignoreWhitespace">if set to <c>true</c> will ignore white space when determining if words are the same.</param>
/// <param name="separators">The list of characters which define word separators.</param>
/// <returns>A DiffResult object which details the differences</returns>
DiffResult CreateWordDiffs(string oldText, string newText, bool ignoreWhitespace, char[] separators);
/// <summary>
/// Create a diff by comparing text in chunks determined by the supplied chunker function.
/// </summary>
/// <param name="oldText">The old text.</param>
/// <param name="newText">The new text.</param>
/// <param name="ignoreWhiteSpace">if set to <c>true</c> will ignore white space when determining if chunks are the same.</param>
/// <param name="chunker">A function that will break the text into chunks.</param>
/// <returns>A DiffResult object which details the differences</returns>
DiffResult CreateCustomDiffs(string oldText, string newText, bool ignoreWhiteSpace, Func<string, string[]> chunker);
/// <summary>
/// Create a diff by comparing text line by line
/// </summary>
/// <param name="oldText">The old text.</param>
/// <param name="newText">The new text.</param>
/// <param name="ignoreWhiteSpace">if set to <c>true</c> will ignore white space when determining if lines are the same.</param>
/// <param name="ignoreCase">Determine if the text comparision is case sensitive or not</param>
/// <param name="chunker">Component responsible for tokenizing the compared texts</param>
/// <returns>A DiffResult object which details the differences</returns>
DiffResult CreateDiffs(string oldText, string newText, bool ignoreWhiteSpace, bool ignoreCase, IChunker chunker);
}
IChunker Interface
public interface IChunker
{
/// <summary>
/// Dive text into sub-parts
/// </summary>
string[] Chunk(string text);
}
Currently provided implementations:
CharacterChunker
CustomFunctionChunker
DelimiterChunker
LineChunker
LineEndingsPreservingChunker
WordChunker
ISideBySideDifferBuilder Interface
/// <summary>
/// Provides methods that generate differences between texts for displaying in a side by side view.
/// </summary>
public interface ISideBySideDiffBuilder
{
/// <summary>
/// Builds a diff model for displaying diffs in a side by side view
/// </summary>
/// <param name="oldText">The old text.</param>
/// <param name="newText">The new text.</param>
/// <returns>The side by side diff model</returns>
SideBySideDiffModel BuildDiffModel(string oldText, string newText);
}
Sample Website
DiffPlex also contains a sample website that shows how to create a basic side by side diff in an ASP MVC website.
Windows app
There are 2 libraries for Windows app development. One is for Windows App SDK, another is for WPF and WinForms.
WinUI 3 Elements
DiffPlex WinUI library DiffPlex.Windows
is used to render textual diffs in your app which targets to Windows App SDK.
using DiffPlex.UI;
And insert following code into the root node of your xaml file, e.g. user control, page or window.
xmlns:diffplex="using:DiffPlex.UI"
DiffTextView
Textual diffs view element.
For example.
<diffplex:DiffTextView x:Name="DiffView" />
DiffView.SetText(OldText, NewText);
You can also customize the style. Following are some of the properties you can get or set.
// true if it is in split view; otherwise, false, in unified view.
public bool IsSplitView { get; set; }
// true if it is in unified view; otherwise, false, in split view.
public bool IsUnifiedView { get; set; }
// The selection mode of list view. Default is None.
public ListViewSelectionMode SelectionMode { get; set; }
// true if ignore white spaces; otherwise, false. Default is true.
public bool IgnoreWhiteSpace { get; set; }
// true if the text is case sensitive; otherwise, false. Default is false.
public bool IsCaseSensitive { get; set; }
// The default text color (foreground brush).
public Brush Foreground { get; set; }
// The background.
public Brush Background { get; set; }
// The width of the line number. Default is 50.
public GridLength LineNumberWidth { get; set; }
// The style of the line number.
public Style LineNumberStyle { get; set; }
// The width of the change type symbol. Default is 20.
public GridLength ChangeTypeWidth { get; set; }
// The style of the change type symbol.
public Style ChangeTypeStyle { get; set; }
// The style of the text.
public Style TextStyle { get; set; }
// true if the text is selection enabled; otherwise, false. Default is true.
public bool IsTextSelectionEnabled { get; set; }
// true if collapse unchanged sections; otherwise, false. Default is false.
public bool IsUnchangedSectionCollapsed { get; set; }
// The lines for context. Default is 2.
public int LineCountForContext { get; set; }
// true if the file selector menu button is enabled; otherwise, false. Default is true.
public bool IsFileMenuEnabled { get; set; }
// The height of command bar. Default is 50.
public GridLength CommandBarHeight { get; set; }
// The default label position of command bar. Default is Right.
public CommandBarDefaultLabelPosition CommandLabelPosition { get; set; }
// The collection of secondary command elements for the command bar.
public IObservableVector<ICommandBarElement> SecondaryCommands { get; }
WPF Controls
DiffPlex WPF control library DiffPlex.Wpf
is used to render textual diffs in your WPF application.
It targets .NET 6
, .NET Framework 4.8
and .NET Framework 4.6
.
using DiffPlex.Wpf.Controls;
To import the controls into your window/page/control, please insert following attribute into the root node (such as <Window />
) of your xaml files.
xmlns:diffplex="clr-namespace:DiffPlex.Wpf.Controls;assembly=DiffPlex.Wpf"
DiffViewer
Textual diffs viewer control with view mode switching by setting an old text and a new text to diff.SideBySideDiffViewer
Side-by-side (splitted) textual diffs viewer control by setting a diff modelSideBySideDiffModel
.InlineDiffViewer
Inline textual diffs viewer control by setting a diff modelDiffPaneModel
.
For example.
<diffplex:DiffViewer x:Name="DiffView" />
DiffView.OldText = oldText;
DiffView.NewText = newText;
You can also customize the style. Following are some of the properties you can get or set.
// The header of old text.
public string OldTextHeader { get; set; }
// The header of new text.
public string NewTextHeader { get; set; }
// true if it is in side-by-side (split) view;
// otherwise, false, in inline (unified) view.
public bool IsSideBySideViewMode { get; }
// true if collapse unchanged sections; otherwise, false.
public bool IgnoreUnchanged { get; set; }
// The font size.
public double FontSize { get; set; }
// The preferred font family.
public FontFamily FontFamily { get; set; }
// The font weight.
public FontWeight FontWeight { get; set; }
// The font style.
public FontStyle FontStyle { get; set; }
// The font-stretching characteristics.
public FontStretch FontStretch { get; set; }
// The default text color (foreground brush).
public Brush Foreground { get; set; }
// The background brush of the line inserted.
public Brush InsertedBackground { get; set; }
// The background brush of the line deleted.
public Brush DeletedBackground { get; set; }
// The text color (foreground brush) of the line number.
public Brush LineNumberForeground { get; set; }
// The width of the line number and change type symbol.
public int LineNumberWidth { get; set; }
// The background brush of the line imaginary.
public Brush ImaginaryBackground { get; set; }
// The text color (foreground brush) of the change type symbol.
public Brush ChangeTypeForeground { get; set; }
// The background brush of the header.
public Brush HeaderBackground { get; set; }
// The height of the header.
public double HeaderHeight { get; set; }
// The background brush of the grid splitter.
public Brush SplitterBackground { get; set; }
// The width of the grid splitter.
public Thickness SplitterWidth { get; set; }
// A value that represents the actual calculated width of the left side panel.
public double LeftSideActualWidth { get; }
// A value that represents the actual calculated width of the right side panel.
public double RightSideActualWidth { get; }
And you can listen following event handlers.
// Occurs when the grid splitter loses mouse capture.
public event DragCompletedEventHandler SplitterDragCompleted;
// Occurs one or more times as the mouse changes position when the grid splitter has logical focus and mouse capture.
public event DragDeltaEventHandler SplitterDragDelta;
// Occurs when the grid splitter receives logical focus and mouse capture.
public event DragStartedEventHandler SplitterDragStarted;
// Occurs when the view mode is changed.
public event EventHandler<ViewModeChangedEventArgs> ViewModeChanged;
WinForms Controls
Windows Forms control of diff viewer is a WPF element host control.
It is also included in DiffPlex.Wpf
assembly.
You can import it to use in your Windows Forms application.
It targets .NET 6
, .NET Framework 4.8
and .NET Framework 4.6
.
using DiffPlex.WindowsForms.Controls;
Then you can add the following control in window or user control.
DiffViewer
Textual diffs viewer control with view mode switching by setting an old text and a new text to diff.
For example.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var diffView = new DiffViewer
{
Margin = new Padding(0),
Dock = DockStyle.Fill,
OldText = oldText,
NewText = newText
};
Controls.Add(diffView);
}
}
You can also customize the style. Following are some of the properties you can get or set.
// The header of old text.
public string OldTextHeader { get; set; }
// The header of new text.
public string NewTextHeader { get; set; }
// true if it is in side-by-side (split) view;
// otherwise, false, in inline (unified) view.
public bool IsSideBySideViewMode { get; }
// true if collapse unchanged sections; otherwise, false.
public bool IgnoreUnchanged { get; set; }
// The font size.
public double FontSize { get; set; }
// The preferred font family names in string.
public string FontFamilyNames { get; set; }
// The font weight.
public int FontWeight { get; set; }
// The font style.
public bool IsFontItalic { get; set; }
// The default text color (foreground brush).
public Color ForeColor { get; set; }
// The background brush of the line inserted.
public Color InsertedBackColor { get; set; }
// The background brush of the line deleted.
public Color DeletedBackColor { get; set; }
// The text color (foreground color) of the line number.
public Color LineNumberForeColor { get; set; }
// The width of the line number and change type symbol.
public int LineNumberWidth { get; set; }
// The background brush of the line imaginary.
public Color ImaginaryBackColor { get; set; }
// The text color (foreground color) of the change type symbol.
public Color ChangeTypeForeColor { get; set; }
// The background brush of the header.
public Color HeaderBackColor { get; set; }
// The height of the header.
public double HeaderHeight { get; set; }
// The background brush of the grid splitter.
public Color SplitterBackColor { get; set; }
// The width of the grid splitter.
public Padding SplitterWidth { get; set; }
// A value that represents the actual calculated width of the left side panel.
public double LeftSideActualWidth { get; }
// A value that represents the actual calculated width of the right side panel.
public double RightSideActualWidth { get; }
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.0 is compatible. netstandard1.1 was computed. netstandard1.2 was computed. netstandard1.3 was computed. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 was computed. netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net35 is compatible. net40 is compatible. net403 was computed. net45 was computed. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Universal Windows Platform | uap was computed. uap10.0 was computed. |
Windows Phone | wp8 was computed. wp81 was computed. wpa81 was computed. |
Windows Store | netcore was computed. netcore45 was computed. netcore451 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 3.5
- No dependencies.
-
.NETFramework 4.0
- No dependencies.
-
.NETStandard 1.0
- NETStandard.Library (>= 1.6.1)
-
.NETStandard 2.0
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages (44)
Showing the top 5 NuGet packages that depend on DiffPlex:
Package | Downloads |
---|---|
Microsoft.CodeAnalysis.Analyzer.Testing
Roslyn Analyzer Test Framework Common Types. |
|
Verify.DiffPlex
Extends Verify (https://github.com/VerifyTests/Verify) to allow comparison of text via DiffPlex (https://github.com/mmanela/diffplex). |
|
Microsoft.CodeAnalysis.SourceGenerators.Testing
Roslyn Source Generator Test Framework Common Types. |
|
ppy.osu.Game
Package Description |
|
CookieCrumble
Package Description |
GitHub repositories (45)
Showing the top 5 popular GitHub repositories that depend on DiffPlex:
Repository | Stars |
---|---|
dotnet/roslyn
The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
|
|
ppy/osu
rhythm is just a *click* away!
|
|
dotnet/runtime
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
|
|
0x7c13/Notepads
A modern, lightweight text editor with a minimalist design.
|
|
dotnet/reactive
The Reactive Extensions for .NET
|
Version | Downloads | Last updated |
---|---|---|
1.7.2 | 1,765,574 | 12/11/2023 |
1.7.1 | 3,238,549 | 3/5/2022 |
1.7.0 | 1,676,179 | 3/10/2021 |
1.6.3 | 1,904,599 | 5/30/2020 |
1.6.2 | 12,119 | 5/22/2020 |
1.6.1 | 347,457 | 4/1/2020 |
1.6.0 | 2,602 | 3/31/2020 |
1.5.0 | 3,504,235 | 11/26/2019 |
1.4.4 | 1,870,007 | 3/21/2019 |
1.4.3 | 47,853 | 1/24/2019 |
1.4.2 | 179,871 | 9/21/2018 |
1.4.1 | 568,731 | 3/31/2017 |
1.4.0 | 2,082 | 3/20/2017 |
1.3.0 | 4,990 | 3/14/2017 |
1.2.1 | 548,477 | 9/14/2014 |
1.2.0 | 54,329 | 5/23/2011 |
1.1.0 | 8,339 | 3/10/2011 |
Fixed diffing of sub-components (like words). Ensures ignoreWhitespace and ignoreCase are honoroed in that case and that the parent reflects modification state of the child.