Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Experimenting with other diff format #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 59 additions & 3 deletions KustoSchemaTools/Changes/ScriptCompareChange.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using Kusto.Language;
using DiffPlex;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;
using Kusto.Language;
using KustoSchemaTools.Model;
using KustoSchemaTools.Parser;
using System.Text;
Expand Down Expand Up @@ -47,18 +50,37 @@ private void Init()
sb.AppendLine($"<td colspan=\"1\"><b>{logo}<b></td>");
sb.AppendLine($"<td colspan =\"11\"><b>{change.Kind}<b></td>");
sb.AppendLine($"</tr>");


var beforeText = before?.Text.PrettifyKql() ?? "";
var afterText = change.Text.PrettifyKql();
var differ = new Differ();
var diff = InlineDiffBuilder.Diff(beforeText, afterText, false);

if (before != null)
{
sb.AppendLine("<tr>");
sb.AppendLine($" <td colspan=\"2\">From:</td>");
sb.AppendLine($" <td colspan=\"10\"><pre lang=\"kql\">{before.Text.PrettifyKql()}</pre></td>");
sb.AppendLine($" <td colspan=\"10\">");

sb.AppendLine(FormatChangeDiff(diff.Lines.Where(itm => itm.Type == ChangeType.Deleted || itm.Type == ChangeType.Unchanged)));

sb.AppendLine();
sb.AppendLine("</td>");
sb.AppendLine("</tr>");
}

sb.AppendLine("<tr>");
sb.AppendLine($" <td colspan=\"2\">{addActionText}:</td>");
sb.AppendLine($" <td colspan=\"10\"><pre lang=\"kql\">{change.Text.PrettifyKql()}</pre></td>");
sb.AppendLine($" <td colspan=\"10\">");

sb.AppendLine(FormatChangeDiff(diff.Lines.Where(itm => itm.Type == ChangeType.Inserted || itm.Type == ChangeType.Unchanged)));

sb.AppendLine();
sb.AppendLine("</td>");
sb.AppendLine("</tr>");


if (change.IsValid == false)
{
foreach (var diagnostic in diagnostics)
Expand All @@ -73,6 +95,40 @@ private void Init()
sb.AppendLine("</table>");
Markdown = sb.ToString();
}


public string FormatChangeDiff(IEnumerable<DiffPiece> diffs)
{
var allDiffs = diffs.ToList();

if (allDiffs.Count <= 1)
{
return allDiffs.Any() ? allDiffs[0].Text : "";
}

var sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("```diff");
foreach (var diff in allDiffs)
{
switch (diff.Type)
{
case ChangeType.Deleted:
sb.AppendLine($"- {diff.Text}");
break;
case ChangeType.Inserted:
sb.AppendLine($"+ {diff.Text}");
break;
case ChangeType.Unchanged:
sb.AppendLine($"{diff.Text}");
break;
default: throw new NotSupportedException();
}
}
sb.AppendLine("```");
sb.AppendLine();
return sb.ToString();
}
}

}
1 change: 1 addition & 0 deletions KustoSchemaTools/KustoSchemaTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DiffPlex" Version="1.7.1" />
<PackageReference Include="Kusto.Toolkit" Version="1.5.0" />
<PackageReference Include="Markdig" Version="0.31.0" />
<PackageReference Include="Microsoft.Azure.Kusto.Data" Version="11.3.2" />
Expand Down