|
| 1 | +# PostgreSQL Completion Scope Implementation Plan |
| 2 | + |
| 3 | +> **For Claude:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task. |
| 4 | +
|
| 5 | +**Goal:** Add a PostgreSQL completion-only API that returns parser candidates plus the visible FROM scope at the cursor. |
| 6 | + |
| 7 | +**Architecture:** Keep ordinary `Parse` strict and avoid exposing partial ASTs. Add `parser.CollectCompletion(sql, cursorOffset)` as a completion context API that wraps existing `Collect` candidates and computes a best-effort `ScopeSnapshot` from grammar/token facts and parseable prefixes. ByteBase remains responsible for metadata and final candidate formatting. |
| 8 | + |
| 9 | +**Tech Stack:** Go, `github.com/bytebase/omni/pg/parser`, `github.com/bytebase/omni/pg/ast`. |
| 10 | + |
| 11 | +### Task 1: Scope API Tests |
| 12 | + |
| 13 | +**Files:** |
| 14 | +- Create: `pg/parser/complete_context_test.go` |
| 15 | + |
| 16 | +**Steps:** |
| 17 | +- Write failing tests for `CollectCompletion`. |
| 18 | +- Cover incomplete `JOIN ... USING (`, aliases with alias columns, incomplete `JOIN`, subquery aliases, and CTE references. |
| 19 | +- Run `go test ./pg/parser -run 'TestCollectCompletion' -count=1`; expected failure is missing API/types. |
| 20 | + |
| 21 | +### Task 2: Minimal API and Scope Extraction |
| 22 | + |
| 23 | +**Files:** |
| 24 | +- Create: `pg/parser/complete_context.go` |
| 25 | + |
| 26 | +**Steps:** |
| 27 | +- Define `CompletionContext`, `ScopeSnapshot`, `RangeReference`, and `RangeReferenceKind`. |
| 28 | +- Implement `CollectCompletion` by reusing `Collect` and extracting visible references for the cursor's SELECT scope. |
| 29 | +- Preserve strict `Parse`; completion-only recovery may tolerate incomplete join qualifiers. |
| 30 | +- Run focused tests until green. |
| 31 | + |
| 32 | +### Task 3: Public Package Wrapper |
| 33 | + |
| 34 | +**Files:** |
| 35 | +- Create: `pg/completion.go` |
| 36 | + |
| 37 | +**Steps:** |
| 38 | +- Add a small `pg.CollectCompletion` wrapper if downstream users prefer the top-level pg package. |
| 39 | +- Keep the parser package API as the primary implementation. |
| 40 | +- Run `go test ./pg ./pg/parser -run 'TestCollectCompletion|TestParse' -count=1`. |
| 41 | + |
| 42 | +### Task 4: Verification |
| 43 | + |
| 44 | +**Steps:** |
| 45 | +- Run `go test ./pg/parser ./pg/completion`. |
| 46 | +- Confirm ordinary strict parse behavior remains covered by existing parser tests. |
0 commit comments