Browse Source

NRefactory: Set parent of child when it is added to collection

pull/1/head^2
David Srbecký 18 years ago
parent
commit
07a2cb2a84
  1. 1
      lib/NRefactory/Project/NRefactory.csproj
  2. 16
      lib/NRefactory/Project/Src/Ast/AbstractNode.cs
  3. 99
      lib/NRefactory/Project/Src/Ast/NodeCollection.cs

1
lib/NRefactory/Project/NRefactory.csproj

@ -54,6 +54,7 @@ @@ -54,6 +54,7 @@
<None Include="Resources\ICSharpCode.NRefactory.snk" />
<None Include="Src\Lexer\BuildKeywords.pl" />
<Compile Include="Configuration\AssemblyInfo.cs" />
<Compile Include="Src\Ast\NodeCollection.cs" />
<Compile Include="Src\Lexer\AbstractLexer.cs" />
<Compile Include="Src\Lexer\CSharp\Keywords.cs" />
<Compile Include="Src\Lexer\CSharp\Lexer.cs" />

16
lib/NRefactory/Project/Src/Ast/AbstractNode.cs

@ -15,14 +15,20 @@ namespace ICSharpCode.NRefactory.Ast @@ -15,14 +15,20 @@ namespace ICSharpCode.NRefactory.Ast
{
public abstract class AbstractNode : INode
{
IList<INode> children = new List<INode>();
NodeCollection children;
public INode Parent { get; set; }
public Location StartLocation { get; set; }
public Location EndLocation { get; set; }
public object UserData { get; set; }
public IList<INode> Children {
IList<INode> INode.Children {
get {
return children;
}
}
public NodeCollection Children {
get {
return children;
}
@ -32,6 +38,12 @@ namespace ICSharpCode.NRefactory.Ast @@ -32,6 +38,12 @@ namespace ICSharpCode.NRefactory.Ast
}
}
public AbstractNode()
{
children = new NodeCollection();
children.Added += delegate(object sender, NodeEventArgs e) { e.Node.Parent = this; };
}
public virtual void AddChild(INode childNode)
{
Debug.Assert(childNode != null);

99
lib/NRefactory/Project/Src/Ast/NodeCollection.cs

@ -0,0 +1,99 @@ @@ -0,0 +1,99 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="David Srbecký" email="dsrbecky@gmail.com"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace ICSharpCode.NRefactory.Ast
{
public class NodeCollection: Collection<INode>
{
public event EventHandler<NodeEventArgs> Added;
public event EventHandler<NodeEventArgs> Removed;
protected virtual void OnAdded(NodeEventArgs e)
{
if (Added != null) {
Added(this, e);
}
}
protected virtual void OnRemoved(NodeEventArgs e)
{
if (Removed != null) {
Removed(this, e);
}
}
protected override void ClearItems()
{
while(this.Count > 0) {
this.RemoveFirst();
}
}
protected override void InsertItem(int index, INode item)
{
base.InsertItem(index, item);
OnAdded(new NodeEventArgs(item));
}
protected override void RemoveItem(int index)
{
INode removedNode = this[index];
base.RemoveItem(index);
OnRemoved(new NodeEventArgs(removedNode));
}
protected override void SetItem(int index, INode item)
{
base.RemoveItem(index);
base.InsertItem(index, item);
}
#region Convenience methods
public INode First {
get {
return this[0];
}
}
public INode Last {
get {
return this[this.Count - 1];
}
}
public void RemoveFirst()
{
this.RemoveAt(0);
}
public void RemoveLast()
{
this.RemoveAt(this.Count - 1);
}
#endregion
}
public class NodeEventArgs: EventArgs
{
INode inode;
public INode Node {
get { return inode; }
}
public NodeEventArgs(INode inode)
{
this.inode = inode;
}
}
}
Loading…
Cancel
Save