Browse Source

Added DescendantNodes API.

pull/45/merge
Mike Krüger 12 years ago
parent
commit
5dec9ee365
  1. 63
      ICSharpCode.NRefactory.CSharp/Ast/AstNode.cs
  2. 77
      ICSharpCode.NRefactory.Tests/CSharp/AstTests.cs
  3. 1
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj
  4. 12
      ICSharpCode.NRefactory/TypeSystem/DomRegion.cs

63
ICSharpCode.NRefactory.CSharp/Ast/AstNode.cs

@ -296,31 +296,64 @@ namespace ICSharpCode.NRefactory.CSharp @@ -296,31 +296,64 @@ namespace ICSharpCode.NRefactory.CSharp
/// Gets all descendants of this node (excluding this node itself).
/// </summary>
public IEnumerable<AstNode> Descendants {
get { return GetDescendants(false); }
get { return GetDescendantsImpl(false); }
}
/// <summary>
/// Gets all descendants of this node (including this node itself).
/// </summary>
public IEnumerable<AstNode> DescendantsAndSelf {
get { return GetDescendants(true); }
get { return GetDescendantsImpl(true); }
}
IEnumerable<AstNode> GetDescendants(bool includeSelf)
static bool IsInsideRegion(DomRegion region, AstNode pos)
{
if (includeSelf)
if (region.IsEmpty)
return true;
var nodeRegion = pos.Region;
return region.IntersectsWith(nodeRegion) || region.OverlapsWith(nodeRegion);
}
public IEnumerable<AstNode> DescendantNodes (Func<AstNode, bool> descendIntoChildren = null)
{
return GetDescendantsImpl(false, new DomRegion (), descendIntoChildren);
}
public IEnumerable<AstNode> DescendantNodes (DomRegion region, Func<AstNode, bool> descendIntoChildren = null)
{
return GetDescendantsImpl(false, region, descendIntoChildren);
}
public IEnumerable<AstNode> DescendantNodesAndSelf (Func<AstNode, bool> descendIntoChildren = null)
{
return GetDescendantsImpl(true, new DomRegion (), descendIntoChildren);
}
public IEnumerable<AstNode> DescendantNodesAndSelf (DomRegion region, Func<AstNode, bool> descendIntoChildren = null)
{
return GetDescendantsImpl(true, region, descendIntoChildren);
}
IEnumerable<AstNode> GetDescendantsImpl(bool includeSelf, DomRegion region = new DomRegion (), Func<AstNode, bool> descendIntoChildren = null)
{
if (includeSelf) {
if (IsInsideRegion (region, this))
yield return this;
Stack<AstNode> nextStack = new Stack<AstNode>();
nextStack.Push(null);
AstNode pos = firstChild;
while (pos != null) {
if (pos.nextSibling != null)
nextStack.Push(pos.nextSibling);
yield return pos;
if (pos.firstChild != null)
pos = pos.firstChild;
else
pos = nextStack.Pop();
if (descendIntoChildren != null && !descendIntoChildren(this))
yield break;
}
Stack<AstNode> descendStack = new Stack<AstNode>();
descendStack.Push(firstChild);
while (descendStack.Count > 0) {
AstNode cur = descendStack.Pop ();
while (cur != null) {
if (IsInsideRegion(region, cur))
yield return cur;
if (descendIntoChildren == null || descendIntoChildren(cur))
descendStack.Push(cur.firstChild);
cur = cur.nextSibling;
}
}
}

77
ICSharpCode.NRefactory.Tests/CSharp/AstTests.cs

@ -0,0 +1,77 @@ @@ -0,0 +1,77 @@
//
// AstTests.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using NUnit.Framework;
using System.Linq;
namespace ICSharpCode.NRefactory.CSharp
{
[TestFixture]
public class AstTests
{
[Test]
public void TestDescendants ()
{
var tree = SyntaxTree.Parse(@"class Test
{
void Foo()
{
Call1();
{
Call2();
}
Call3();
}
}");
var method = tree.GetNodeAt<MethodDeclaration>(6, 1);
// Body, Call1, Block, Call2 and Call 3
Assert.AreEqual(5, method.DescendantNodes().Count(n => n is Statement));
}
[Test]
public void TestDescendantsWithPredicate ()
{
var tree = SyntaxTree.Parse(@"class Test
{
void Foo()
{
Call1();
{
Call2();
}
Call3();
}
}");
var method = tree.GetNodeAt<MethodDeclaration>(6, 1);
// Body, Call1, Block and Call 3 - NOT call2
var childs = method.DescendantNodes(child => !(child is BlockStatement) || (((BlockStatement)child).Parent is MethodDeclaration)).Where(n => n is Statement).ToList();
Assert.AreEqual(4, childs.Count);
}
}
}

1
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -415,6 +415,7 @@ @@ -415,6 +415,7 @@
<Compile Include="CSharp\CodeIssues\DuplicateExpressionsInConditionsIssueTests.cs" />
<Compile Include="CSharp\CodeIssues\DuplicateIfInIfChainIssueTests.cs" />
<Compile Include="CSharp\CodeIssues\UseBlockInsteadColonIssueTests.cs" />
<Compile Include="CSharp\AstTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\cecil\Mono.Cecil.csproj">

12
ICSharpCode.NRefactory/TypeSystem/DomRegion.cs

@ -157,6 +157,18 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -157,6 +157,18 @@ namespace ICSharpCode.NRefactory.TypeSystem
return IsInside(location.Line, location.Column);
}
public bool IntersectsWith (DomRegion region)
{
return region.Begin <= End && region.End >= Begin;
}
public bool OverlapsWith (DomRegion region)
{
var maxBegin = Begin > region.Begin ? Begin : region.Begin;
var minEnd = End < region.End ? End : region.End;
return maxBegin < minEnd;
}
public override string ToString()
{
return string.Format(

Loading…
Cancel
Save