Note that there are some explanatory texts on larger screens.

plurals
  1. POCode diff using Roslyn CTP API
    primarykey
    data
    text
    <p>I'm trying to do some basic code diff with the Roslyn API, and I'm running into some unexpected problems. Essentially, I have two pieces of code that are the same, except one line has been added. This should just return the line of the changed text, but for some reason, it's telling me that everything has changed. I have also tried just editing one line instead of adding a line, but I get the same result. I would like to be able to apply this to two versions of a source file to identify differences between the two. Here's the code I'm currently using:</p> <pre><code> SyntaxTree tree = SyntaxTree.ParseCompilationUnit( @"using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine(""Hello, World!""); } } }"); var root = (CompilationUnitSyntax)tree.Root; var compilation = Compilation.Create("HelloWorld") .AddReferences( new AssemblyFileReference( typeof(object).Assembly.Location)) .AddSyntaxTrees(tree); var model = compilation.GetSemanticModel(tree); var nameInfo = model.GetSemanticInfo(root.Usings[0].Name); var systemSymbol = (NamespaceSymbol)nameInfo.Symbol; SyntaxTree tree2 = SyntaxTree.ParseCompilationUnit( @"using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine(""Hello, World!""); Console.WriteLine(""jjfjjf""); } } }"); var root2 = (CompilationUnitSyntax)tree2.Root; var compilation2 = Compilation.Create("HelloWorld") .AddReferences( new AssemblyFileReference( typeof(object).Assembly.Location)) .AddSyntaxTrees(tree2); var model2 = compilation2.GetSemanticModel(tree2); var nameInfo2 = model2.GetSemanticInfo(root2.Usings[0].Name); var systemSymbol2 = (NamespaceSymbol)nameInfo2.Symbol; foreach (TextSpan t in tree2.GetChangedSpans(tree)) { Console.WriteLine(tree2.Text.GetText(t)); } </code></pre> <p>And here's the output I'm getting:</p> <pre><code>System using System Collections Generic using System Linq using System Text namespace HelloWorld { class Program { static Main args { Console WriteLine "Hello, World!" Console.WriteLine("jjfjjf"); } } } Press any key to continue . . . </code></pre> <p>Interestingly, it seems to show each line as tokens for every line except for the added line, where it displays the line without breaking it up. Does anyone know how to isolate the actual changes?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload