$ show biomejs/gritql
GritQL is a declarative query language and CLI tool for searching, linting, and transforming source code across many programming languages (JS/TS, Python, Java, Go, Rust, Ruby, PHP, SQL, etc.). The repository implements a Rust workspace comprising a pattern-matching engine (grit-pattern-matcher) that compiles GritQL patterns into executable AST-based matchers, a CLI client (grit) with commands like `apply`, `check`, `list`, `patterns`, and `lsp`, an LSP server for IDE integration, WebAssembly bindings for browser use (playground), and Node.js NAPI bindings for programmatic access. Patterns are authored in `.grit` files or embedded in Markdown with sample-based testing. The system uses tree-sitter parsers (vendored grammars) to parse source files, supports pattern caching for performance, and includes authentication, analytics, and remote module resolution. The architecture is modular: core pattern matching is isolated in `crates/grit-pattern-matcher`, pattern compilation in `crates/core`, language support in `crates/language`, and UX in `crates/cli`. The documentation site (Next.js with Markdoc) provides an interactive playground. External dependencies include tree-sitter parsers, NAPI-RS, WebAssembly tooling, and optional LLM APIs for AI-assisted patterns.
The LLM identified 4 files worth highlighting for reuse:
Parses unified diff output (via `parse_modified_ranges` and `run_git_diff`) into structured `FileDiff` and `RangePair` objects. Handles file creation/deletion, submodule exclusions, and context-agnostic range extraction. This module is a self-contained utility that could be reused in any Rust project needing diff analysis without importing the rest of GritQL.
Implements `ThreadedCache`, a concurrent, lock-free cache for storing file-pattern mismatch results. Uses a dedicated worker thread for background writes and atomics for lock-free reads. The pattern (delegated cache with background flushing) is reusable for any high-read, low-write caching scenario.
Provides `EmbeddedSourceMap` to map positions between an outer document (e.g., Jupyter notebook) and embedded language code blocks, supporting multi-stage edits. The generic offset-based mapping and clone-with-edits pattern can be extracted for any project that needs to track edits in embedded source regions (e.g., SQL in Python, HTML in JS).
Defines the `Messager` trait and several implementations (`JSONLineMessenger`, `TestingMessenger`, `RemoteMessenger`) for structured output of pattern match results, progress, and diagnostics. The trait separates result emission from processing logic and supports progress bars, summary formatting, and interactive prompts. This pattern is reusable for any tool needing pluggable output formats (JSON, human-readable, remote).