Skip to main content

dsl_completion/
phase.rs

1//! Phase-aware completion context.
2//!
3//! Walks the tokens of the current statement (text from the previous `;`
4//! or buffer start, up to the cursor) through a small state machine.
5//! The resulting [`Phase`] is what completion expects *next* at the
6//! cursor. This is what lets us avoid suggesting `SELECT` in the middle
7//! of a statement, or suggesting columns where only a table makes sense.
8//!
9//! The detector is token-based; it ignores comments and strings.
10
11use text_size::TextSize;
12
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum Phase {
15  /// Empty buffer or right after a `;`. Top-level statement keywords.
16  Start,
17
18  /// After `SELECT`. Expect projection items (columns / `*` / funcs /
19  /// `DISTINCT` / `ALL`).
20  SelectProjection,
21  /// Mid-projection identifier (after a column name). Expect `AS`,
22  /// comma to add another column, or `FROM`.
23  InProjection,
24  /// Right after a comma in the projection list. Expect another column.
25  NextProjection,
26  /// Right after `*` (whole row). Expect `FROM` or a comma to mix in
27  /// more projections like `SELECT *, foo FROM ...`.
28  AfterStar,
29  /// Right after `AS` inside the projection list. Expect alias name.
30  ProjectionAlias,
31
32  /// After `FROM` / a comma inside FROM list / `JOIN`. Expect a table.
33  ExpectTable,
34  /// After a table name (possibly aliased). Expect comma / JOIN family
35  /// / WHERE / GROUP BY / HAVING / ORDER BY / LIMIT / OFFSET / `;`.
36  AfterTable,
37  /// After `INNER` / `LEFT` / `RIGHT` / `FULL` / `CROSS`. Expect JOIN
38  /// or OUTER.
39  JoinModifier,
40  /// After a join completed but before ON / USING.
41  JoinComplete,
42  /// After `ON`. Expect a predicate.
43  OnClause,
44  /// After `USING`. Expect `(`.
45  UsingClause,
46
47  /// After `WHERE`. Expect a predicate.
48  WhereClause,
49  /// Mid-predicate. Expect AND / OR / ORDER BY / GROUP BY / ...
50  InPredicate,
51
52  /// After `GROUP` (expect `BY`).
53  AfterGroup,
54  /// After `GROUP BY`. Expect columns / numbers / GROUPING SETS / ROLLUP.
55  GroupByList,
56  /// After `ORDER` (expect `BY`).
57  AfterOrder,
58  /// After `ORDER BY`. Expect column [ASC|DESC] [NULLS FIRST|LAST].
59  OrderByList,
60  /// After `HAVING`. Expect a predicate over aggregates.
61  HavingClause,
62  /// After `LIMIT`. Expect a number.
63  LimitClause,
64  /// After `OFFSET`. Expect a number.
65  OffsetClause,
66
67  // ----- DML -----
68  AfterInsert,
69  AfterInsertTable,
70  InsertColumnList,
71  InsertExpectValues,
72  InsertValuesList,
73  AfterUpdate,
74  AfterUpdateTable,
75  UpdateAssignment,
76  AfterDelete,
77  /// After `RETURNING` in INSERT / UPDATE / DELETE. Expect columns of
78  /// the target table (the one named in INTO / UPDATE / DELETE FROM),
79  /// plus `*`. Acts like a SELECT projection scoped to that one table.
80  ReturningClause,
81
82  // ----- DDL -----
83  AfterCreate,
84  AfterAlter,
85  AfterDrop,
86
87  /// After `ALTER TABLE [IF EXISTS] [ONLY]` -- expect a table name.
88  /// Engine surfaces tables here.
89  AfterAlterTableExpectName,
90  /// After `ALTER TABLE <name>` -- expect a sub-action keyword
91  /// (ADD COLUMN, DROP COLUMN, RENAME, ALTER COLUMN, ...). Engine
92  /// emits the curated snippet list.
93  AfterAlterTableName,
94
95  /// After `GRANT` / `REVOKE` -- expect a privilege keyword
96  /// (SELECT / INSERT / UPDATE / DELETE / TRUNCATE / REFERENCES /
97  /// TRIGGER / USAGE / EXECUTE / CREATE / CONNECT / TEMPORARY) or
98  /// `ALL [PRIVILEGES]`. Engine emits the curated keyword set.
99  AfterGrantOrRevoke,
100  /// After `GRANT/REVOKE ... ON [TABLE|SEQUENCE|FUNCTION|SCHEMA|...]`
101  /// -- expect a target name. Engine emits tables/sequences/funcs.
102  AfterGrantOn,
103  /// After `GRANT ... TO` or `REVOKE ... FROM` -- expect a role
104  /// from the catalog, plus the `PUBLIC` pseudo-role.
105  AfterGrantTo,
106
107  /// Cursor sits inside a `$$ ... $$` body (DO block or function body).
108  /// PL/pgSQL keywords + functions + columns make sense here.
109  PlpgsqlBody,
110  /// Right-hand side of a PL/pgSQL assignment (`v_x := ...` or
111  /// `NEW.col := ...`). Statement-level keywords like SELECT / CREATE
112  /// are invalid here -- only expressions are.
113  PlpgsqlAssignRhs,
114
115  // ----- CREATE TABLE sub-phases -----
116  /// After `CREATE TABLE [IF NOT EXISTS]` -- expect a fresh table name.
117  /// No completion offered (user invents the name).
118  CtlExpectTableName,
119  /// Start of an entry inside CREATE TABLE body (after `(` or `,`).
120  /// User could either type a column name or a constraint-line keyword
121  /// (CONSTRAINT / PRIMARY KEY / FOREIGN KEY / UNIQUE / CHECK).
122  CtlBodyStart,
123  /// After a column name -- expect a type.
124  CtlExpectType,
125  /// After a column type -- expect column constraint keywords
126  /// (NOT NULL, DEFAULT, PRIMARY KEY, REFERENCES, UNIQUE, CHECK, ...).
127  CtlExpectColumnConstraint,
128  /// After `CONSTRAINT` -- expect a fresh constraint name. No completion.
129  CtlExpectConstraintName,
130  /// After `CONSTRAINT <name>` -- expect a kind keyword (PRIMARY KEY,
131  /// FOREIGN KEY, UNIQUE, CHECK).
132  CtlExpectConstraintKind,
133  /// After `REFERENCES` -- expect a target table.
134  CtlExpectFkTable {/* nothing for now */},
135  /// After `REFERENCES tbl (` -- expect a column of that table.
136  CtlExpectFkColumn {
137    table: String,
138  },
139  /// Inside a `CHECK ( ... )` expression. Expect this table's
140  /// columns plus the full PG function library and expression
141  /// keywords -- CHECK bodies are arbitrary boolean expressions.
142  CtlCheckExpr {
143    table: Option<String>,
144  },
145
146  /// Right after the PG `::` cast operator. Expect a type (DATE,
147  /// NUMERIC, TEXT, JSONB, custom enum/domain, ...).
148  CastType,
149
150  /// Anything we couldn't classify -- the engine falls back to a broad
151  /// emitter so the menu is never empty.
152  Unknown,
153}
154
155#[derive(Debug, Clone, PartialEq, Eq)]
156enum Tok<'a> {
157  Word(&'a str), // identifier or keyword; uppercase via .to_ascii_uppercase()
158  Punct(char),   // single-char punctuation: , ; ( ) * . = < > ! +
159  StringLit,
160  NumberLit,
161}
162
163/// Tokenise from `start` (inclusive) to `end_exclusive` (exclusive),
164/// skipping whitespace, line comments (`-- ...`), block comments
165/// (`/* ... */`), single-quoted strings (with `\` escapes), double-quoted
166/// identifiers, and dollar-quoted blocks.
167fn tokenise(src: &str, start: usize, end: usize) -> Vec<Tok<'_>> {
168  let bytes = src.as_bytes();
169  let mut out = Vec::new();
170  let mut i = start;
171  while i < end {
172    // Skip past any non-ASCII bytes -- the state machine only
173    // recognises ASCII keywords + punctuation, and slicing into a
174    // multi-byte char would panic.
175    if !bytes[i].is_ascii() {
176      i += 1;
177      continue;
178    }
179    let c = bytes[i] as char;
180    if c.is_whitespace() {
181      i += 1;
182      continue;
183    }
184    if c == '-' && i + 1 < end && bytes[i + 1] == b'-' {
185      // Line comment
186      while i < end && bytes[i] != b'\n' {
187        i += 1;
188      }
189      continue;
190    }
191    if c == '/' && i + 1 < end && bytes[i + 1] == b'*' {
192      i += 2;
193      while i + 1 < end && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
194        i += 1;
195      }
196      i = (i + 2).min(end);
197      continue;
198    }
199    if c == '\'' {
200      i += 1;
201      while i < end {
202        let b = bytes[i];
203        if b == b'\'' && (i == 0 || bytes[i - 1] != b'\\') {
204          i += 1;
205          break;
206        }
207        i += 1;
208      }
209      out.push(Tok::StringLit);
210      continue;
211    }
212    if c == '"' {
213      // Quoted identifier; treat as a Word.
214      let id_start = i + 1;
215      i += 1;
216      while i < end && bytes[i] != b'"' {
217        i += 1;
218      }
219      let id_end = i;
220      i = (i + 1).min(end);
221      if id_end > id_start {
222        out.push(Tok::Word(&src[id_start..id_end]));
223      }
224      continue;
225    }
226    if c == '$' {
227      // Dollar-quoted block (tag may be empty).
228      if let Some(tag_end_rel) = src[i + 1..end].find('$') {
229        let tag = &src[i + 1..i + 1 + tag_end_rel];
230        if tag.chars().all(|ch| ch.is_alphanumeric() || ch == '_') {
231          let closer = format!("${tag}$");
232          let body_start = i + 1 + tag_end_rel + 1;
233          if let Some(rel) = src[body_start..end].find(&closer) {
234            i = body_start + rel + closer.len();
235          } else {
236            i = end;
237          }
238          out.push(Tok::StringLit);
239          continue;
240        }
241      }
242    }
243    if c.is_ascii_digit() {
244      let s = i;
245      while i < end && (bytes[i].is_ascii_digit() || bytes[i] == b'.') {
246        i += 1;
247      }
248      let _ = s;
249      out.push(Tok::NumberLit);
250      continue;
251    }
252    if is_word_start(c) {
253      let s = i;
254      while i < end {
255        // Halt on non-ASCII so the resulting slice never
256        // straddles a UTF-8 char boundary.
257        if !bytes[i].is_ascii() {
258          break;
259        }
260        if !is_word_cont(bytes[i] as char) {
261          break;
262        }
263        i += 1;
264      }
265      out.push(Tok::Word(&src[s..i]));
266      continue;
267    }
268    out.push(Tok::Punct(c));
269    i += 1;
270  }
271  out
272}
273
274fn is_word_start(c: char) -> bool {
275  c.is_alphabetic() || c == '_'
276}
277fn is_word_cont(c: char) -> bool {
278  c.is_alphanumeric() || c == '_'
279}
280
281/// True when the cursor sits immediately after `::` (possibly with a
282/// partial type-name word in between). Skips whitespace between the
283/// colons and the word so `now() ::` and `now()::DAT` both qualify.
284/// True when the cursor sits in the role-name slot of an
285/// `ALTER ... OWNER TO ` clause. Walks back from `pos` over any
286/// in-progress ASCII identifier characters and whitespace, then
287/// verifies the preceding tokens are `OWNER TO`. Bails out on any
288/// non-ASCII byte (we don't expect role names to be non-ASCII; the
289/// preceding `OWNER TO` keywords are pure ASCII).
290fn after_owner_to(src: &str, pos: usize) -> bool {
291  let bytes = src.as_bytes();
292  let mut i = pos.min(bytes.len());
293  while i > 0 && bytes[i - 1].is_ascii() && is_word_cont(bytes[i - 1] as char) {
294    i -= 1;
295  }
296  while i > 0 && bytes[i - 1].is_ascii() && bytes[i - 1].is_ascii_whitespace() {
297    i -= 1;
298  }
299  if i < 2 || !src.is_char_boundary(i) {
300    return false;
301  }
302  if !src[..i].to_ascii_uppercase().ends_with("TO") {
303    return false;
304  }
305  let pre_end = i - 2;
306  if !src.is_char_boundary(pre_end) {
307    return false;
308  }
309  let pre = src[..pre_end].trim_end();
310  pre.to_ascii_uppercase().ends_with("OWNER")
311}
312
313/// True when the cursor sits in the role-name slot of `SET ROLE`.
314fn after_set_role(src: &str, pos: usize) -> bool {
315  let bytes = src.as_bytes();
316  let mut i = pos.min(bytes.len());
317  while i > 0 && bytes[i - 1].is_ascii() && is_word_cont(bytes[i - 1] as char) {
318    i -= 1;
319  }
320  while i > 0 && bytes[i - 1].is_ascii() && bytes[i - 1].is_ascii_whitespace() {
321    i -= 1;
322  }
323  if i < 4 || !src.is_char_boundary(i) {
324    return false;
325  }
326  if !src[..i].to_ascii_uppercase().ends_with("ROLE") {
327    return false;
328  }
329  let pre_end = i - 4;
330  if !src.is_char_boundary(pre_end) {
331    return false;
332  }
333  let pre = src[..pre_end].trim_end();
334  pre.to_ascii_uppercase().ends_with("SET")
335}
336
337fn after_double_colon(src: &str, pos: usize) -> bool {
338  let bytes = src.as_bytes();
339  let mut i = pos;
340  // Skip the partial type identifier the user is typing.
341  while i > 0 && is_word_cont(bytes[i - 1] as char) {
342    i -= 1;
343  }
344  // Skip any whitespace between `::` and the partial word.
345  while i > 0 && bytes[i - 1].is_ascii_whitespace() {
346    i -= 1;
347  }
348  i >= 2 && bytes[i - 1] == b':' && bytes[i - 2] == b':'
349}
350
351/// Find the byte offset of the last unquoted `;` before `pos`. Returns
352/// 0 when none. This is where the current statement starts.
353/// When the cursor at `pos` sits inside an unclosed parenthesized
354/// subquery body (the body starts with SELECT / INSERT / UPDATE /
355/// DELETE), return the byte offset immediately after the opening
356/// `(`. The innermost unclosed subquery wins so nested cases like
357/// `SELECT * FROM (SELECT * FROM (SELECT |))` route to the deepest
358/// SELECT. Strings and double-quoted identifiers are skipped.
359fn subquery_body_start(src: &str, start: usize, pos: usize) -> Option<usize> {
360  let bytes = src.as_bytes();
361  let upper = src.to_ascii_uppercase();
362  let upper_bytes = upper.as_bytes();
363  let end = pos.min(bytes.len());
364  let mut in_single = false;
365  let mut in_double = false;
366  let mut stack: Vec<usize> = Vec::new(); // bodies (after-paren positions) of SELECT-like subqueries
367  let mut depth: i32 = 0; // running paren depth
368  let mut subq_depths: Vec<i32> = Vec::new(); // matching depth for each stack entry
369  let mut i = start;
370  while i < end {
371    let c = bytes[i] as char;
372    if !in_double && c == '\'' && (i == 0 || bytes[i - 1] != b'\\') {
373      in_single = !in_single;
374      i += 1;
375      continue;
376    }
377    if !in_single && c == '"' {
378      in_double = !in_double;
379      i += 1;
380      continue;
381    }
382    if in_single || in_double {
383      i += 1;
384      continue;
385    }
386    if c == '(' {
387      depth += 1;
388      // Peek for SELECT / INSERT / UPDATE / DELETE after optional ws.
389      let mut k = i + 1;
390      while k < end && bytes[k].is_ascii_whitespace() {
391        k += 1;
392      }
393      let starts_subquery = ["SELECT", "INSERT", "UPDATE", "DELETE", "WITH", "VALUES"].iter().any(|kw| {
394        k + kw.len() <= upper_bytes.len()
395          && &upper_bytes[k..k + kw.len()] == kw.as_bytes()
396          && (k + kw.len() == upper_bytes.len() || !is_word_cont(upper_bytes[k + kw.len()] as char))
397      });
398      if starts_subquery {
399        stack.push(i + 1);
400        subq_depths.push(depth);
401      }
402    } else if c == ')' {
403      if let Some(&d) = subq_depths.last()
404        && d == depth
405      {
406        stack.pop();
407        subq_depths.pop();
408      }
409      depth -= 1;
410    }
411    i += 1;
412  }
413  stack.last().copied()
414}
415
416/// When the cursor sits inside a `CREATE [OR REPLACE] [TEMP|TEMPORARY]
417/// [RECURSIVE] [MATERIALIZED] VIEW <name> AS <body>` statement,
418/// return the offset just after the ` AS ` so the walker treats the
419/// body as a fresh SELECT/WITH. Returns None when the cursor is
420/// before the AS or the prefix doesn't match.
421fn create_view_body_start(src: &str, start: usize, pos: usize) -> Option<usize> {
422  let end = pos.min(src.len());
423  if start >= end {
424    return None;
425  }
426  let upper = src[start..end].to_ascii_uppercase();
427  // Quick reject -- avoid the expensive scan when there's no CREATE.
428  if !upper.contains("CREATE") || !upper.contains("VIEW") {
429    return None;
430  }
431  let bytes = upper.as_bytes();
432  let n = bytes.len();
433  // Skip leading whitespace.
434  let mut i = 0usize;
435  while i < n && bytes[i].is_ascii_whitespace() {
436    i += 1;
437  }
438  // Must begin with CREATE (word-bounded).
439  if !match_word(bytes, i, b"CREATE") {
440    return None;
441  }
442  i += 6;
443  // Consume optional `OR REPLACE`, `TEMP|TEMPORARY`, `RECURSIVE`,
444  // `MATERIALIZED` modifiers in any plausible order before VIEW.
445  loop {
446    while i < n && bytes[i].is_ascii_whitespace() {
447      i += 1;
448    }
449    if match_word(bytes, i, b"OR") {
450      i += 2;
451      while i < n && bytes[i].is_ascii_whitespace() {
452        i += 1;
453      }
454      if !match_word(bytes, i, b"REPLACE") {
455        return None;
456      }
457      i += 7;
458      continue;
459    }
460    if match_word(bytes, i, b"TEMP") {
461      i += 4;
462      continue;
463    }
464    if match_word(bytes, i, b"TEMPORARY") {
465      i += 9;
466      continue;
467    }
468    if match_word(bytes, i, b"RECURSIVE") {
469      i += 9;
470      continue;
471    }
472    if match_word(bytes, i, b"MATERIALIZED") {
473      i += 12;
474      continue;
475    }
476    break;
477  }
478  while i < n && bytes[i].is_ascii_whitespace() {
479    i += 1;
480  }
481  // Must reach VIEW.
482  if !match_word(bytes, i, b"VIEW") {
483    return None;
484  }
485  i += 4;
486  while i < n && bytes[i].is_ascii_whitespace() {
487    i += 1;
488  }
489  // Optional IF NOT EXISTS.
490  if match_word(bytes, i, b"IF") {
491    i += 2;
492    while i < n && bytes[i].is_ascii_whitespace() {
493      i += 1;
494    }
495    if !match_word(bytes, i, b"NOT") {
496      return None;
497    }
498    i += 3;
499    while i < n && bytes[i].is_ascii_whitespace() {
500      i += 1;
501    }
502    if !match_word(bytes, i, b"EXISTS") {
503      return None;
504    }
505    i += 6;
506    while i < n && bytes[i].is_ascii_whitespace() {
507      i += 1;
508    }
509  }
510  // View name -- read until whitespace / `(` / end.
511  let name_start = i;
512  while i < n && !bytes[i].is_ascii_whitespace() && bytes[i] != b'(' {
513    i += 1;
514  }
515  if i == name_start {
516    return None;
517  }
518  while i < n && bytes[i].is_ascii_whitespace() {
519    i += 1;
520  }
521  // Optional `(col1, col2, ...)` column-alias list.
522  if i < n && bytes[i] == b'(' {
523    let mut depth = 1i32;
524    i += 1;
525    while i < n && depth > 0 {
526      match bytes[i] {
527        b'(' => depth += 1,
528        b')' => depth -= 1,
529        _ => {},
530      }
531      i += 1;
532    }
533    while i < n && bytes[i].is_ascii_whitespace() {
534      i += 1;
535    }
536  }
537  // Optional WITH (...) options.
538  if match_word(bytes, i, b"WITH") {
539    let after = i + 4;
540    let mut k = after;
541    while k < n && bytes[k].is_ascii_whitespace() {
542      k += 1;
543    }
544    if k < n && bytes[k] == b'(' {
545      let mut depth = 1i32;
546      i = k + 1;
547      while i < n && depth > 0 {
548        match bytes[i] {
549          b'(' => depth += 1,
550          b')' => depth -= 1,
551          _ => {},
552        }
553        i += 1;
554      }
555      while i < n && bytes[i].is_ascii_whitespace() {
556        i += 1;
557      }
558    }
559  }
560  // Now must see AS.
561  if !match_word(bytes, i, b"AS") {
562    return None;
563  }
564  let after_as = i + 2;
565  if after_as > pos.saturating_sub(start) {
566    return None;
567  }
568  Some(start + after_as)
569}
570
571fn match_word(bytes: &[u8], i: usize, word: &[u8]) -> bool {
572  let n = bytes.len();
573  if i + word.len() > n {
574    return false;
575  }
576  if &bytes[i..i + word.len()] != word {
577    return false;
578  }
579  if i + word.len() < n && is_word_cont(bytes[i + word.len()] as char) {
580    return false;
581  }
582  if i > 0 && is_word_cont(bytes[i - 1] as char) {
583    return false;
584  }
585  true
586}
587
588fn statement_start(src: &str, pos: usize) -> usize {
589  let bytes = src.as_bytes();
590  let mut in_single = false;
591  let mut in_double = false;
592  let mut last_semi = 0;
593  let mut i = 0;
594  while i < pos.min(bytes.len()) {
595    let c = bytes[i] as char;
596    if !in_double && c == '\'' && (i == 0 || bytes[i - 1] != b'\\') {
597      in_single = !in_single;
598    } else if !in_single && c == '"' {
599      in_double = !in_double;
600    } else if !in_single && !in_double && c == ';' {
601      last_semi = i + 1;
602    }
603    i += 1;
604  }
605  last_semi
606}
607
608/// Walk the tokens to derive the current phase.
609pub fn detect(src: &str, offset: TextSize) -> Phase {
610  let pos: usize = offset.into();
611  let pos = pos.min(src.len());
612
613  // PG cast operator `::` -- when the immediately-preceding non-word
614  // chars are `::`, the cursor is in type position. Works even when
615  // the user has started typing a partial type name after the colons.
616  if after_double_colon(src, pos) {
617    return Phase::CastType;
618  }
619
620  // `OWNER TO` slot: ALTER TABLE/SCHEMA/SEQUENCE/FUNCTION/DATABASE/
621  // TYPE/INDEX/VIEW/MATERIALIZED VIEW/DOMAIN/COLLATION ... OWNER TO
622  // <cursor>. Routes to the role-list slot used by GRANT TO.
623  if after_owner_to(src, pos) {
624    return Phase::AfterGrantTo;
625  }
626
627  // `SET ROLE <cursor>` and `RESET ROLE` -- session role switch.
628  // SET ROLE expects a role; RESET ROLE has no completion target
629  // (handled by Phase::Unknown fallback).
630  if after_set_role(src, pos) {
631    return Phase::AfterGrantTo;
632  }
633
634  // Inside an unclosed dollar-quoted block: cursor is in a PL/pgSQL
635  // or SQL function body. Route to body-style completion.
636  if inside_dollar_quoted(src, pos) {
637    // Narrow to expression-only when the cursor sits after a `:=`
638    // (or `=` inside an UPDATE SET clause) on the same statement.
639    if after_assignment(src, pos) {
640      return Phase::PlpgsqlAssignRhs;
641    }
642    return Phase::PlpgsqlBody;
643  }
644
645  let raw_start = statement_start(src, pos);
646  // If the cursor sits inside an unclosed `(SELECT ...` /
647  // `(INSERT ...)` / `(UPDATE ...)` / `(DELETE ...)` -- typical CTE
648  // body, subquery in FROM, or scalar subquery -- pretend the
649  // statement starts at the subquery body. Otherwise the outer
650  // `WITH t AS (` / `SELECT * FROM (` prefix collapses the walker to
651  // Phase::Start and the user sees the whole DDL menu.
652  //
653  // CREATE VIEW / MATERIALIZED VIEW share the problem at top level
654  // (no parens): the body after `AS` is a SELECT but the walker
655  // doesn't recognize the CREATE VIEW prefix. Anchor at the body
656  // start so it sees a fresh SELECT statement.
657  let start = subquery_body_start(src, raw_start, pos)
658    .or_else(|| create_view_body_start(src, raw_start, pos))
659    .unwrap_or(raw_start);
660  let toks = tokenise(src, start, pos);
661
662  // Strip the trailing partial identifier the user is typing right now
663  // -- it's not a "completed" token from the state-machine's point of
664  // view. We keep it though if the previous char wasn't a word char
665  // (e.g. after `,` or whitespace before nothing).
666  // Easier rule: if the cursor sits right after a word char, drop the
667  // last Tok::Word; it's the in-progress token.
668  let mut effective = toks;
669  if pos > 0 && is_word_cont(src.as_bytes()[pos - 1] as char) && matches!(effective.last(), Some(Tok::Word(_))) {
670    effective.pop();
671  }
672
673  // Skip past a leading WITH ... CTE-list so the walker sees the inner
674  // statement (SELECT/INSERT/UPDATE/DELETE) as if it started fresh.
675  // Tracks paren depth and resumes at the first top-level statement
676  // keyword after the last `)` of the CTE definitions. Without this,
677  // the CTE name (an unknown Word at Start) collapses the walker into
678  // Phase::Unknown and the menu degrades to a 600-item dump.
679  let trimmed = strip_with_prefix(&effective);
680  walk(trimmed)
681}
682
683/// Return the suffix of `toks` after a leading `WITH [RECURSIVE] <cte
684/// list>`. If the first token isn't WITH, returns `toks` unchanged.
685/// If the inner statement keyword hasn't been typed yet (cursor still
686/// inside a CTE body), returns `toks` so the walker keeps the existing
687/// behaviour for that position.
688fn strip_with_prefix<'a, 'b>(toks: &'a [Tok<'b>]) -> &'a [Tok<'b>] {
689  let Some(Tok::Word(w)) = toks.first() else {
690    return toks;
691  };
692  if !w.eq_ignore_ascii_case("WITH") {
693    return toks;
694  }
695  let mut depth = 0i32;
696  for (i, t) in toks.iter().enumerate().skip(1) {
697    match t {
698      Tok::Punct('(') => depth += 1,
699      Tok::Punct(')') => depth -= 1,
700      Tok::Word(w) if depth == 0 => {
701        let up = w.to_ascii_uppercase();
702        if matches!(up.as_str(), "SELECT" | "INSERT" | "UPDATE" | "DELETE") {
703          return &toks[i..];
704        }
705      },
706      _ => {},
707    }
708  }
709  toks
710}
711
712fn upper(s: &str) -> String {
713  s.to_ascii_uppercase()
714}
715
716fn walk(toks: &[Tok<'_>]) -> Phase {
717  use Phase::*;
718  let mut phase = Start;
719  let mut i = 0;
720  while i < toks.len() {
721    let t = &toks[i];
722    phase = match (&phase, t) {
723      // Top-level statement starters
724      (Start, Tok::Word(w)) => match upper(w).as_str() {
725        "SELECT" => SelectProjection,
726        "INSERT" => AfterInsert,
727        "UPDATE" => AfterUpdate,
728        "DELETE" => AfterDelete,
729        "CREATE" => AfterCreate,
730        "ALTER" => AfterAlter,
731        "DROP" => AfterDrop,
732        "GRANT" => AfterGrantOrRevoke,
733        "REVOKE" => AfterGrantOrRevoke,
734        "WITH" => Start, // CTE; fall through to inner SELECT
735        _ => Unknown,
736      },
737
738      // ----- SELECT branch -----
739      (SelectProjection, Tok::Word(w)) => match upper(w).as_str() {
740        "FROM" => ExpectTable,
741        "DISTINCT" | "ALL" => SelectProjection,
742        _ => InProjection,
743      },
744      (SelectProjection, Tok::Punct('*')) => AfterStar,
745      (SelectProjection, Tok::Punct(',')) => NextProjection,
746      (SelectProjection, Tok::Punct('(')) => InProjection,
747      (SelectProjection, _) => InProjection,
748
749      (NextProjection, Tok::Word(w)) => match upper(w).as_str() {
750        "FROM" => ExpectTable,
751        _ => InProjection,
752      },
753      (NextProjection, Tok::Punct('*')) => AfterStar,
754      (NextProjection, _) => InProjection,
755
756      (InProjection, Tok::Word(w)) => match upper(w).as_str() {
757        "FROM" => ExpectTable,
758        "AS" => ProjectionAlias,
759        _ => InProjection,
760      },
761      (InProjection, Tok::Punct(',')) => NextProjection,
762      (InProjection, Tok::Punct('.')) => InProjection,
763      (InProjection, Tok::Punct('(')) => InProjection,
764      (InProjection, _) => InProjection,
765
766      (ProjectionAlias, Tok::Word(_)) => InProjection,
767      (ProjectionAlias, _) => ProjectionAlias,
768
769      (AfterStar, Tok::Word(w)) => match upper(w).as_str() {
770        "FROM" => ExpectTable,
771        _ => InProjection,
772      },
773      (AfterStar, Tok::Punct(',')) => NextProjection,
774      (AfterStar, _) => AfterStar,
775
776      // ----- FROM / table list / joins -----
777      (ExpectTable, Tok::Word(_)) => AfterTable,
778      (ExpectTable, _) => ExpectTable,
779
780      (AfterTable, Tok::Word(w)) => match upper(w).as_str() {
781        "AS" => AfterTable,
782        "JOIN" => ExpectTable,
783        "INNER" | "LEFT" | "RIGHT" | "FULL" | "CROSS" => JoinModifier,
784        "WHERE" => WhereClause,
785        "GROUP" => AfterGroup,
786        "ORDER" => AfterOrder,
787        "HAVING" => HavingClause,
788        "LIMIT" => LimitClause,
789        "OFFSET" => OffsetClause,
790        "ON" => OnClause,
791        "USING" => UsingClause,
792        _ => AfterTable, // alias name
793      },
794      (AfterTable, Tok::Punct(',')) => ExpectTable,
795      (AfterTable, Tok::Punct(';')) => Start,
796      (AfterTable, _) => AfterTable,
797
798      (JoinModifier, Tok::Word(w)) => match upper(w).as_str() {
799        "JOIN" => ExpectTable,
800        "OUTER" => JoinModifier,
801        _ => JoinModifier,
802      },
803      (JoinModifier, _) => JoinModifier,
804
805      (OnClause, Tok::Word(w)) => match upper(w).as_str() {
806        "AND" | "OR" => OnClause,
807        "JOIN" => ExpectTable,
808        "WHERE" => WhereClause,
809        "GROUP" => AfterGroup,
810        "ORDER" => AfterOrder,
811        "INNER" | "LEFT" | "RIGHT" | "FULL" | "CROSS" => JoinModifier,
812        _ => InPredicate,
813      },
814      (OnClause, Tok::Punct(';')) => Start,
815      (OnClause, _) => OnClause,
816
817      // ----- WHERE / predicate -----
818      (WhereClause, Tok::Word(w)) => match upper(w).as_str() {
819        "GROUP" => AfterGroup,
820        "ORDER" => AfterOrder,
821        "HAVING" => HavingClause,
822        "LIMIT" => LimitClause,
823        "RETURNING" => ReturningClause,
824        _ => InPredicate,
825      },
826      (WhereClause, Tok::Punct(';')) => Start,
827      (WhereClause, _) => WhereClause,
828
829      (InPredicate, Tok::Word(w)) => match upper(w).as_str() {
830        "AND" | "OR" | "NOT" => WhereClause,
831        "GROUP" => AfterGroup,
832        "ORDER" => AfterOrder,
833        "HAVING" => HavingClause,
834        "LIMIT" => LimitClause,
835        "RETURNING" => ReturningClause,
836        _ => InPredicate,
837      },
838      (InPredicate, Tok::Punct(';')) => Start,
839      (InPredicate, _) => InPredicate,
840
841      // ----- GROUP BY / ORDER BY -----
842      (AfterGroup, Tok::Word(w)) if upper(w) == "BY" => GroupByList,
843      (AfterGroup, _) => AfterGroup,
844      (GroupByList, Tok::Word(w)) => match upper(w).as_str() {
845        "ORDER" => AfterOrder,
846        "HAVING" => HavingClause,
847        "LIMIT" => LimitClause,
848        _ => GroupByList,
849      },
850      (GroupByList, Tok::Punct(';')) => Start,
851      (GroupByList, _) => GroupByList,
852
853      (AfterOrder, Tok::Word(w)) if upper(w) == "BY" => OrderByList,
854      (AfterOrder, _) => AfterOrder,
855      (OrderByList, Tok::Word(w)) => match upper(w).as_str() {
856        "LIMIT" => LimitClause,
857        _ => OrderByList,
858      },
859      (OrderByList, Tok::Punct(';')) => Start,
860      (OrderByList, _) => OrderByList,
861
862      (HavingClause, Tok::Word(w)) => match upper(w).as_str() {
863        "ORDER" => AfterOrder,
864        "LIMIT" => LimitClause,
865        _ => HavingClause,
866      },
867      (HavingClause, Tok::Punct(';')) => Start,
868      (HavingClause, _) => HavingClause,
869
870      (LimitClause, Tok::Word(w)) if upper(w) == "OFFSET" => OffsetClause,
871      (LimitClause, Tok::Punct(';')) => Start,
872      (LimitClause, _) => LimitClause,
873      (OffsetClause, Tok::Punct(';')) => Start,
874      (OffsetClause, _) => OffsetClause,
875
876      // ----- INSERT / UPDATE / DELETE skeletons -----
877      (AfterInsert, Tok::Word(w)) if upper(w) == "INTO" => AfterInsertTable,
878      (AfterInsert, _) => AfterInsert,
879      (AfterInsertTable, Tok::Word(_)) => InsertColumnList,
880      (AfterInsertTable, _) => AfterInsertTable,
881      (InsertColumnList, Tok::Word(w)) if upper(w) == "VALUES" => InsertValuesList,
882      (InsertColumnList, Tok::Word(w)) if upper(w) == "SELECT" => SelectProjection,
883      (InsertColumnList, Tok::Punct(';')) => Start,
884      (InsertColumnList, _) => InsertColumnList,
885      (InsertExpectValues, _) => InsertValuesList,
886      (InsertValuesList, Tok::Word(w)) if upper(w) == "RETURNING" => ReturningClause,
887      (InsertValuesList, Tok::Punct(';')) => Start,
888      (InsertValuesList, _) => InsertValuesList,
889
890      (AfterUpdate, Tok::Word(_)) => AfterUpdateTable,
891      (AfterUpdate, _) => AfterUpdate,
892      (AfterUpdateTable, Tok::Word(w)) if upper(w) == "SET" => UpdateAssignment,
893      (AfterUpdateTable, _) => AfterUpdateTable,
894      (UpdateAssignment, Tok::Word(w)) => match upper(w).as_str() {
895        "WHERE" => WhereClause,
896        "RETURNING" => ReturningClause,
897        _ => UpdateAssignment,
898      },
899      (UpdateAssignment, Tok::Punct(';')) => Start,
900      (UpdateAssignment, _) => UpdateAssignment,
901
902      (AfterDelete, Tok::Word(w)) if upper(w) == "FROM" => ExpectTable,
903      (AfterDelete, _) => AfterDelete,
904
905      // RETURNING accepts a comma-separated column list followed by `;`.
906      (ReturningClause, Tok::Punct(';')) => Start,
907      (ReturningClause, _) => ReturningClause,
908
909      // ----- DDL -----
910      (AfterAlter, Tok::Word(w)) if upper(w) == "TABLE" => AfterAlterTableExpectName,
911      (AfterAlter, _) => Unknown,
912      (AfterCreate, _) | (AfterDrop, _) => Unknown,
913
914      // ALTER TABLE [IF EXISTS] [ONLY] <name>
915      (AfterAlterTableExpectName, Tok::Word(w)) => match upper(w).as_str() {
916        "IF" | "EXISTS" | "ONLY" => AfterAlterTableExpectName,
917        _ => AfterAlterTableName,
918      },
919      (AfterAlterTableExpectName, Tok::Punct('.')) => AfterAlterTableExpectName, // schema.name
920      (AfterAlterTableExpectName, _) => AfterAlterTableExpectName,
921
922      (AfterAlterTableName, Tok::Punct(';')) => Start,
923      (AfterAlterTableName, _) => AfterAlterTableName,
924
925      // GRANT / REVOKE: <privilege> [, <privilege>]* ON <target> TO/FROM <role>
926      (AfterGrantOrRevoke, Tok::Word(w)) => match upper(w).as_str() {
927        "ON" => AfterGrantOn,
928        "TO" | "FROM" => AfterGrantTo,
929        _ => AfterGrantOrRevoke, // privilege keyword (SELECT / ALL / ...)
930      },
931      (AfterGrantOrRevoke, Tok::Punct(',')) => AfterGrantOrRevoke,
932      (AfterGrantOrRevoke, Tok::Punct(';')) => Start,
933      (AfterGrantOrRevoke, _) => AfterGrantOrRevoke,
934
935      (AfterGrantOn, Tok::Word(w)) => match upper(w).as_str() {
936        "TO" | "FROM" => AfterGrantTo,
937        _ => AfterGrantOn, // target name / TABLE / SEQUENCE / ...
938      },
939      (AfterGrantOn, Tok::Punct(';')) => Start,
940      (AfterGrantOn, _) => AfterGrantOn,
941
942      (AfterGrantTo, Tok::Punct(';')) => Start,
943      (AfterGrantTo, _) => AfterGrantTo,
944
945      // Reset on semicolon from anywhere.
946      (_, Tok::Punct(';')) => Start,
947      (_, _) => phase.clone(),
948    };
949    i += 1;
950  }
951  phase
952}
953
954/// Returns true when `pos` sits inside an unclosed `$tag$ ... $tag$`
955/// block. Scans from buffer start counting opens vs closes; an odd
956/// count puts the cursor inside the body.
957fn inside_dollar_quoted(src: &str, pos: usize) -> bool {
958  let bytes = src.as_bytes();
959  let mut i = 0usize;
960  let mut current_tag: Option<String> = None;
961  while i < pos.min(bytes.len()) {
962    if let Some(tag) = &current_tag {
963      let closer = format!("${tag}$");
964      if src[i..].starts_with(&closer) {
965        i += closer.len();
966        current_tag = None;
967        continue;
968      }
969      i += 1;
970      continue;
971    }
972    if bytes[i] == b'$'
973      && let Some(end) = src[i + 1..].find('$')
974    {
975      let tag = &src[i + 1..i + 1 + end];
976      if tag.chars().all(|c| c.is_alphanumeric() || c == '_') {
977        current_tag = Some(tag.to_string());
978        i += 1 + end + 1;
979        continue;
980      }
981    }
982    i += 1;
983  }
984  current_tag.is_some()
985}
986
987/// True when the most recent `:=` (or `=` inside an UPDATE SET clause)
988/// before `pos` lies on the same PL/pgSQL statement -- i.e. between the
989/// last `;` and the cursor. Strings and parens are respected so a `:=`
990/// inside a literal doesn't fake-trigger.
991fn after_assignment(src: &str, pos: usize) -> bool {
992  let bytes = src.as_bytes();
993  let upper_pos = pos.min(bytes.len());
994  // Find the previous statement terminator.
995  let start = src[..upper_pos].rfind(';').map(|i| i + 1).unwrap_or(0);
996  let slice = &src[start..upper_pos];
997  let bs = slice.as_bytes();
998  let n = bs.len();
999  let mut depth = 0i32;
1000  let mut i = 0usize;
1001  let mut found = false;
1002  while i < n {
1003    match bs[i] {
1004      b'\'' => {
1005        i += 1;
1006        while i < n {
1007          if bs[i] == b'\'' {
1008            i += 1;
1009            break;
1010          }
1011          i += 1;
1012        }
1013      },
1014      b'(' => {
1015        depth += 1;
1016        i += 1;
1017      },
1018      b')' => {
1019        depth -= 1;
1020        i += 1;
1021      },
1022      b':' if depth == 0 && i + 1 < n && bs[i + 1] == b'=' => {
1023        found = true;
1024        i += 2;
1025      },
1026      _ => i += 1,
1027    }
1028  }
1029  found
1030}