Commit effef73
authored
feat(mongodb): replace catch-all rules with explicit method definitions for C3 autocomplete (#57)
* All 6 tasks are complete. Let me provide a summary of what was done in this session:
## Session Summary
This session continued the implementation of the plan to remove hardcoded method lists by leveraging C3 token candidates from the restructured grammar.
### What was done:
**Task 3 — Fix `query_type.go` build failure:**
- Rewrote `classifyMethodChain()` to use the new grammar API: `chain.CollectionExplainMethod()`, `chain.AllCursorMethodCall()`, `chain.CollectionMethodCall()`
- Rewrote `classifyMethodCall()` → `classifyCollectionMethodCall()` to use `mongodb.ICollectionMethodCallContext` instead of the removed `mongodb.IMethodCallContext`
- Removed the `GenericMethod()` check (no longer exists)
- Added classification for the 20 new collection methods (bulkWrite, update, mapReduce, findAndModify, hideIndex, unhideIndex, reIndex, createSearchIndex, etc.)
- Updated the `incomplete_find_statement` test expectation (parser now recovers `find` better → `Select` instead of `DML`)
**Task 3 — Fix `convertCandidates()` token flooding:**
- At `db.|`, C3 tokens leak from the `collectionOperation` path. Fixed by only using C3 tokens when `collectionAccess` is NOT present (i.e., at collection/cursor method positions)
- At `db.|`, returns only hardcoded database methods + collections (unchanged)
- At `db.users.|` and `db.users.find().|`, returns C3 tokens directly
**Task 4 — Regenerate test expectations:**
- Used record mode to regenerate `test_completion.yaml`
- Key behavior change: `db.users.|` now shows 89 methods (both collection + cursor) instead of 34 collection-only. This is correct per the grammar, which allows either type as the first method.
- `db.users.find().|` correctly shows only 35 cursor methods (grammar enforces `cursorMethodCall` after first method)
**Task 5 — Cleanup verification:**
- `isMethodToken()` — still needed for token → function type classification
- `getDatabaseMethodCandidates()` — still needed because database methods from `dbStatement` aren't provided by C3 at `db.|` position
- `previousNonDotToken()` — still needed for `db.` vs `db[` distinction
- All 15 database methods verified as matching `dbStatement` grammar rule
**All tests pass, lint clean.**
* All done. Here's what changed:
**1. Grammar fix (`MongoShellParser.g4`)**: Removed `cursorMethodCall` from the first position in `methodChain`. Now only `collectionMethodCall` is valid as the first method after a collection:
```
methodChain
: DOT collectionExplainMethod DOT collectionMethodCall (DOT cursorMethodCall)*
| DOT collectionMethodCall (DOT cursorMethodCall)*
;
```
There's no valid use case for `db.users.sort()` — cursor methods only make sense after a collection method like `find()`.
**2. Removed `isMethodToken` and `getTokenCandidateType`**: In the `else` branch of `convertCandidates`, we're at a method position where every C3 token is a method. So we treat all tokens as `CandidateTypeFunction` with `()` suffix. This eliminates the ~120-line `isMethodToken` switch block entirely.
This also fixed a bug: `getPlanCache` was previously classified as KEYWORD (no `()`). Now it correctly appears as `getPlanCache()` with FUNCTION type.
**3. Completion behavior now**:
- `db.|` → 15 database methods + collections (hardcoded, unchanged)
- `db.users.|` → only **collection methods** (48 methods — no cursor methods like `allowDiskUse`, `sort`, `limit`)
- `db.users.find().|` → only **cursor methods** (35 methods)
* Done. Here's what changed:
**Removed `genericDbMethod`** — the `identifier LPAREN arguments? RPAREN` catch-all rule from `dbStatement`. Same pattern we already removed for collection methods (`genericMethod`).
**Added 45 new lexer tokens** for the 48 missing db methods (3 already had tokens: `AGGREGATE`, `WATCH`, `SET_WRITE_CONCERN`).
**Added 48 new `dbStatement` alternatives** — each follows the explicit pattern `DB DOT TOKEN LPAREN arguments? RPAREN` with a labeled alternative name.
**Updated `getDatabaseMethodCandidates()`** — expanded from 15 to 63 methods (all db methods now listed alphabetically).
**Updated `identifier` rule** — all 45 new tokens added so they can still be used as identifiers (collection names, keys, etc.).1 parent 91a7b9d commit effef73
File tree
8 files changed
+26860
-13358
lines changed- mongodb
8 files changed
+26860
-13358
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
80 | 99 | | |
81 | 100 | | |
82 | 101 | | |
| |||
91 | 110 | | |
92 | 111 | | |
93 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
94 | 160 | | |
95 | 161 | | |
96 | 162 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
71 | 118 | | |
72 | 119 | | |
73 | 120 | | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | 121 | | |
80 | 122 | | |
81 | 123 | | |
| |||
171 | 213 | | |
172 | 214 | | |
173 | 215 | | |
174 | | - | |
| 216 | + | |
| 217 | + | |
175 | 218 | | |
176 | | - | |
| 219 | + | |
| 220 | + | |
177 | 221 | | |
178 | 222 | | |
179 | | - | |
180 | | - | |
| 223 | + | |
| 224 | + | |
181 | 225 | | |
182 | 226 | | |
183 | 227 | | |
| |||
209 | 253 | | |
210 | 254 | | |
211 | 255 | | |
212 | | - | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
213 | 282 | | |
214 | 283 | | |
215 | 284 | | |
| |||
244 | 313 | | |
245 | 314 | | |
246 | 315 | | |
247 | | - | |
248 | 316 | | |
249 | 317 | | |
250 | 318 | | |
| |||
403 | 471 | | |
404 | 472 | | |
405 | 473 | | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
406 | 579 | | |
407 | 580 | | |
408 | 581 | | |
| |||
555 | 728 | | |
556 | 729 | | |
557 | 730 | | |
558 | | - | |
559 | | - | |
560 | | - | |
561 | | - | |
562 | | - | |
563 | 731 | | |
564 | 732 | | |
565 | 733 | | |
| |||
862 | 1030 | | |
863 | 1031 | | |
864 | 1032 | | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
| 1039 | + | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
865 | 1096 | | |
0 commit comments