March 2, 2026 1 min read
Building Context-Aware SQL Autocompletion with CodeMirror 6
Build Log
Featured
Depth: ●●●○○
How I built a SQL autocompletion engine that understands cursor position: FK-based JOIN conditions after ON, aggregate functions in HAVING, and schema-aware columns after SELECT.
Three-Layer Architecture
Layer 1: Context Detection parses SQL around cursor to determine clause. Layer 2: 14 Clause-Specific Handlers know what's valid (joinHandler suggests FK-based ON conditions, havingHandler suggests aggregates). Layer 3: 12 Suggestion Generators build CodeMirror completion items.
The Killer Feature
After JOIN, the system looks up foreign key relationships and suggests related tables with pre-filled ON conditions. No other SQL editor does this automatically for arbitrary databases.