$ show Wilfred/difftastic
Difftastic is a structural diff tool that compares files based on syntax rather than lines. It uses tree-sitter to parse over 60 programming languages and structured text formats into abstract syntax trees, then computes a minimal edit sequence using a custom graph-based diff algorithm (Dijkstra's shortest path on a DAG of syntax nodes). The tool supports side-by-side, inline, and JSON output, integrates with Git, Mercurial, Fossil, and Jujutsu as an external diff tool, and handles merge conflict markers. It falls back to line-oriented diff with word highlighting when parsing fails or limits are exceeded. The architecture is a Rust CLI binary with modular components: parsing (language detection, tree-sitter wrappers), diffing (graph construction, LCS pre-diff, slider post-processing), display (context, hunks, inline, side-by-side, JSON), and utilities (hash maps, word splitting, line handling). Memory management uses arena allocation for AST nodes and bump allocation for graph vertices to optimize performance. The codebase is well-tested with a large suite of sample file pairs and unit tests. Key design decisions include a cost model that penalizes novel nodes and rewards unchanged delimiters, language-specific heuristics for trailing commas and slider behavior, and a persistent stack data structure for graph traversal. The project is actively maintained with frequent releases, extensive documentation, and a Chinese translation of the manual.
The LLM identified 3 files worth highlighting for reuse:
Implements Dijkstra's shortest path algorithm on a directed acyclic graph of syntax tree vertices to compute the optimal edit sequence. Uses a custom cost model (novel nodes cost 300, unchanged delimiters cost 10, unchanged nodes cost 1 + depth/punctuation penalties) and a radix heap priority queue. The algorithm is designed specifically for tree-structured diffs and can be extracted for any similar problem requiring minimal edit distance on hierarchical data.
Configures and caches tree-sitter parsers for 60+ languages, converting tree-sitter ASTs into a generic Syntax representation (lists/atoms) with delimiter tracking, trailing comma handling, and sub-language embedding (e.g., CSS/JS in HTML, shell in Make). The configuration pattern (language-specific atom nodes, delimiter tokens, highlight queries) is reusable for any tool that needs to parse multiple languages with tree-sitter, and the caching mechanism avoids repeated parser initialization.
Provides ANSI color/style application with Unicode-aware width calculation, tab expansion, and word wrapping. Handles CJK, emoji, and combining characters correctly. The `split_string_by_width` function and `color_positions` logic are reusable for terminal output formatting in any Rust CLI tool that needs to display colored, wrapped text with proper alignment.