using System.CodeDom.Compiler;
namespace Terminal.Gui.Analyzers.Internal;
///
/// Just a simple set of extension methods to increment and decrement the indentation
/// level of an via push and pop terms, and to avoid having
/// explicit values all over the place.
///
public static class IndentedTextWriterExtensions
{
///
/// Decrements by 1, but only if it is greater than 0.
///
///
/// The resulting indentation level of the .
///
[MethodImpl (MethodImplOptions.AggressiveInlining)]
public static int Pop (this IndentedTextWriter w, string endScopeDelimiter = "}")
{
if (w.Indent > 0)
{
w.Indent--;
w.WriteLine (endScopeDelimiter);
}
return w.Indent;
}
///
/// Decrements by 1 and then writes a closing curly brace.
///
[MethodImpl (MethodImplOptions.AggressiveInlining)]
public static void PopCurly (this IndentedTextWriter w, bool withSemicolon = false)
{
w.Indent--;
if (withSemicolon)
{
w.WriteLine ("};");
}
else
{
w.WriteLine ('}');
}
}
///
/// Increments by 1, with optional parameters to customize the scope push.
///
/// An instance of an .
///
/// The first line to be written before indenting and before the optional line or
/// null if not needed.
///
///
/// An opening delimiter to write. Written before the indentation and after (if provided). Default is an opening curly brace.
///
/// Calling with no parameters will write an opening curly brace and a line break at the current indentation and then increment.
[MethodImpl (MethodImplOptions.AggressiveInlining)]
public static void Push (this IndentedTextWriter w, string? declaration = null, char scopeDelimiter = '{')
{
if (declaration is { Length: > 0 })
{
w.WriteLine (declaration);
}
w.WriteLine (scopeDelimiter);
w.Indent++;
}
}