$ show izhangzhihao/intellij-rainbow-brackets
This repository is the open-source 'Rainbow Brackets Lite' IntelliJ IDEA plugin, a free and feature-limited version of the popular Rainbow Brackets plugin. The plugin colorizes nested brackets (round, square, squiggly, angle) with distinct colors to improve code readability, and provides additional features like rainbow indent guide lines and scope highlighting via mouse shortcuts. It is implemented as a JetBrains plugin using the IntelliJ Platform SDK, with core logic split between an Annotator (for simple leaf-elements) and a HighlightVisitor (for more accurate per-language matching via BracePairProvider extension points). The architecture follows standard IntelliJ plugin patterns: extension points for brace providers, custom text attributes, and a thorough settings UI integrated into the IDE's color scheme page. The codebase is well-structured with proper separation of concerns, utility functions (memoization, color blending), and comprehensive tests for multiple languages. While tightly coupled to the IntelliJ API, several components (BracePairProvider interface, memoization utility, color blending) demonstrate non-obvious skill and could be extracted for reuse in other JetBrains plugins or JVM projects.
The LLM identified 5 files worth highlighting for reuse:
Provides generic memoization for functions with 1-3 parameters using a thread-safe ConcurrentHashMap. The `memoize()` extension function allows any lambda to be cached, and the internal `MemoizedHandler` avoids double computation via putIfAbsent. This utility is isolated, well-tested (by usage), and can be extracted directly into any JVM project requiring lazy caching without external dependencies.
Contains a single function `alphaBlend` that blends a foreground color onto a background color with a given alpha (0.0-1.0). It correctly clamps alpha and computes RGB channels linearly. This is a self-contained, stateless utility that can be reused in any Java/Kotlin project for color compositing, with no dependencies on IntelliJ APIs.
Defines the `BracePairProvider` interface with two methods: `pairs()` and `blackList()`. This is an extension point for the plugin, allowing language-specific providers to define additional brace pairs or exclude certain pairs from rainbow highlighting. The interface is minimal and clean, enabling easy registration via plugin XML. It demonstrates a scalable pattern for adding support for new languages or avoiding false matches without modifying core logic.
Implements a custom `CustomHighlighterRenderer` for painting rainbow-colored indent guide lines. It handles soft wraps, folding, and clipping, computing visual positions from offsets. The logic for determining the proper vertical line segment when soft wraps are present is non-trivial and mirrors upstream IntelliJ code with added rainbow coloring. This component is specific to the plugin but demonstrates sophisticated canvas painting that could inform similar custom renderers in other IDE plugins.
The primary HighlightVisitor for rainbow brackets. It filters leaf PSI elements, finds matching BracePair via the language-specific providers, computes the nesting level by traversing parent elements (using a tail-recursive function), and applies the appropriate color. The `iterateBracketParents` function efficiently counts enclosing pairs by checking first/last children of each parent. The use of memoized brace type sets (`BracePairs.braceTypeSet`) avoids repeated lookups. This demonstrates a solid pattern for implementing custom bracket coloring in IntelliJ.