ktsu.CodeBlocker
1.2.11
Prefix Reserved
See the version list below for details.
dotnet add package ktsu.CodeBlocker --version 1.2.11
NuGet\Install-Package ktsu.CodeBlocker -Version 1.2.11
<PackageReference Include="ktsu.CodeBlocker" Version="1.2.11" />
<PackageVersion Include="ktsu.CodeBlocker" Version="1.2.11" />
<PackageReference Include="ktsu.CodeBlocker" />
paket add ktsu.CodeBlocker --version 1.2.11
#r "nuget: ktsu.CodeBlocker, 1.2.11"
#:package ktsu.CodeBlocker@1.2.11
#addin nuget:?package=ktsu.CodeBlocker&version=1.2.11
#tool nuget:?package=ktsu.CodeBlocker&version=1.2.11
ktsu.CodeBlocker
An IndentedTextWriter that makes generating code blocks easier.
Introduction
CodeBlocker is a specialized utility built on top of IndentedTextWriter that simplifies the process of programmatically generating structured code. It provides automatic indentation management and a fluent interface for creating code blocks with proper nesting, making it ideal for code generation tasks, template engines, and dynamic source code creation.
Features
- Automatic Indentation: Properly manages indentation levels as you create nested code blocks
- Configurable Indentation: Support for custom indent strings (tabs, spaces, or any custom pattern)
- Scope Management: Uses C#
usingstatements for clean, readable scope creation with automatic brace handling powered byktsu.ScopedAction, with optional trailing semicolons viaScopeWithTrailingSemicolon - Flexible API: Write individual lines or entire code blocks with proper formatting
- Standard Output Support: Works with StringWriter for flexible output handling
- Cross-Platform: Supports .NET 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, .NET Standard 2.0 and 2.1
- Lightweight: Minimal dependencies, built on top of
ktsu.ScopedActionfor robust scope management - Well-Tested: Includes comprehensive unit and integration tests
Installation
Package Manager Console
Install-Package ktsu.CodeBlocker
.NET CLI
dotnet add package ktsu.CodeBlocker
Package Reference
<PackageReference Include="ktsu.CodeBlocker" Version="1.1.5" />
Usage Examples
Basic Example
namespace CodeBlockerExample;
using ktsu.CodeBlocker;
internal class Example
{
public static void GenerateCode()
{
// Create a new CodeBlocker instance with default tab indentation
using var codeBlocker = CodeBlocker.Create();
// Write using statements and namespace
codeBlocker.WriteLine("using System;");
codeBlocker.NewLine(); // Add empty line without indentation
codeBlocker.WriteLine("namespace Example");
// Use Scope for automatic brace and indentation management
using (new Scope(codeBlocker))
{
codeBlocker.WriteLine("public class Example");
using (new Scope(codeBlocker))
{
codeBlocker.WriteLine("public static void Main()");
using (new Scope(codeBlocker))
{
codeBlocker.WriteLine("Console.WriteLine(\"Hello, World!\");");
} // Scope automatically writes closing brace and manages indentation
}
}
// Output the generated code
Console.WriteLine(codeBlocker.ToString());
}
}
The above example generates the following code:
using System;
namespace Example
{
public class Example
{
public static void Main()
{
Console.WriteLine("Hello, World!");
}
}
}
Note: The
Scopeclass writes closing braces without semicolons (}). If you need trailing semicolons after closing braces (};), such as for enum declarations or struct initializers in C/C++, useScopeWithTrailingSemicoloninstead.
Custom Indentation
CodeBlocker supports configurable indentation strings, allowing you to use spaces, tabs, or any custom pattern:
// Using 2 spaces for indentation
using var codeBlocker2Space = CodeBlocker.Create(" ");
// Using 4 spaces for indentation
using var codeBlocker4Space = CodeBlocker.Create(" ");
// Using custom patterns (e.g., for markup generation)
using var customCodeBlocker = CodeBlocker.Create("-->");
// With existing StringWriter and custom indentation
using var stringWriter = new StringWriter();
using var codeBlocker = new CodeBlocker(stringWriter, " ");
codeBlocker.WriteLine("function example() {");
codeBlocker.Indent();
codeBlocker.WriteLine("console.log('Hello with 2 spaces!');");
codeBlocker.Outdent();
codeBlocker.WriteLine("}");
Console.WriteLine(codeBlocker.ToString());
// Output:
// function example() {
// console.log('Hello with 2 spaces!');
// }
// Check current indent configuration
Console.WriteLine($"Current indent: '{codeBlocker.IndentString}'"); // " "
// Custom indentation works seamlessly with Scope
using var scopeCodeBlocker = CodeBlocker.Create(" "); // 4 spaces
scopeCodeBlocker.WriteLine("public class Example");
using (new Scope(scopeCodeBlocker))
{
scopeCodeBlocker.WriteLine("public void Method()");
using (new Scope(scopeCodeBlocker))
{
scopeCodeBlocker.WriteLine("// 4-space indented code");
}
}
Advanced Usage
// Creating a CodeBlocker with a custom StringWriter
using var stringWriter = new StringWriter();
using var codeBlocker = new CodeBlocker(stringWriter);
// Generate a more complex structure
codeBlocker.WriteLine("public interface IExample");
using (new Scope(codeBlocker))
{
// Define interface methods
codeBlocker.WriteLine("void Method1();");
codeBlocker.WriteLine("string Method2(int parameter);");
// Define nested interface
codeBlocker.NewLine();
codeBlocker.WriteLine("public interface INestedExample");
using (new Scope(codeBlocker))
{
codeBlocker.WriteLine("void NestedMethod();");
}
}
// Add implementation
codeBlocker.NewLine();
codeBlocker.WriteLine("public class Implementation : IExample");
using (new Scope(codeBlocker))
{
// Implement methods
codeBlocker.WriteLine("public void Method1()");
using (new Scope(codeBlocker))
{
codeBlocker.WriteLine("// Implementation here");
}
codeBlocker.NewLine();
codeBlocker.WriteLine("public string Method2(int parameter)");
using (new Scope(codeBlocker))
{
codeBlocker.WriteLine("return parameter.ToString();");
}
}
// Get the result
string result = codeBlocker.ToString();
API Reference
CodeBlocker Class
The main class for building indented code blocks.
Constructors
| Name | Description |
|---|---|
CodeBlocker(StringWriter stringWriter) |
Creates a new CodeBlocker with the specified StringWriter using tab indentation |
CodeBlocker(StringWriter stringWriter, string indentString) |
Creates a new CodeBlocker with the specified StringWriter and custom indent string |
Properties
| Name | Type | Description |
|---|---|---|
CurrentIndent |
int |
Gets or sets the current indentation level |
IndentString |
string |
Gets the current indent string being used (e.g., "\t", " ", " ") |
Methods
| Name | Return Type | Description |
|---|---|---|
WriteLine(string line) |
void |
Writes a line of text with appropriate indentation |
WriteLine() |
void |
Writes an empty line with current indentation |
Write(string text) |
void |
Writes text without adding a new line |
NewLine() |
void |
Writes an empty line without indentation |
Indent() |
void |
Increases the indent level |
Outdent() |
void |
Decreases the indent level |
ToString() |
string |
Returns the generated code as a string |
Create() |
CodeBlocker |
Static factory method to create a new CodeBlocker instance with tab indentation |
Create(string indentString) |
CodeBlocker |
Static factory method to create a new CodeBlocker instance with custom indentation |
Dispose() |
void |
Disposes of the CodeBlocker and underlying resources |
Scope Class
Helper class for managing indentation scopes with automatic brace handling. Built on top of ktsu.ScopedAction for guaranteed resource cleanup and exception safety.
Constructor
| Name | Description |
|---|---|
Scope(CodeBlocker codeBlocker) |
Creates a new scope that automatically writes opening brace {, increases indentation, and handles cleanup on disposal |
Methods
| Name | Return Type | Description |
|---|---|---|
Dispose() |
void |
Decreases indentation level and writes closing brace } when scope is exited |
Behavior
- On Creation: Writes
{and increases indentation level - On Disposal: Decreases indentation level and writes
} - Exception Safety: Guaranteed cleanup even if exceptions occur within the scope
- Resource Management: Built on
ktsu.ScopedActionfor reliable resource handling
ScopeWithTrailingSemicolon Class
Variant of Scope that appends a semicolon after the closing brace. Useful for code generation scenarios like C/C++ enum or struct declarations where a trailing semicolon is required.
Constructor
| Name | Description |
|---|---|
ScopeWithTrailingSemicolon(CodeBlocker codeBlocker) |
Creates a new scope that automatically writes opening brace {, increases indentation, and handles cleanup on disposal |
Methods
| Name | Return Type | Description |
|---|---|---|
Dispose() |
void |
Decreases indentation level and writes closing brace with semicolon }; when scope is exited |
Behavior
- On Creation: Writes
{and increases indentation level - On Disposal: Decreases indentation level and writes
}; - Exception Safety: Guaranteed cleanup even if exceptions occur within the scope
- Resource Management: Built on
ktsu.ScopedActionfor reliable resource handling
Contributing
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE.md file for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- ktsu.ScopedAction (>= 1.1.30)
- System.Memory (>= 4.6.3)
- System.Threading.Tasks.Extensions (>= 4.6.3)
-
.NETStandard 2.1
- ktsu.ScopedAction (>= 1.1.30)
-
net10.0
- ktsu.ScopedAction (>= 1.1.30)
-
net5.0
- ktsu.ScopedAction (>= 1.1.30)
-
net6.0
- ktsu.ScopedAction (>= 1.1.30)
-
net7.0
- ktsu.ScopedAction (>= 1.1.30)
-
net8.0
- ktsu.ScopedAction (>= 1.1.30)
-
net9.0
- ktsu.ScopedAction (>= 1.1.30)
NuGet packages (4)
Showing the top 4 NuGet packages that depend on ktsu.CodeBlocker:
| Package | Downloads |
|---|---|
|
ktsu.Schema
A .NET library for defining, managing, and editing data structure schemas with a rich type system, type-safe identifiers, and polymorphic JSON serialization. Define structured data models programmatically or visually and serialize them to .schema.json files, providing a metadata foundation for code generation, data validation, and tooling that needs to reason about your data structures. Ships with an accompanying Dear ImGui desktop editor for visual schema authoring. |
|
|
ktsu.Coder.Core
A flexible and extensible .NET library for representing code as language-agnostic Abstract Syntax Trees, serializing them to human-readable YAML for round-trip storage and version control, and generating source in C#, Python, JavaScript and C++. Provides strongly-typed AST nodes for classes, functions, parameters, statements, expressions and typed literals, with a plugin-based architecture for adding target languages, full deep-cloning support and custom metadata on any node. Ships an ImGui node-graph editor over the same AST, with a force-directed layout, an inspector for every node's properties, undo/redo, and a desktop application built on it. |
|
|
ktsu.Coder
A flexible and extensible .NET library for representing code as language-agnostic Abstract Syntax Trees, serializing them to human-readable YAML for round-trip storage and version control, and generating source in C#, Python, JavaScript and C++. Provides strongly-typed AST nodes for classes, functions, parameters, statements, expressions and typed literals, with a plugin-based architecture for adding target languages, full deep-cloning support and custom metadata on any node. Ships an ImGui node-graph editor over the same AST, with a force-directed layout, an inspector for every node's properties, undo/redo, and a desktop application built on it. |
|
|
ktsu.SourceGeneratorToolkit
A .NET library providing reusable infrastructure for Roslyn source generators: an IIncrementalGenerator base driven by JSON metadata files supplied as AdditionalFiles, diagnostic descriptor allocation and reporting under one identifier scheme, navigable source locations into the metadata, and deterministic LF output for generated sources that are committed and verified. A companion testing package supplies a CSharpGeneratorDriver harness that runs a generator the way MSBuild does and checks it caches its output like an incremental generator should. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.0.4 | 713 | 9/7/2026 |
| 2.0.3 | 119 | 9/4/2026 |
| 2.0.2 | 267 | 9/3/2026 |
| 2.0.1 | 208 | 8/28/2026 |
| 2.0.0 | 131 | 8/27/2026 |
| 1.3.0 | 224 | 8/27/2026 |
| 1.2.16 | 122 | 8/27/2026 |
| 1.2.15 | 142 | 8/25/2026 |
| 1.2.14 | 108 | 8/24/2026 |
| 1.2.13 | 114 | 8/21/2026 |
| 1.2.12 | 115 | 8/20/2026 |
| 1.2.11 | 116 | 8/19/2026 |
| 1.2.10 | 95 | 8/18/2026 |
| 1.2.9 | 111 | 8/17/2026 |
| 1.2.8 | 310 | 7/3/2026 |
| 1.2.7 | 124 | 7/2/2026 |
| 1.2.6 | 133 | 7/1/2026 |
| 1.2.5 | 134 | 6/30/2026 |
| 1.2.4 | 153 | 6/29/2026 |
| 1.2.3 | 126 | 6/28/2026 |
## v1.2.11 (patch)
Changes since v1.2.10:
- chore: store icon.png in LFS as .gitattributes declares ([@matt-edmondson](https://github.com/matt-edmondson))
- docs: scope build badge to the default branch ([@matt-edmondson](https://github.com/matt-edmondson))
- docs: correct README, DESCRIPTION and TAGS metadata ([@matt-edmondson](https://github.com/matt-edmondson))