What is MatchBrackets?

MatchBrackets is a lightweight .NET 6 / .NET 8 library that scans source or text input and produces a Dictionary<int,int> mapping each opening bracket to its closing counterpart. It supports (), {}, [], <> in a single pass for fast editor features like brace colorization, region highlighting, structural navigation, and safe extraction of bracketed spans.

Why Use It?

  • Simple API: pass a string, get precise index pairs
  • Supports multi-targeting (net6.0, net8.0)
  • Eliminates manual stack handling boilerplate
  • Enables efficient syntax tools, refactor helpers, and code analysis extensions
  • Locate matching pairs to make it easier to color paired brackets — perfect for rainbow bracket extensions
  • MIT licensed, small footprint, no dependencies

Key Use Case: Bracket Colorization

One of the primary uses of MatchBrackets is to locate matching bracket pairs for visual colorization. By returning exact index positions of each opening and closing bracket, the library makes it trivial to implement rainbow bracket features, matching bracket highlighting, or any other visual enhancement that requires knowing which brackets belong together.

// Example: Get bracket pairs for colorization
var pairs = BracketMatcher.Match(sourceCode);

// Now you can assign colors based on nesting depth
foreach (var pair in pairs)
{
    int openIndex = pair.Key;
    int closeIndex = pair.Value;

    // Apply color to matching brackets at these positions
    ApplyBracketColor(openIndex, closeIndex, nestingLevel);
}

Accelerate editor enhancements and parsing workflows by offloading bracket pairing logic to a focused, battle‑tested utility. Let your extension or analysis code work with clean structural boundaries instead of ad hoc scans.

📦 Install via NuGet

MatchBrackets Supports .NET 6 & 8

NuGet\Install-Package MatchBrackets
💡 Target Framework Support
  • .NET 6.0: Long-term support release
  • .NET 8.0: Latest LTS with performance improvements

ℹ️ MatchBrackets has no external dependencies and works across all .NET platforms including Windows, Linux, and macOS.