diff --git a/SharpNBT/TagBuilder.cs b/SharpNBT/TagBuilder.cs index 195d4bc..e16912a 100644 --- a/SharpNBT/TagBuilder.cs +++ b/SharpNBT/TagBuilder.cs @@ -376,5 +376,62 @@ namespace SharpNBT tree.Push(root); return root; } + + /// + /// Creates a new and pushes it to the current scope level, returning a object that pulls the current + /// scope back out one level when disposed. + /// + /// The name to apply to the , or to omit a name. + /// A that will close the when disposed. + /// This is essentially no different than and but can use `using` blocks to distinguish scope. + public Context NewCompound([CanBeNull] string name) + { + BeginCompound(name); + return new Context(tree.Peek(), EndCompound); + } + + /// + /// Creates a new and pushes it to the current scope level, returning a object that pulls the current + /// scope back out one level when disposed. + /// + /// The of the child items this list will contain. + /// The name to apply to the , or to omit a name. + /// A that will close the when disposed. + /// This is essentially no different than and but can use `using` blocks to distinguish scope. + public Context NewList(TagType childType, [CanBeNull] string name) + { + BeginList(childType, name); + return new Context(tree.Peek(), EndList); + } + + /// + /// Represents the context of a single "level" of depth into a AST. + /// + /// Implements to that each node can used with using statements for easily distinguishable scope. + [PublicAPI] + public class Context: IDisposable + { + internal delegate TagBuilder CloseHandler(); + private readonly CloseHandler closeHandler; + + /// + /// Gets the top-level tag for this context. + /// + [NotNull] + public TagContainer Tag { get; } + + internal Context([NotNull] TagContainer tag, [NotNull] CloseHandler handler) + { + Tag = tag; + closeHandler = handler; + } + + /// Closes this context. + public void Dispose() + { + Console.WriteLine(Tag); + closeHandler.Invoke(); + } + } } } \ No newline at end of file