Browse Source

Added DescendantNodes API.

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

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

@ -296,31 +296,64 @@ namespace ICSharpCode.NRefactory.CSharp
/// Gets all descendants of this node (excluding this node itself). /// Gets all descendants of this node (excluding this node itself).
/// </summary> /// </summary>
public IEnumerable<AstNode> Descendants { public IEnumerable<AstNode> Descendants {
get { return GetDescendants(false); } get { return GetDescendantsImpl(false); }
} }
/// <summary> /// <summary>
/// Gets all descendants of this node (including this node itself). /// Gets all descendants of this node (including this node itself).
/// </summary> /// </summary>
public IEnumerable<AstNode> DescendantsAndSelf { 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)
yield return this; return true;
Stack<AstNode> nextStack = new Stack<AstNode>(); var nodeRegion = pos.Region;
nextStack.Push(null); return region.IntersectsWith(nodeRegion) || region.OverlapsWith(nodeRegion);
AstNode pos = firstChild; }
while (pos != null) {
if (pos.nextSibling != null) public IEnumerable<AstNode> DescendantNodes (Func<AstNode, bool> descendIntoChildren = null)
nextStack.Push(pos.nextSibling); {
yield return pos; return GetDescendantsImpl(false, new DomRegion (), descendIntoChildren);
if (pos.firstChild != null) }
pos = pos.firstChild;
else public IEnumerable<AstNode> DescendantNodes (DomRegion region, Func<AstNode, bool> descendIntoChildren = null)
pos = nextStack.Pop(); {
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;
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 @@
//
// 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 @@
<Compile Include="CSharp\CodeIssues\DuplicateExpressionsInConditionsIssueTests.cs" /> <Compile Include="CSharp\CodeIssues\DuplicateExpressionsInConditionsIssueTests.cs" />
<Compile Include="CSharp\CodeIssues\DuplicateIfInIfChainIssueTests.cs" /> <Compile Include="CSharp\CodeIssues\DuplicateIfInIfChainIssueTests.cs" />
<Compile Include="CSharp\CodeIssues\UseBlockInsteadColonIssueTests.cs" /> <Compile Include="CSharp\CodeIssues\UseBlockInsteadColonIssueTests.cs" />
<Compile Include="CSharp\AstTests.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\cecil\Mono.Cecil.csproj"> <ProjectReference Include="..\..\cecil\Mono.Cecil.csproj">

14
ICSharpCode.NRefactory/TypeSystem/DomRegion.cs

@ -156,7 +156,19 @@ namespace ICSharpCode.NRefactory.TypeSystem
{ {
return IsInside(location.Line, location.Column); 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() public override string ToString()
{ {
return string.Format( return string.Format(

Loading…
Cancel
Save