|
| 1 | +namespace NetEvolve.CodeBuilder; |
| 2 | + |
| 3 | +using System; |
| 4 | + |
| 5 | +public partial record CSharpCodeBuilder |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Creates a scope that automatically manages indentation levels. |
| 9 | + /// </summary> |
| 10 | + /// <returns>A <see cref="ScopeHandler"/> that increments indentation on creation and decrements on disposal.</returns> |
| 11 | + /// <remarks> |
| 12 | + /// The returned scope handler implements <see cref="IDisposable"/> and is designed for use with |
| 13 | + /// the 'using' statement. When the scope is created, indentation is incremented by one level. |
| 14 | + /// When the scope is disposed (at the end of the using block), indentation is decremented. |
| 15 | + /// </remarks> |
| 16 | + /// <example> |
| 17 | + /// <code> |
| 18 | + /// using (builder.Scope()) |
| 19 | + /// { |
| 20 | + /// builder.AppendLine("return true;"); |
| 21 | + /// } |
| 22 | + /// </code> |
| 23 | + /// </example> |
| 24 | + public IDisposable Scope() => new ScopeHandler(this); |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// A disposable struct that manages indentation scope for a <see cref="CSharpCodeBuilder"/>. |
| 28 | + /// </summary> |
| 29 | + /// <remarks> |
| 30 | + /// This struct increments the indentation level when created and decrements it when disposed. |
| 31 | + /// It is designed to work with the 'using' statement to provide automatic indentation management. |
| 32 | + /// </remarks> |
| 33 | + private readonly struct ScopeHandler : IDisposable, IEquatable<ScopeHandler> |
| 34 | + { |
| 35 | + private readonly CSharpCodeBuilder _builder; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Initializes a new instance of the <see cref="ScopeHandler"/> struct and increments the indentation level. |
| 39 | + /// </summary> |
| 40 | + /// <param name="builder">The <see cref="CSharpCodeBuilder"/> instance to manage indentation for.</param> |
| 41 | + internal ScopeHandler(CSharpCodeBuilder builder) |
| 42 | + { |
| 43 | + _builder = builder; |
| 44 | + _ = _builder.Append('{'); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Decrements the indentation level when the scope is disposed. |
| 49 | + /// </summary> |
| 50 | + /// <remarks> |
| 51 | + /// This method is called automatically when the 'using' statement block ends. |
| 52 | + /// </remarks> |
| 53 | + public void Dispose() => _ = _builder.Append('}'); |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Determines whether the specified object is equal to the current instance. |
| 57 | + /// </summary> |
| 58 | + /// <param name="obj">The object to compare with the current instance.</param> |
| 59 | + /// <returns>Always returns <c>false</c> since ScopeHandler instances should not be compared.</returns> |
| 60 | + public override readonly bool Equals(object? obj) => false; |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Determines whether the specified ScopeHandler is equal to the current instance. |
| 64 | + /// </summary> |
| 65 | + /// <param name="other">The ScopeHandler to compare with the current instance.</param> |
| 66 | + /// <returns>Always returns <c>false</c> since ScopeHandler instances should not be compared.</returns> |
| 67 | + public readonly bool Equals(ScopeHandler other) => false; |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Returns the hash code for this instance. |
| 71 | + /// </summary> |
| 72 | + /// <returns>A hash code based on the internal builder reference.</returns> |
| 73 | + public override readonly int GetHashCode() => _builder?.GetHashCode() ?? 0; |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Determines whether two ScopeHandler instances are equal. |
| 77 | + /// </summary> |
| 78 | + /// <param name="_">The first instance to compare.</param> |
| 79 | + /// <param name="__">The second instance to compare.</param> |
| 80 | + /// <returns>Always returns <c>false</c> since ScopeHandler instances should not be compared.</returns> |
| 81 | + public static bool operator ==(ScopeHandler _, ScopeHandler __) => false; |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Determines whether two ScopeHandler instances are not equal. |
| 85 | + /// </summary> |
| 86 | + /// <param name="_">The first instance to compare.</param> |
| 87 | + /// <param name="__">The second instance to compare.</param> |
| 88 | + /// <returns>Always returns <c>true</c> since ScopeHandler instances should not be compared.</returns> |
| 89 | + public static bool operator !=(ScopeHandler _, ScopeHandler __) => true; |
| 90 | + } |
| 91 | +} |
0 commit comments