|
| 1 | +# Smart Column Expansion and Completion Features |
| 2 | + |
| 3 | +## New Features |
| 4 | + |
| 5 | +### 1. Smart * Expansion (`\sE`) |
| 6 | + |
| 7 | +The `\sE` keybinding now intelligently expands `SELECT *` by **executing the query** to get the actual column schema, instead of relying on static data file hints. |
| 8 | + |
| 9 | +#### What's New |
| 10 | + |
| 11 | +**Before:** |
| 12 | +- Only worked with `-- #! data_file.csv` hints |
| 13 | +- Couldn't handle CTEs, subqueries, or joins |
| 14 | +- Limited to direct table access |
| 15 | + |
| 16 | +**After:** |
| 17 | +- Works with CTEs, subqueries, joins, and complex queries |
| 18 | +- Executes `LIMIT 0` version of your query to get schema |
| 19 | +- Falls back to static schema if execution fails |
| 20 | +- Can be toggled on/off in config |
| 21 | + |
| 22 | +#### Usage |
| 23 | + |
| 24 | +1. Write a query with `SELECT *`: |
| 25 | + ```sql |
| 26 | + WITH sales AS ( |
| 27 | + SELECT * FROM data WHERE amount > 100 |
| 28 | + ) |
| 29 | + SELECT * FROM sales |
| 30 | + ``` |
| 31 | + |
| 32 | +2. Put cursor on the `SELECT *` line you want to expand |
| 33 | + |
| 34 | +3. Press `\sE` (or `:SqlCliExpandStar`) |
| 35 | + |
| 36 | +4. The plugin: |
| 37 | + - Captures the full query context (including the CTE) |
| 38 | + - Executes it with `LIMIT 0` to get column names |
| 39 | + - Replaces `*` with actual columns |
| 40 | + - Adds a column hint comment for completion (see below) |
| 41 | + |
| 42 | +#### Result |
| 43 | + |
| 44 | +```sql |
| 45 | +-- Columns: order_id, customer_id, amount, date, region |
| 46 | + |
| 47 | +WITH sales AS ( |
| 48 | + SELECT * FROM data WHERE amount > 100 |
| 49 | +) |
| 50 | +SELECT |
| 51 | + order_id |
| 52 | + , customer_id |
| 53 | + , amount |
| 54 | + , date |
| 55 | + , region |
| 56 | +FROM sales |
| 57 | +``` |
| 58 | + |
| 59 | +### 2. Auto Column Hint Comments |
| 60 | + |
| 61 | +After expanding `*`, a special comment is automatically inserted at the top of your buffer: |
| 62 | + |
| 63 | +```sql |
| 64 | +-- Columns: order_id, customer_id, amount, date, region |
| 65 | +``` |
| 66 | + |
| 67 | +#### Why This Matters |
| 68 | + |
| 69 | +Neovim's default word completion (`Ctrl+N`) looks for words in the **current buffer**. Since query results are in a different buffer, column names weren't available for completion. |
| 70 | + |
| 71 | +The hint comment solves this by adding column names to your SQL buffer! |
| 72 | + |
| 73 | +#### Usage |
| 74 | + |
| 75 | +1. After expanding *, the hint comment is added automatically |
| 76 | + |
| 77 | +2. Start typing a column name and press `Ctrl+N`: |
| 78 | + ```sql |
| 79 | + SELECT ord<Ctrl+N> |
| 80 | + ``` |
| 81 | + |
| 82 | +3. Neovim suggests: `order_id` (from the hint comment!) |
| 83 | + |
| 84 | +#### Configuration |
| 85 | + |
| 86 | +```lua |
| 87 | +require('sql-cli').setup({ |
| 88 | + smart_expansion = { |
| 89 | + enabled = true, -- Enable smart expansion |
| 90 | + auto_insert_column_hints = true, -- Auto-add hint comments |
| 91 | + auto_sync_column_hints = true, -- Sync after query execution |
| 92 | + fallback_to_static = true, -- Fall back if query fails |
| 93 | + } |
| 94 | +}) |
| 95 | +``` |
| 96 | + |
| 97 | +### 3. Column Hint Syncing |
| 98 | + |
| 99 | +When you execute a query and get results, the plugin can automatically update the column hint comment with the result columns. |
| 100 | + |
| 101 | +This keeps your completion suggestions in sync with your actual query results! |
| 102 | + |
| 103 | +#### Configuration |
| 104 | + |
| 105 | +Set `auto_sync_column_hints = true` in config (enabled by default). |
| 106 | + |
| 107 | +### 4. Distinct Values Display (`\srD`) |
| 108 | + |
| 109 | +This feature already existed but may not have been easily discoverable. |
| 110 | + |
| 111 | +#### Usage |
| 112 | + |
| 113 | +1. Put cursor on a column name in your query |
| 114 | +2. Press `\srD` |
| 115 | +3. A floating window shows distinct values and their counts |
| 116 | + |
| 117 | +#### Example |
| 118 | + |
| 119 | +Cursor on `region`: |
| 120 | + |
| 121 | +``` |
| 122 | +╔═══ Distinct values for 'region' ═══╗ |
| 123 | +║ Total distinct values: 4 ║ |
| 124 | +║ ║ |
| 125 | +║ Value Count ║ |
| 126 | +║ ───────────────────────── ║ |
| 127 | +║ East 1,234 ║ |
| 128 | +║ West 1,156 ║ |
| 129 | +║ North 892 ║ |
| 130 | +║ South 743 ║ |
| 131 | +╚═════════════════════════════════════╝ |
| 132 | +``` |
| 133 | + |
| 134 | +## Configuration |
| 135 | + |
| 136 | +### Full Config Example |
| 137 | + |
| 138 | +```lua |
| 139 | +require('sql-cli').setup({ |
| 140 | + -- Smart expansion features |
| 141 | + smart_expansion = { |
| 142 | + enabled = true, -- Use smart expansion (query execution) |
| 143 | + auto_insert_column_hints = true, -- Add column hints for completion |
| 144 | + auto_sync_column_hints = true, -- Sync hints after query execution |
| 145 | + fallback_to_static = true, -- Fall back to --schema-json if query fails |
| 146 | + }, |
| 147 | + |
| 148 | + -- Keymaps |
| 149 | + keymaps = { |
| 150 | + expand_star = '<leader>sE', -- Smart * expansion |
| 151 | + -- ... other keymaps |
| 152 | + } |
| 153 | +}) |
| 154 | +``` |
| 155 | + |
| 156 | +### Disable Smart Expansion |
| 157 | + |
| 158 | +To use the old static expansion: |
| 159 | + |
| 160 | +```lua |
| 161 | +smart_expansion = { |
| 162 | + enabled = false, -- Use old expand_star_columns behavior |
| 163 | +} |
| 164 | +``` |
| 165 | + |
| 166 | +### Disable Column Hints |
| 167 | + |
| 168 | +To expand * without adding hint comments: |
| 169 | + |
| 170 | +```lua |
| 171 | +smart_expansion = { |
| 172 | + enabled = true, |
| 173 | + auto_insert_column_hints = false, -- Don't add hints |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +## Keybindings Summary |
| 178 | + |
| 179 | +| Key | Description | |
| 180 | +|-----------|--------------------------------------| |
| 181 | +| `\sE` | Smart SELECT * expansion | |
| 182 | +| `\srD` | Show distinct values for column | |
| 183 | +| `Ctrl+N` | Autocomplete (use with column hints) | |
| 184 | + |
| 185 | +## Technical Details |
| 186 | + |
| 187 | +### How Smart Expansion Works |
| 188 | + |
| 189 | +1. **Query Capture**: Finds the complete query context (including CTEs, subqueries) |
| 190 | +2. **Preview Execution**: Runs query with `LIMIT 0` to get schema |
| 191 | +3. **Column Extraction**: Parses JSON result to get column names |
| 192 | +4. **Replacement**: Replaces `*` with formatted column list |
| 193 | +5. **Hint Addition**: Adds comment for completion (if enabled) |
| 194 | + |
| 195 | +### Query Context Detection |
| 196 | + |
| 197 | +The plugin looks backward/forward to find query boundaries: |
| 198 | +- Stops at `GO` statements |
| 199 | +- Captures `WITH` clauses at the start |
| 200 | +- Includes everything up to the next `GO` |
| 201 | + |
| 202 | +This means it correctly handles: |
| 203 | +- Multiple CTEs |
| 204 | +- Nested subqueries |
| 205 | +- JOIN operations |
| 206 | +- Complex WHERE clauses |
| 207 | + |
| 208 | +### Fallback Behavior |
| 209 | + |
| 210 | +If smart expansion fails (query error, syntax issue): |
| 211 | +1. Shows error message |
| 212 | +2. Falls back to static `--schema-json` if `fallback_to_static = true` |
| 213 | +3. Uses data file hint or opened CSV as before |
| 214 | + |
| 215 | +## Testing |
| 216 | + |
| 217 | +See `test_smart_expansion.sql` for examples: |
| 218 | + |
| 219 | +```bash |
| 220 | +# Open in neovim |
| 221 | +nvim test_smart_expansion.sql |
| 222 | + |
| 223 | +# Try \sE on each SELECT * line |
| 224 | +# Try \srD on column names |
| 225 | +# Try Ctrl+N after expansion to test completion |
| 226 | +``` |
| 227 | + |
| 228 | +## Troubleshooting |
| 229 | + |
| 230 | +### "Could not determine query context" |
| 231 | + |
| 232 | +- Make sure your cursor is on or near a `SELECT *` line |
| 233 | +- Check that the query is properly formatted (not in middle of CTE) |
| 234 | + |
| 235 | +### "Query execution failed" |
| 236 | + |
| 237 | +- Query may have syntax errors |
| 238 | +- Data file might not be set (use `:SqlCliSetData`) |
| 239 | +- Try the query manually first with `:SqlCliExecute` |
| 240 | + |
| 241 | +### Column hints not appearing |
| 242 | + |
| 243 | +- Check `smart_expansion.auto_insert_column_hints = true` |
| 244 | +- Verify expansion actually ran (check for notification) |
| 245 | +- Look for `-- Columns:` comment at top of buffer |
| 246 | + |
| 247 | +### Completion not working |
| 248 | + |
| 249 | +- Press `Ctrl+N` after typing partial column name |
| 250 | +- Make sure column hint comment exists in buffer |
| 251 | +- Neovim's completion looks at **current buffer** only |
| 252 | + |
| 253 | +## Future Enhancements |
| 254 | + |
| 255 | +Possible improvements: |
| 256 | +- LSP integration for true cross-buffer completion |
| 257 | +- Column hints from results buffer without expansion |
| 258 | +- Intelligent column ordering (frequently used first) |
| 259 | +- Type hints in completion (String, Integer, etc.) |
| 260 | +- Schema caching to avoid repeated executions |
| 261 | + |
| 262 | +## Related Documentation |
| 263 | + |
| 264 | +- Main plugin README: `README.md` |
| 265 | +- Keybindings: `KEYBINDINGS.md` |
| 266 | +- Design doc: `../docs/NVIM_SMART_COLUMN_COMPLETION.md` |
0 commit comments