Skip to content

Commit f41e66b

Browse files
committed
AST Comparison : diff on Method Declarations
- Implemented very simple SyntaxWalker to collect Method Declaration
1 parent dac4019 commit f41e66b

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace SemanticDiff.Core.AST
2+
3+
module Comparison =
4+
5+
open Roslyn.Compilers
6+
open Roslyn.Compilers.CSharp
7+
open Roslyn.Compilers.Common
8+
open Roslyn.Services
9+
open Roslyn.Services.CSharp
10+
11+
type MethodCollector () =
12+
inherit SyntaxWalker()
13+
let mutable i = 0
14+
let mutable visitedMethods = []
15+
16+
member this.VisitedMethods
17+
with get() = visitedMethods
18+
19+
override this.VisitMethodDeclaration node =
20+
i <- i + 1
21+
visitedMethods <- node :: visitedMethods
22+
// good-old printf debugging :-)
23+
printfn "Visiting %s : %dth method" <| node.Identifier.ValueText <| i
24+
25+
let internal collectMethodsInternal (collector:MethodCollector) (node:SyntaxNode) =
26+
collector.Visit(node)
27+
collector.VisitedMethods
28+
29+
let collectMethods node =
30+
let collector = new MethodCollector()
31+
collectMethodsInternal collector node

SemanticDiff.Core/SemanticDiff.Core.fsproj

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
<WarningLevel>3</WarningLevel>
3232
<DocumentationFile>bin\Release\SemanticDiff.Core.XML</DocumentationFile>
3333
</PropertyGroup>
34+
<PropertyGroup>
35+
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
36+
</PropertyGroup>
37+
<Import Project="$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets" Condition=" Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')" />
38+
<ItemGroup>
39+
<None Include="packages.config" />
40+
<Compile Include="AST.Comparison.fs" />
41+
</ItemGroup>
3442
<ItemGroup>
3543
<Reference Include="mscorlib" />
3644
<Reference Include="FSharp.Core, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
@@ -61,13 +69,6 @@
6169
<Reference Include="System.Core" />
6270
<Reference Include="System.Numerics" />
6371
</ItemGroup>
64-
<ItemGroup>
65-
<None Include="packages.config" />
66-
</ItemGroup>
67-
<PropertyGroup>
68-
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
69-
</PropertyGroup>
70-
<Import Project="$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets" Condition=" Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')" />
7172
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
7273
Other similar extension points exist, see Microsoft.Common.targets.
7374
<Target Name="BeforeBuild">

SemanticDiff.Tests/Experiments.fs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ open Roslyn.Compilers.Common
99
open Roslyn.Services
1010
open Roslyn.Services.CSharp
1111

12+
open SemanticDiff.Core.AST.Comparison
13+
1214
let sampleCode = "void foo()\
1315
{
1416
int i = 1 + 1;
@@ -21,6 +23,19 @@ let sampleCode2 = "void foo()\
2123
// ... and whitespace changes
2224
}"
2325

26+
let codeWithMethods = "void foo()
27+
{
28+
return;
29+
}
30+
int bar()
31+
{
32+
return 1;
33+
}
34+
int foobar()
35+
{
36+
return bar() + bar ();
37+
}"
38+
2439
[<TestClass>]
2540
type RoslynBasicExperiments() =
2641

@@ -36,3 +51,12 @@ type RoslynBasicExperiments() =
3651

3752
(* The "IsEquivalentTo" disregards SyntaxTrivia *)
3853
Assert.IsTrue(ast1.IsEquivalentTo ast2)
54+
55+
[<TestMethod>]
56+
member this.FindMethodDeclarationInAST() =
57+
let ast = SyntaxTree.ParseText codeWithMethods
58+
let visitor = new MethodCollector()
59+
60+
visitor.Visit(ast.GetRoot())
61+
62+
Assert.AreEqual(3, List.length visitor.VisitedMethods)

SemanticDiff.Tests/SemanticDiff.Tests.fsproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@
6868
<None Include="packages.config" />
6969
<Compile Include="Experiments.fs" />
7070
</ItemGroup>
71+
<ItemGroup>
72+
<ProjectReference Include="..\SemanticDiff.Core\SemanticDiff.Core.fsproj">
73+
<Name>SemanticDiff.Core</Name>
74+
<Project>{9c582785-e24e-441c-b2f7-2c2a979f6a3e}</Project>
75+
<Private>True</Private>
76+
</ProjectReference>
77+
</ItemGroup>
7178
<PropertyGroup>
7279
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
7380
</PropertyGroup>

0 commit comments

Comments
 (0)