本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxTree.GetChanges方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxTree.GetChanges方法的具体用法?C# SyntaxTree.GetChanges怎么用?C# SyntaxTree.GetChanges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxTree
的用法示例。
在下文中一共展示了SyntaxTree.GetChanges方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compare
/// <summary>
/// Generates Diff results for each change that occurred from the ancestor to the changed
/// </summary>
/// <param name="ancestor">
/// The abstract syntax tree that the changed version originated from
/// </param>
/// <param name="changed">The abstract syntax tree that resulted from changing the ancestor</param>
/// <returns>Changes between the ancestor and changed</returns>
public static IEnumerable<Diff> Compare(SyntaxTree ancestor, SyntaxTree changed)
{
var offset = 0; //Tracks differences in indexes moving through the changed syntax tree
foreach (var change in changed.GetChanges(ancestor))
{ //Assumes that this list is sorted by place in file
var origLength = change.Span.Length;
var offsetChange = (change.NewText.Length - origLength);
var chanStart = change.Span.Start + offset;
var chanLength = origLength + offsetChange;
offset += offsetChange;
var d = new Diff
{
Ancestor = SpanDetails.Create(change.Span, ancestor),
Changed = SpanDetails.Create(new TextSpan(chanStart, chanLength), changed)
};
yield return d;
}
}
示例2: TryGetChangesIgnoringWhiteSpace
private static bool TryGetChangesIgnoringWhiteSpace(SyntaxTree oldTree, SyntaxTree newTree, out IEnumerable<string> textchanges)
{
#region CodeContracts
Contract.Requires(oldTree != null);
Contract.Requires(newTree != null);
Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn<IEnumerable<string>>(out textchanges) != null);
#endregion CodeContracts
var workspace = MSBuildWorkspace.Create();
oldTree = Formatter.Format(oldTree.GetRoot(), workspace).SyntaxTree;
newTree = Formatter.Format(newTree.GetRoot(), workspace).SyntaxTree;
textchanges = null;
if (oldTree == newTree)
{
return false;
}
var changes = newTree.GetChanges(oldTree);
if (!changes.Any()) {
return false;
}
textchanges = changes.Select(x => x.NewText.Trim());
//textchanges = changes.Select(x => x.NewText.Trim());
if (textchanges.All(x => x == ""))
{
return false;
}
return true;
}