mmanela / diffplex

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

ignoreWhitespace: true apparently ignores only leading whitespace #62

Closed daveyostcom closed 3 years ago

daveyostcom commented 4 years ago
using System;
using DiffPlex;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;
using System.Text;

// ignoreWhitespace: true
// apparently ignores only leading whitespace

// Cribbed from https://stackoverflow.com/a/23302056/1390116

namespace DiffPlexTest.Controllers {

  class Program {

    static void Main(string[] args) {
      StringBuilder sb = new StringBuilder();

      string oldText = "123\nabc def\nabc def g\n";
      string newText = " 123\nabc  def\nabc def  g\n";

      var d = new Differ();
      var builder = new InlineDiffBuilder(d);
      var result = builder.BuildDiffModel(oldText, newText, ignoreWhitespace: true);

      foreach (var line in result.Lines) {
        if (line.Type == ChangeType.Inserted) {
          sb.Append("+ ");
        } else if (line.Type == ChangeType.Deleted) {
          sb.Append("- ");
        } else if (line.Type == ChangeType.Modified) {
          sb.Append("* ");
        } else if (line.Type == ChangeType.Imaginary) {
          sb.Append("? ");
        } else if (line.Type == ChangeType.Unchanged) {
          sb.Append("  ");
        }
        sb.Append(line.Text + "\n");
      }
      Console.WriteLine(sb);
    }

  }

}

// expected:
// –––––––––
// 123
// abc def
// abc def g
//
// actual:
// –––––––
//    123
// - abc def
// - abc def g
// + abc  def
// + abc def  g
mmanela commented 4 years ago

Yea, if you do word diffing it works as you expect but when doing line diffing it only does beginning and end.

What is your expected behavior @daveyostcom ?

jimfoye commented 3 years ago

@mmanela So, how do we "do word diffing" to get the diff to ignore all whitespace? I'm using the SideBySideDiffBuilder static helper methods, but always the result is the same, as OP reported, that only whitespace at the start and end of lines is being ignored. Thanks.

mmanela commented 3 years ago

You found a bug for sure, just pushed a commit to fix it