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