Giter Site home page Giter Site logo

nickkfwong / diffplex Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mmanela/diffplex

0.0 0.0 0.0 6.67 MB

DiffPlex is Netstandard 1.0 C# library to generate textual diffs.

License: Apache License 2.0

C# 93.34% CSS 0.97% JavaScript 1.65% Ruby 0.53% PowerShell 0.24% HTML 3.26%

diffplex's Introduction

DiffPlex Build Status DiffPlex NuGet version

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 the Differ class) - This is the core diffing class. It exposes the low level functions to generate differences between texts.
  • ISidebySideDiffer (implemented by the SideBySideDiffer class) - This is a higher level interface. It consumes the IDiffer interface and generates a SideBySideDiffModel. 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 the DiffPlex Project.
  • UnidiffFormater.cs contained in the DiffPlex.ConsoleRunner project.

For use of the ISidebySideDiffer interface see:

  • DiffController.cs and associated MVC views in the WebDiffer project
  • TextBoxDiffRenderer.cs in the SilverlightDiffer project

Sample code

var diffBuilder = new InlineDiffBuilder(new Differ());
var diff = diffBuilder.BuildDiffModel(before, after);

foreach (var line in diff.Lines)
{
    switch (line.Type)
    {
        case ChangeType.Inserted:
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("+ ");
            break;
        case ChangeType.Deleted:
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("- ");
            break;
        default:
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("  ");
            break;
    }

    Console.WriteLine(line.Text);
}

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

ISidebySideDiffer 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.

Web page sample

WPF Controls

DiffPlex WPF controls library DiffPlex.Wpf is used to render textual diffs in your WPF application.

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"

Then you can add one of following controls in UI.

  • SideBySideDiffViewer Side-by-side (splitted) textual diffs viewer control.
  • InlineDiffViewer Inline textual diffs viewer control.

For example,

<diffplex:SideBySideDiffViewer x:Name="DiffView" />

And set the property DiffModel or call SetDiffModel member method to bind the result to show.

DiffView.SetDiffModel(OldText, NewText);

WPF sample

You can also customize the style of these controls. Following are some of the properties you can get or 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 text color (foreground brush) of the change type symbol.
public Brush ChangeTypeForeground { get; set; }

For SideBySideDiffViewer, we also provide following properties and 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;

// The background brush of the line imaginary.
public Brush ImaginaryBackground { get; set; }

// The background brush of the grid splitter.
public Brush SplitterBackground { get; set; }

// The width of the grid splitter.
public Brush 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; }

diffplex's People

Contributors

aarnott avatar azure-pipelines[bot] avatar cezarypiatek avatar drewnoakes avatar gantrior avatar igitur avatar kingcean avatar mmanela avatar rjantz2 avatar sharwell avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.