CodeWriter
Wrapper around StringBuilder that provides formatting and utility methods for code writing.
internal sealed class CodeWriter : System.IDisposableExamples
Section titled “Examples”The following example demonstrates how to write a simple type declaration with a namespace, fields, and a method.
using Hertzole.SourceGen;
public class SourceGenerator{ public string Generate() { // Must dispose when the writer is no longer needed. using CodeWriter writer = new CodeWriter();
writer.AppendLine("using System;").AppendLine(); writer.AppendNamespace("My.Namespace"); writer.AppendLine("public class MyClass"); // Creates a new indented block surrounded by two brackets. using (writer.WithBlock()) { // Now inside the class scope
// Create a new field and a new line below that to add some space. writer.AppendLine("public int myField;").AppendLine();
writer.AppendLine("public void MyMethod()"); using (writer.WithBlock()) { writer.AppendLine("Console.WriteLine(\"Hello World!\");"); } }
// Call ToString to get the final string. return writer.ToString(); }}The returning string from the writer would output this:
using System;
namespace My.Namespace{ public class MyClass { public int myField;
public void MyMethod() { Console.WriteLine("Hello World!"); } }}Constructors
Section titled “Constructors”CodeWriter()
Section titled “CodeWriter()”Creates a new instance of a CodeWriter.
public CodeWriter();Properties
Section titled “Properties”Indent
Section titled “Indent”The current indent. Each indent is 4 spaces (equivalent to one tab).
public int Indent { get; set; }Methods
Section titled “Methods”Append(string?)
Section titled “Append(string?)”Appends string to the current line without creating a new line.
public CodeWriter Append(string? value);The string to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(ITypeSymbol, bool, bool)
Section titled “Append(ITypeSymbol, bool, bool)”Appends the string representation of the specified ITypeSymbol, along with its required declarations, without creating a newline.
Optionally inserts the partial keyword in the declaration if isPartial is true.
Optionally inserts the namespace if appendNamespace is true.
public CodeWriter Append(ITypeSymbol value, bool isPartial = true, bool appendNamespace = true);The type to append.
isPartial
Section titled “isPartial”Whether to append the partial keyword in the declaration.
appendNamespace
Section titled “appendNamespace”Whether to append the namespace.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(object)
Section titled “Append(object)”Appends the string representation of the specified object to the current line without creating a new line.
public CodeWriter Append(object value);The object to append.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(bool)
Section titled “Append(bool)”Appends bool to the current line without creating a new line.
public CodeWriter Append(bool value);The bool to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(decimal, string, IFormatProvider?)
Section titled “Append(decimal, string, IFormatProvider?)”Appends decimal with the specified format to the current line without creating a new line.
public CodeWriter Append(decimal value, string format, IFormatProvider? provider = null);The decimal to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(decimal)
Section titled “Append(decimal)”Appends decimal to the current line without creating a new line.
public CodeWriter Append(decimal value);The decimal to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(double, string, IFormatProvider?)
Section titled “Append(double, string, IFormatProvider?)”Appends double with the specified format to the current line without creating a new line.
public CodeWriter Append(double value, string format, IFormatProvider? provider = null);The double to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(float, string, IFormatProvider?)
Section titled “Append(float, string, IFormatProvider?)”Appends float with the specified format to the current line without creating a new line.
public CodeWriter Append(float value, string format, IFormatProvider? provider = null);The float to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(float)
Section titled “Append(float)”Appends float to the current line without creating a new line.
public CodeWriter Append(float value);The float to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(ulong, string, IFormatProvider?)
Section titled “Append(ulong, string, IFormatProvider?)”Appends ulong with the specified format to the current line without creating a new line.
public CodeWriter Append(ulong value, string format, IFormatProvider? provider = null);The ulong to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(ulong)
Section titled “Append(ulong)”Appends ulong to the current line without creating a new line.
public CodeWriter Append(ulong value);The ulong to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(long, string, IFormatProvider?)
Section titled “Append(long, string, IFormatProvider?)”Appends long with the specified format to the current line without creating a new line.
public CodeWriter Append(long value, string format, IFormatProvider? provider = null);The long to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(long)
Section titled “Append(long)”Appends long to the current line without creating a new line.
public CodeWriter Append(long value);The long to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(uint, string, IFormatProvider?)
Section titled “Append(uint, string, IFormatProvider?)”Appends uint with the specified format to the current line without creating a new line.
public CodeWriter Append(uint value, string format, IFormatProvider? provider = null);The uint to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(uint)
Section titled “Append(uint)”Appends uint to the current line without creating a new line.
public CodeWriter Append(uint value);The uint to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(int, string, IFormatProvider?)
Section titled “Append(int, string, IFormatProvider?)”Appends int with the specified format to the current line without creating a new line.
public CodeWriter Append(int value, string format, IFormatProvider? provider = null);The int to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(double)
Section titled “Append(double)”Appends double to the current line without creating a new line.
public CodeWriter Append(double value);The double to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(ushort, string, IFormatProvider?)
Section titled “Append(ushort, string, IFormatProvider?)”Appends ushort with the specified format to the current line without creating a new line.
public CodeWriter Append(ushort value, string format, IFormatProvider? provider = null);The ushort to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(ReadOnlyMemory<char>)
Section titled “Append(ReadOnlyMemory<char>)”Appends ReadOnlyMemory<char> to the current line without creating a new line.
public CodeWriter Append(ReadOnlyMemory<char> value);The ReadOnlyMemory<char> to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(ArrayBuilder<char>)
Section titled “Append(ArrayBuilder<char>)”Appends ArrayBuilder<char> to the current line without creating a new line.
public CodeWriter Append(ArrayBuilder<char> value);The ArrayBuilder<char> to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(char)
Section titled “Append(char)”Appends char to the current line without creating a new line.
public CodeWriter Append(char value);The char to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(char, int)
Section titled “Append(char, int)”Appends the specified number of copies of the specified char value to the current line without creating a new line.
public CodeWriter Append(char value, int repeatCount);The char to append.
repeatCount
Section titled “repeatCount”How many times the char should be inserted.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(char[])
Section titled “Append(char[])”Appends the specified char array to the current line without creating a new line.
public CodeWriter Append(char[] value);The char array to append.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(int)
Section titled “Append(int)”Appends int to the current line without creating a new line.
public CodeWriter Append(int value);The int to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(ReadOnlySpan<char>)
Section titled “Append(ReadOnlySpan<char>)”Appends ReadOnlySpan<char> to the current line without creating a new line.
public CodeWriter Append(ReadOnlySpan<char> value);The ReadOnlySpan<char> to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(char[], int, int)
Section titled “Append(char[], int, int)”Appends a subarray of characters to the current line without creating a new line.
public CodeWriter Append(char[] value, int startIndex, int charCount);The character array to append.
startIndex
Section titled “startIndex”The starting position in the character array.
charCount
Section titled “charCount”The number of characters to append.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(byte, string, IFormatProvider?)
Section titled “Append(byte, string, IFormatProvider?)”Appends byte with the specified format to the current line without creating a new line.
public CodeWriter Append(byte value, string format, IFormatProvider? provider = null);The byte to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(sbyte)
Section titled “Append(sbyte)”Appends sbyte to the current line without creating a new line.
public CodeWriter Append(sbyte value);The sbyte to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(sbyte, string, IFormatProvider?)
Section titled “Append(sbyte, string, IFormatProvider?)”Appends sbyte with the specified format to the current line without creating a new line.
public CodeWriter Append(sbyte value, string format, IFormatProvider? provider = null);The sbyte to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(short)
Section titled “Append(short)”Appends short to the current line without creating a new line.
public CodeWriter Append(short value);The short to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(short, string, IFormatProvider?)
Section titled “Append(short, string, IFormatProvider?)”Appends short with the specified format to the current line without creating a new line.
public CodeWriter Append(short value, string format, IFormatProvider? provider = null);The short to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(ushort)
Section titled “Append(ushort)”Appends ushort to the current line without creating a new line.
public CodeWriter Append(ushort value);The ushort to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
Append(byte)
Section titled “Append(byte)”Appends byte to the current line without creating a new line.
public CodeWriter Append(byte value);The byte to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendConditionalSymbol(string?)
Section titled “AppendConditionalSymbol(string?)”Appends a conditional preprocessor directive (e.g. #if DEBUG). Does nothing if the condition is null or whitespace.
public CodeWriter AppendConditionalSymbol(string? condition);condition
Section titled “condition”The condition for the preprocessor directive.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendEmbeddedAttribute()
Section titled “AppendEmbeddedAttribute()”Appends an [Embedded] attribute to the current line. You should only use this if you’ve added Microsoft.CodeAnalysis.EmbeddedAttribute.
public CodeWriter AppendEmbeddedAttribute();Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendExcludeFromCodeCoverageAttribute()
Section titled “AppendExcludeFromCodeCoverageAttribute()”Appends an ExcludeFromCodeCoverage attribute to the current line, followed by a newline.
public CodeWriter AppendExcludeFromCodeCoverageAttribute();Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendGeneratedCodeAttribute(string, string)
Section titled “AppendGeneratedCodeAttribute(string, string)”Appends a GeneratedCode attribute to the current line, followed by a newline.
public CodeWriter AppendGeneratedCodeAttribute(string generator, string version);generator
Section titled “generator”The name of the code generator.
version
Section titled “version”The version of the code generator.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendIndentedSource(ReadOnlySpan<char>)
Section titled “AppendIndentedSource(ReadOnlySpan<char>)”Appends an entire multi-line string to the writer and handles the indentation.
public CodeWriter AppendIndentedSource(ReadOnlySpan<char> value);The value to append.
Examples
Section titled “Examples”The following example demonstrates how to use AppendIndentedSource to insert a multi-line string.
using Hertzole.SourceGen;
internal static class CodeHelper{ public static void AppendDisposedBlock(CodeWriter writer) { writer.AppendIndentedSource( """ if (isDisposed) { throw new System.ObjectDisposedException("This object is disposed."); } """ ); }}Remarks
Section titled “Remarks”This method automatically indents each line of the string according to the current indentation level. It normalizes line endings and replaces tabs with spaces.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendIndentedSource(string)
Section titled “AppendIndentedSource(string)”Appends an entire multi-line string to the writer and handles the indentation.
public CodeWriter AppendIndentedSource(string value);The value to append.
Examples
Section titled “Examples”The following example demonstrates how to use AppendIndentedSource to insert a multi-line string.
using Hertzole.SourceGen;
internal static class CodeHelper{ public static void AppendDisposedBlock(CodeWriter writer) { writer.AppendIndentedSource( """ if (isDisposed) { throw new System.ObjectDisposedException("This object is disposed."); } """ ); }}Remarks
Section titled “Remarks”This method automatically indents each line of the string according to the current indentation level. It normalizes line endings and replaces tabs with spaces.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(long, string, IFormatProvider?)
Section titled “AppendLine(long, string, IFormatProvider?)”Appends long with the specified format followed by a newline to the current line.
public CodeWriter AppendLine(long value, string format, IFormatProvider? provider = null);The long to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(ulong)
Section titled “AppendLine(ulong)”Appends ulong followed by a newline to the current line.
public CodeWriter AppendLine(ulong value);The ulong to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(ulong, string, IFormatProvider?)
Section titled “AppendLine(ulong, string, IFormatProvider?)”Appends ulong with the specified format followed by a newline to the current line.
public CodeWriter AppendLine(ulong value, string format, IFormatProvider? provider = null);The ulong to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(float)
Section titled “AppendLine(float)”Appends float followed by a newline to the current line.
public CodeWriter AppendLine(float value);The float to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(float, string, IFormatProvider?)
Section titled “AppendLine(float, string, IFormatProvider?)”Appends float with the specified format followed by a newline to the current line.
public CodeWriter AppendLine(float value, string format, IFormatProvider? provider = null);The float to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(double)
Section titled “AppendLine(double)”Appends double followed by a newline to the current line.
public CodeWriter AppendLine(double value);The double to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(decimal)
Section titled “AppendLine(decimal)”Appends decimal followed by a newline to the current line.
public CodeWriter AppendLine(decimal value);The decimal to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(long)
Section titled “AppendLine(long)”Appends long followed by a newline to the current line.
public CodeWriter AppendLine(long value);The long to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(decimal, string, IFormatProvider?)
Section titled “AppendLine(decimal, string, IFormatProvider?)”Appends decimal with the specified format followed by a newline to the current line.
public CodeWriter AppendLine(decimal value, string format, IFormatProvider? provider = null);The decimal to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(bool)
Section titled “AppendLine(bool)”Appends bool followed by a newline to the current line.
public CodeWriter AppendLine(bool value);The bool to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(object)
Section titled “AppendLine(object)”Appends the string representation of the specified object followed by a newline to the current line.
public CodeWriter AppendLine(object value);The object to append.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(ITypeSymbol, bool, bool)
Section titled “AppendLine(ITypeSymbol, bool, bool)”Appends the string representation of the specified ITypeSymbol, along with its required declarations, followed by a newline.
Optionally inserts the partial keyword in the declaration if isPartial is true.
Optionally inserts the namespace if appendNamespace is true.
public CodeWriter AppendLine(ITypeSymbol value, bool isPartial = true, bool appendNamespace = true);The type to append.
isPartial
Section titled “isPartial”Whether to append the partial keyword in the declaration.
appendNamespace
Section titled “appendNamespace”Whether to append the namespace.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(double, string, IFormatProvider?)
Section titled “AppendLine(double, string, IFormatProvider?)”Appends double with the specified format followed by a newline to the current line.
public CodeWriter AppendLine(double value, string format, IFormatProvider? provider = null);The double to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(uint, string, IFormatProvider?)
Section titled “AppendLine(uint, string, IFormatProvider?)”Appends uint with the specified format followed by a newline to the current line.
public CodeWriter AppendLine(uint value, string format, IFormatProvider? provider = null);The uint to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(char[], int, int)
Section titled “AppendLine(char[], int, int)”Appends a subarray of characters followed by a newline to the current line.
public CodeWriter AppendLine(char[] value, int startIndex, int charCount);The character array to append.
startIndex
Section titled “startIndex”The starting position in the character array.
charCount
Section titled “charCount”The number of characters to append.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(int, string, IFormatProvider?)
Section titled “AppendLine(int, string, IFormatProvider?)”Appends int with the specified format followed by a newline to the current line.
public CodeWriter AppendLine(int value, string format, IFormatProvider? provider = null);The int to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine()
Section titled “AppendLine()”Appends a newline to the current line.
public CodeWriter AppendLine();Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(string?)
Section titled “AppendLine(string?)”Appends string followed by a newline to the current line.
public CodeWriter AppendLine(string? value);The string to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(ReadOnlySpan<char>)
Section titled “AppendLine(ReadOnlySpan<char>)”Appends ReadOnlySpan<char> followed by a newline to the current line.
public CodeWriter AppendLine(ReadOnlySpan<char> value);The ReadOnlySpan<char> to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(ReadOnlyMemory<char>)
Section titled “AppendLine(ReadOnlyMemory<char>)”Appends ReadOnlyMemory<char> followed by a newline to the current line.
public CodeWriter AppendLine(ReadOnlyMemory<char> value);The ReadOnlyMemory<char> to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(uint)
Section titled “AppendLine(uint)”Appends uint followed by a newline to the current line.
public CodeWriter AppendLine(uint value);The uint to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(char)
Section titled “AppendLine(char)”Appends char followed by a newline to the current line.
public CodeWriter AppendLine(char value);The char to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(char, int)
Section titled “AppendLine(char, int)”Appends the specified number of copies of the specified char followed by a newline to the current line.
public CodeWriter AppendLine(char value, int repeatCount);The char to append.
repeatCount
Section titled “repeatCount”How many times the char should be inserted.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(char[])
Section titled “AppendLine(char[])”Appends the specified char array followed by a newline to the current line.
public CodeWriter AppendLine(char[] value);The char array to append.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(ArrayBuilder<char>)
Section titled “AppendLine(ArrayBuilder<char>)”Appends ArrayBuilder<char> followed by a newline to the current line.
public CodeWriter AppendLine(ArrayBuilder<char> value);The ArrayBuilder<char> to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(ushort)
Section titled “AppendLine(ushort)”Appends ushort followed by a newline to the current line.
public CodeWriter AppendLine(ushort value);The ushort to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(int)
Section titled “AppendLine(int)”Appends int followed by a newline to the current line.
public CodeWriter AppendLine(int value);The int to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(ushort, string, IFormatProvider?)
Section titled “AppendLine(ushort, string, IFormatProvider?)”Appends ushort with the specified format followed by a newline to the current line.
public CodeWriter AppendLine(ushort value, string format, IFormatProvider? provider = null);The ushort to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(byte)
Section titled “AppendLine(byte)”Appends byte followed by a newline to the current line.
public CodeWriter AppendLine(byte value);The byte to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(short, string, IFormatProvider?)
Section titled “AppendLine(short, string, IFormatProvider?)”Appends short with the specified format followed by a newline to the current line.
public CodeWriter AppendLine(short value, string format, IFormatProvider? provider = null);The short to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(sbyte, string, IFormatProvider?)
Section titled “AppendLine(sbyte, string, IFormatProvider?)”Appends sbyte with the specified format followed by a newline to the current line.
public CodeWriter AppendLine(sbyte value, string format, IFormatProvider? provider = null);The sbyte to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(sbyte)
Section titled “AppendLine(sbyte)”Appends sbyte followed by a newline to the current line.
public CodeWriter AppendLine(sbyte value);The sbyte to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(byte, string, IFormatProvider?)
Section titled “AppendLine(byte, string, IFormatProvider?)”Appends byte with the specified format followed by a newline to the current line.
public CodeWriter AppendLine(byte value, string format, IFormatProvider? provider = null);The byte to insert.
format
Section titled “format”A standard or custom numeric format string.
provider
Section titled “provider”A optional object that supplies culture-specific formatting information.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendLine(short)
Section titled “AppendLine(short)”Appends short followed by a newline to the current line.
public CodeWriter AppendLine(short value);The short to insert.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendNamespace(INamespaceSymbol?)
Section titled “AppendNamespace(INamespaceSymbol?)”Appends a namespace declaration for the specified symbol. Does nothing if the symbol is null or a global namespace.
public CodeWriter AppendNamespace(INamespaceSymbol? symbol);symbol
Section titled “symbol”The namespace symbol to append.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendNamespace(string)
Section titled “AppendNamespace(string)”Appends a namespace declaration. Does nothing if the value is null or empty.
public CodeWriter AppendNamespace(string value);The namespace name to append.
Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendNullable()
Section titled “AppendNullable()”Appends #nullable enable. If appended, #nullable restore will be appended at the end of the file.
public CodeWriter AppendNullable();Returns
Section titled “Returns”The current code writer instance after the append operation.
AppendPreprocessorSymbol(string?)
Section titled “AppendPreprocessorSymbol(string?)”Appends a preprocessor symbol. Prepends # if the value does not already start with it. Does nothing if the value is null or whitespace.
public CodeWriter AppendPreprocessorSymbol(string? value);The preprocessor symbol to append (e.g. #endif or endif).
Returns
Section titled “Returns”The current code writer instance after the append operation.
Clear()
Section titled “Clear()”Clears all written content and resets the writer state.
public CodeWriter Clear();Returns
Section titled “Returns”The current code writer instance after the append operation.
Dispose()
Section titled “Dispose()”Disposes the code writer and returns the underlying StringBuilder to the pool.
public void Dispose();ToString()
Section titled “ToString()”Returns the written content as a string. Closes any open namespace block and appends #nullable restore if needed.
public override string ToString();Returns
Section titled “Returns”The written content as a string.
WithBlock(bool)
Section titled “WithBlock(bool)”Opens a new code block. Returns a disposable scope that closes the block and restores indentation when disposed.
public BlockScope WithBlock(bool newLineOnDispose = false);newLineOnDispose
Section titled “newLineOnDispose”If true, a newline will be inserted when the block scope is disposed.
Examples
Section titled “Examples”The following example demonstrates how to use WithBlock.
using Hertzole.SourceGen;
internal static class CodeHelper{ public static string Write() { using CodeWriter writer = new CodeWriter(); AppendMethod(writer, "Method1", true); AppendMethod(writer, "Method2", false);
return writer.ToString(); }
public static void AppendMethod(CodeWriter writer, string methodName, bool isPublic) { writer.Append(isPublic ? "public " : "private ") .Append("void ").Append(methodName).AppendLine("()");
// Must dispose the scope to close the block and restore indentation. using (writer.WithBlock()) { writer.AppendLine("Console.WriteLine(\"Hello World!\");"); } }}The output from the writer would be:
public void Method1(){ Console.WriteLine("Hello World!");}public void Method2(){ Console.WriteLine("Hello World!");}The following examples demonstrates the output when newLineOnDispose is set to true.
using Hertzole.SourceGen;
internal static class CodeHelper{ public static string Write() { using CodeWriter writer = new CodeWriter(); AppendMethod(writer, "Method1", true); AppendMethod(writer, "Method2", false);
return writer.ToString(); }
public static void AppendMethod(CodeWriter writer, string methodName, bool isPublic) { writer.Append(isPublic ? "public " : "private ") .Append("void ").Append(methodName).AppendLine("()");
// Must dispose the scope to close the block and restore indentation. // This will add a new line when closing the block. using (writer.WithBlock(true)) { writer.AppendLine("Console.WriteLine(\"Hello World!\");"); } }}The output from the writer would be:
public void Method1(){ Console.WriteLine("Hello World!");}
public void Method2(){ Console.WriteLine("Hello World!");}Returns
Section titled “Returns”A disposable scope that closes the block when disposed.
WithCondition(string?)
Section titled “WithCondition(string?)”Creates a scope in a preprocessor conditional block (#if CONDITION). Returns a disposable scope that closes the conditional block (#endif) when disposed.
public ConditionalScope WithCondition(string? condition);condition
Section titled “condition”The condition.
Examples
Section titled “Examples”The following example demonstrates how to use WithCondition to wrap the content in a preprocessor conditional block.
using Hertzole.SourceGen;
internal static class CodeHelper{ public static void Write() { using CodeWriter writer = new CodeWriter(); writer.AppendLine("public void Method()"); using (writer.WithBlock()) { AppendDebugLog(writer, "Calling method!"); writer.AppendLine("// TODO: Method stuff"); } }
public static void AppendDebugLog(CodeWriter writer, string message) { using (writer.WithCondition("DEBUG")) { writer.AppendLine($"System.Diagnostics.Debug.WriteLine(\"{message}\");"); } }}The output from the writer would be:
public void Method(){#if DEBUG System.Diagnostics.Debug.WriteLine("Calling method!");#endif // TODO: Method stuff}Remarks
Section titled “Remarks”You can write #if CONDITION, if CONDITION or just CONDITION and the condition will be wrapped in #if and #endif, as long as the provided string isn’t null or empty/whitespace.
Returns
Section titled “Returns”A disposable scope that closes the conditional block when disposed.
WithIndent(int)
Section titled “WithIndent(int)”Temporarily changes the indentation level. Returns a disposable scope that restores the original indentation when disposed.
public IndentScope WithIndent(int newIndent);newIndent
Section titled “newIndent”The indentation level to use within the scope.
Examples
Section titled “Examples”The following example demonstrates how to use WithIndent to change indentation.
using Hertzole.SourceGen;
internal static class CodeHelper{ public static void Write() { using CodeWriter writer = new CodeWriter(); writer.Indent = 1; writer.AppendLine("// Stairs"); AppendStairs(writer, 5); }
public static void AppendStairs(CodeWriter writer, int length) { using (writer.WithIndent(0)) { for (int i = 0; i < length; i++) { writer.Append(' ', i).AppendLine('_'); } } }}The output from the writer would be:
// Stairs_ _ _ _ _Returns
Section titled “Returns”A disposable scope that restores the original indentation when disposed.