Skip to main content

dsl_completion/
create_table.rs

1//! CREATE TABLE sub-phase analyzer.
2//!
3//! When the cursor sits inside the parenthesised body of a CREATE TABLE
4//! statement we want much narrower completion than the general SQL
5//! state machine offers: types when writing a column type, column
6//! constraint keywords (NOT NULL / DEFAULT / ...) after the type,
7//! tables after REFERENCES, and so on. The user explicitly does NOT
8//! want all-table-column completion when they are typing a column
9//! identifier.
10//!
11//! Strategy:
12//!   1. Locate every `CREATE TABLE [IF NOT EXISTS] <name> (` whose body
13//!      contains the cursor.
14//!   2. From the body-open paren up to the cursor, tokenize and walk an
15//!      entry-level state machine that resets on each top-level `,`.
16//!   3. Return a [`Phase`] reflecting what the user is about to type.
17
18use crate::phase::Phase;
19use text_size::TextSize;
20
21/// Try to classify the cursor's position inside an enclosing CREATE
22/// TABLE body. Returns None when the cursor is not inside one.
23pub fn detect(source: &str, offset: TextSize) -> Option<Phase> {
24  let pos: usize = offset.into();
25  let pos = pos.min(source.len());
26
27  // CREATE TABLE name?
28  if let Some(p) = detect_expect_name(source, pos) {
29    return Some(p);
30  }
31
32  let body = find_enclosing_body(source, pos)?;
33  let entry = current_entry(source, body.open + 1, pos);
34  let enclosing = enclosing_table_name(source, body.open);
35  Some(classify_entry(source, &entry, enclosing.as_deref()))
36}
37
38/// Walk back from the body-open paren to capture the CREATE TABLE name.
39/// Lets us suggest columns of THIS table inside `PRIMARY KEY (...)` etc.
40fn enclosing_table_name(source: &str, open: usize) -> Option<String> {
41  let before = &source[..open];
42  let upper = before.to_ascii_uppercase();
43  let kw_pos = upper.rfind("CREATE TABLE")?;
44  let after = &before[kw_pos + "CREATE TABLE".len()..];
45  let after_trim = after.trim_start();
46  let after_trim = after_trim
47    .strip_prefix("IF NOT EXISTS")
48    .or_else(|| after_trim.strip_prefix("if not exists"))
49    .map(|s| s.trim_start())
50    .unwrap_or(after_trim);
51  let name: String = after_trim.chars().take_while(|c| c.is_alphanumeric() || *c == '_' || *c == '.').collect();
52  if name.is_empty() {
53    return None;
54  }
55  // Strip schema qualifier `public.users` -> `users`.
56  Some(name.rsplit('.').next().unwrap_or(&name).to_string())
57}
58
59/// Cursor is right after `CREATE TABLE [IF NOT EXISTS] ` -- expect a name.
60fn detect_expect_name(source: &str, pos: usize) -> Option<Phase> {
61  let before = &source[..pos];
62  let trimmed_upper = strip_trailing_word(before).to_uppercase();
63  if trimmed_upper.ends_with("CREATE TABLE") || trimmed_upper.ends_with("CREATE TABLE IF NOT EXISTS") {
64    return Some(Phase::CtlExpectTableName);
65  }
66  None
67}
68
69struct Body {
70  open: usize,
71  /// Inclusive (exclusive paren index doesn't matter for our purposes;
72  /// we only need open).
73  #[allow(dead_code)]
74  close: usize,
75}
76
77/// Scan source for `CREATE TABLE ... (` whose open-paren / close-paren
78/// span contains `pos`. Returns the open paren index. Quotes and dollar
79/// quotes are honoured so a `(` inside a literal does not confuse us.
80fn find_enclosing_body(source: &str, pos: usize) -> Option<Body> {
81  let bytes = source.as_bytes();
82  let mut i = 0usize;
83  let upper_src = source.to_ascii_uppercase();
84
85  while i < bytes.len() {
86    // Skip strings + comments quickly.
87    let c = bytes[i] as char;
88    if c == '-' && i + 1 < bytes.len() && bytes[i + 1] == b'-' {
89      while i < bytes.len() && bytes[i] != b'\n' {
90        i += 1;
91      }
92      continue;
93    }
94    if c == '/' && i + 1 < bytes.len() && bytes[i + 1] == b'*' {
95      i += 2;
96      while i + 1 < bytes.len() && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
97        i += 1;
98      }
99      i = (i + 2).min(bytes.len());
100      continue;
101    }
102    if c == '\'' {
103      i += 1;
104      while i < bytes.len() && !(bytes[i] == b'\'' && bytes[i - 1] != b'\\') {
105        i += 1;
106      }
107      i = (i + 1).min(bytes.len());
108      continue;
109    }
110    if c == '$'
111      && let Some(end) = source[i + 1..].find('$')
112    {
113      let tag = &source[i + 1..i + 1 + end];
114      if tag.chars().all(|ch| ch.is_alphanumeric() || ch == '_') {
115        let closer = format!("${tag}$");
116        let body_start = i + 1 + end + 1;
117        if let Some(off) = source[body_start..].find(&closer) {
118          i = body_start + off + closer.len();
119          continue;
120        }
121        return None;
122      }
123    }
124    // Look for "CREATE TABLE" as a whole word at i. Byte compare
125    // so a non-ASCII byte at `i` (mid multi-byte char) can't
126    // panic-slice the uppercased string.
127    const CT: &[u8] = b"CREATE TABLE";
128    if i + CT.len() <= bytes.len()
129      && upper_src.as_bytes()[i..i + CT.len()].eq_ignore_ascii_case(CT)
130      && (i == 0 || !is_word(bytes[i - 1] as char))
131      && bytes.get(i + CT.len()).is_none_or(|b| !is_word(*b as char))
132    {
133      // Walk forward to the next '(' that is not inside a quote.
134      let mut j = i + "CREATE TABLE".len();
135      while j < bytes.len() && bytes[j] as char != '(' {
136        if bytes[j] == b';' {
137          break;
138        }
139        j += 1;
140      }
141      if j < bytes.len() && bytes[j] as char == '(' {
142        let open = j;
143        // Find matching close paren.
144        let mut depth: i32 = 1;
145        let mut k = open + 1;
146        while k < bytes.len() && depth > 0 {
147          match bytes[k] as char {
148            '(' => depth += 1,
149            ')' => depth -= 1,
150            '\'' => {
151              k += 1;
152              while k < bytes.len() && !(bytes[k] == b'\'' && bytes[k - 1] != b'\\') {
153                k += 1;
154              }
155            },
156            _ => {},
157          }
158          k += 1;
159        }
160        // When the closing paren is missing (user still typing
161        // the body), treat the end-of-source as the close so
162        // any cursor inside still resolves to this body.
163        let close = if depth > 0 { bytes.len() } else { k.saturating_sub(1) };
164        if pos > open && pos <= close {
165          return Some(Body { open, close });
166        }
167        i = close + 1;
168        continue;
169      }
170    }
171    i += 1;
172  }
173  None
174}
175
176/// Slice of body text since the last top-level comma (or the open paren).
177struct Entry<'a> {
178  text: &'a str,
179}
180
181fn current_entry<'a>(source: &'a str, body_start: usize, cursor: usize) -> Entry<'a> {
182  let bytes = source.as_bytes();
183  let end = cursor.min(bytes.len());
184  let mut depth: i32 = 0;
185  let mut last_split = body_start;
186  let mut i = body_start;
187  while i < end {
188    let c = bytes[i] as char;
189    // Skip line + block comments so a `-- foo` between columns doesn't
190    // fake-extend the previous entry; without this the token count
191    // includes comment words and misclassifies the column phase.
192    if c == '-' && i + 1 < end && bytes[i + 1] == b'-' {
193      while i < end && bytes[i] != b'\n' {
194        i += 1;
195      }
196      continue;
197    }
198    if c == '/' && i + 1 < end && bytes[i + 1] == b'*' {
199      i += 2;
200      while i + 1 < end && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
201        i += 1;
202      }
203      i = (i + 2).min(end);
204      continue;
205    }
206    if c == '\'' {
207      i += 1;
208      while i < end && !(bytes[i] == b'\'' && bytes[i - 1] != b'\\') {
209        i += 1;
210      }
211      i = (i + 1).min(end);
212      continue;
213    }
214    if c == '(' {
215      depth += 1;
216    } else if c == ')' {
217      depth -= 1;
218    } else if c == ',' && depth == 0 {
219      last_split = i + 1;
220    }
221    i += 1;
222  }
223  Entry { text: strip_entry_comments(&source[last_split..end]) }
224}
225
226/// Remove `-- ...` and `/* ... */` runs from a snippet so the token
227/// counter in classify_entry only sees real SQL identifiers.
228fn strip_entry_comments(s: &str) -> &str {
229  // For simplicity, only strip when comments don't sit before non-ws:
230  // common case is `id int -- pk` where the comment is at end. The
231  // body-walk above already skipped fully-consumed comments; this is
232  // a safety net for trailing-comment tails on the active entry. We
233  // return the slice up to the first `--` or `/*` if found.
234  if let Some(idx) = s.find("--") {
235    return &s[..idx];
236  }
237  if let Some(idx) = s.find("/*") {
238    return &s[..idx];
239  }
240  s
241}
242
243fn classify_entry(_source: &str, entry: &Entry<'_>, enclosing: Option<&str>) -> Phase {
244  let raw = entry.text.trim_start();
245  if raw.is_empty() {
246    return Phase::CtlBodyStart;
247  }
248  // Was the user mid-typing a word right before the cursor? When the
249  // last char is a word char and not separated by whitespace from what
250  // came before, that token is "in progress" -- it belongs to the
251  // next slot we expect the user to fill. Strip it so the phase
252  // reflects what they are typing, not what they have committed.
253  let bytes = raw.as_bytes();
254  let trailing_partial = matches!(bytes.last(), Some(&b) if (b as char).is_alphanumeric() || b == b'_');
255  let committed: &str = if trailing_partial {
256    let mut end = raw.len();
257    while end > 0 {
258      let c = bytes[end - 1] as char;
259      if c.is_alphanumeric() || c == '_' {
260        end -= 1;
261      } else {
262        break;
263      }
264    }
265    &raw[..end]
266  } else {
267    raw
268  };
269  let committed = committed.trim_end();
270  let upper = committed.to_uppercase();
271
272  // Constraint-line entries.
273  if upper.starts_with("CONSTRAINT") {
274    let rest = strip_kw(committed, "CONSTRAINT");
275    // Strip the constraint name (single identifier) before classifying
276    // the rest -- so `CONSTRAINT pk_users PRIMARY KEY (` lands in the
277    // column-list arm below.
278    let after_name = {
279      let trimmed = rest.trim_start();
280      let ident_len = trimmed.chars().take_while(|c| c.is_alphanumeric() || *c == '_').count();
281      &trimmed[ident_len..]
282    };
283    let after_upper = after_name.trim_start().to_uppercase();
284    if after_upper.starts_with("PRIMARY KEY") || after_upper.starts_with("UNIQUE") {
285      if inside_paren(committed)
286        && let Some(t) = enclosing.map(str::to_string)
287      {
288        return Phase::CtlExpectFkColumn { table: t };
289      }
290      return Phase::Unknown;
291    }
292    if after_upper.starts_with("CHECK") {
293      // Named CHECK constraint: CONSTRAINT <name> CHECK ( <expr> )
294      // -- inside the paren the user types an arbitrary expression,
295      // so surface columns + the full PG function library +
296      // expression keywords (sql, char_length, length, now, ...).
297      if inside_paren(committed) {
298        return Phase::CtlCheckExpr { table: enclosing.map(str::to_string) };
299      }
300      return Phase::Unknown;
301    }
302    if after_upper.starts_with("FOREIGN KEY") {
303      if !after_upper.contains("REFERENCES")
304        && inside_paren(committed)
305        && let Some(t) = enclosing.map(str::to_string)
306      {
307        return Phase::CtlExpectFkColumn { table: t };
308      }
309      return fk_phase(&after_upper);
310    }
311    if let Some(pos) = after_upper.rfind("REFERENCES") {
312      let after = after_upper[pos + "REFERENCES".len()..].trim_start();
313      return fk_phase_after_references(after);
314    }
315    return if has_at_most_one_identifier(rest) {
316      Phase::CtlExpectConstraintName
317    } else {
318      Phase::CtlExpectConstraintKind
319    };
320  }
321  // Bare table-level PRIMARY KEY / UNIQUE clause -- once the cursor is
322  // inside the parens, suggest columns of the table being created.
323  if upper.starts_with("PRIMARY KEY") || upper.starts_with("UNIQUE") {
324    if inside_paren(committed)
325      && let Some(t) = enclosing.map(str::to_string)
326    {
327      return Phase::CtlExpectFkColumn { table: t };
328    }
329    return Phase::Unknown;
330  }
331  if upper.starts_with("CHECK") {
332    if inside_paren(committed) {
333      return Phase::CtlCheckExpr { table: enclosing.map(str::to_string) };
334    }
335    return Phase::Unknown;
336  }
337  if upper.starts_with("FOREIGN KEY") {
338    // Inside the LEFT paren (the local-column list) but before REFERENCES
339    // -- suggest columns of this table.
340    if !upper.contains("REFERENCES")
341      && inside_paren(committed)
342      && let Some(t) = enclosing.map(str::to_string)
343    {
344      return Phase::CtlExpectFkColumn { table: t };
345    }
346    return fk_phase(&upper);
347  }
348  if let Some(pos) = upper.rfind("REFERENCES") {
349    let after = upper[pos + "REFERENCES".len()..].trim_start();
350    return fk_phase_after_references(after);
351  }
352
353  // Column declaration: <ident> [TYPE] [constraints ...]
354  let committed_tokens = simple_tokens(committed);
355  match committed_tokens.len() {
356    // Nothing committed yet -- user is typing the column name. We
357    // do NOT want catalog columns / functions to appear here, just
358    // the alternative starter keywords (CONSTRAINT, PRIMARY KEY, ...).
359    0 => Phase::CtlBodyStart,
360    // One identifier committed -- now expecting the type.
361    1 => Phase::CtlExpectType,
362    // Identifier + type-word committed (or more). Past the type
363    // stage: expecting column constraints next.
364    _ => {
365      // Column-level CHECK (...) / GENERATED ALWAYS AS (...): same
366      // "arbitrary expression over this table's columns" need the
367      // table-level CHECK entry above already gets right, but a
368      // column-level one (no comma before it -- it's a modifier
369      // directly after the column's type, not its own list entry)
370      // never reached that check. Whatever was typed after CHECK(/
371      // GENERATED...AS( just fell into the generic column-constraint
372      // keyword phase below regardless of being mid-expression.
373      if inside_paren(committed)
374        && (upper.contains("CHECK(")
375          || upper.contains("CHECK (")
376          || (upper.contains("GENERATED") && (upper.contains("AS(") || upper.contains("AS ("))))
377      {
378        return Phase::CtlCheckExpr { table: enclosing.map(str::to_string) };
379      }
380      let second = committed_tokens[1].as_str();
381      if is_complete_type_token(second) || committed_tokens.len() > 2 {
382        Phase::CtlExpectColumnConstraint
383      } else {
384        Phase::CtlExpectType
385      }
386    },
387  }
388}
389
390/// True when the most recently opened `(` in `text` is still unclosed --
391/// i.e. the cursor is inside a paren group. Respects single-quoted
392/// strings so an apostrophe doesn't fake-open the group.
393fn inside_paren(text: &str) -> bool {
394  let bytes = text.as_bytes();
395  let n = bytes.len();
396  let mut depth = 0i32;
397  let mut i = 0usize;
398  while i < n {
399    match bytes[i] {
400      b'\'' => {
401        i += 1;
402        while i < n {
403          if bytes[i] == b'\'' {
404            i += 1;
405            break;
406          }
407          i += 1;
408        }
409      },
410      b'(' => {
411        depth += 1;
412        i += 1;
413      },
414      b')' => {
415        depth -= 1;
416        i += 1;
417      },
418      _ => i += 1,
419    }
420  }
421  depth > 0
422}
423
424fn fk_phase(upper: &str) -> Phase {
425  if let Some(p) = upper.find("REFERENCES") {
426    let after = upper[p + "REFERENCES".len()..].trim_start();
427    return fk_phase_after_references(after);
428  }
429  Phase::Unknown
430}
431
432fn fk_phase_after_references(after_upper: &str) -> Phase {
433  if after_upper.is_empty() {
434    return Phase::CtlExpectFkTable {};
435  }
436  let table_tok = after_upper.split(|c: char| c.is_whitespace() || c == '(').next().unwrap_or("");
437  if table_tok.is_empty() {
438    return Phase::CtlExpectFkTable {};
439  }
440  let rest = after_upper[table_tok.len()..].trim_start();
441  if let Some(body) = rest.strip_prefix('(') {
442    // Only stay in column-suggest mode while the paren remains open.
443    // Once `)` appears, the cursor sits past the column list (in an
444    // ON DELETE / DEFERRABLE / ... trailing slot) -- let the phase
445    // keyword detectors handle it.
446    if body.contains(')') {
447      return Phase::Unknown;
448    }
449    return Phase::CtlExpectFkColumn { table: table_tok.to_ascii_lowercase() };
450  }
451  Phase::CtlExpectFkTable {}
452}
453
454// ---------------------------------------------------------------------------
455// helpers
456// ---------------------------------------------------------------------------
457
458fn is_word(c: char) -> bool {
459  c.is_alphanumeric() || c == '_'
460}
461
462fn strip_trailing_word(s: &str) -> &str {
463  let bytes = s.as_bytes();
464  let mut end = bytes.len();
465  while end > 0 && bytes[end - 1].is_ascii() && is_word(bytes[end - 1] as char) {
466    end -= 1;
467  }
468  while end > 0 && bytes[end - 1].is_ascii_whitespace() {
469    end -= 1;
470  }
471  // Ensure we land on a UTF-8 char boundary -- non-ASCII tails can
472  // leave `end` inside a multi-byte sequence.
473  while end > 0 && !s.is_char_boundary(end) {
474    end -= 1;
475  }
476  &s[..end]
477}
478
479fn strip_kw<'a>(text: &'a str, kw: &str) -> &'a str {
480  let kw_len = kw.len();
481  if text.len() >= kw_len && text[..kw_len].eq_ignore_ascii_case(kw) {
482    return &text[kw_len..];
483  }
484  text
485}
486
487fn has_at_most_one_identifier(s: &str) -> bool {
488  simple_tokens(s).len() <= 1
489}
490
491/// Split into rough whitespace-bounded tokens, but respect parenthesised
492/// blocks so `NUMERIC(10,2)` stays one token.
493fn simple_tokens(s: &str) -> Vec<String> {
494  let mut out: Vec<String> = Vec::new();
495  let mut cur = String::new();
496  let mut depth: i32 = 0;
497  for c in s.chars() {
498    match c {
499      '(' => {
500        depth += 1;
501        cur.push(c);
502      },
503      ')' => {
504        depth -= 1;
505        cur.push(c);
506      },
507      _ if c.is_whitespace() && depth == 0 => {
508        if !cur.is_empty() {
509          out.push(std::mem::take(&mut cur));
510        }
511      },
512      _ => cur.push(c),
513    }
514  }
515  if !cur.is_empty() {
516    out.push(cur);
517  }
518  out
519}
520
521fn is_complete_type_token(tok: &str) -> bool {
522  let upper = tok.to_ascii_uppercase();
523  if tok.ends_with(')') {
524    return true;
525  }
526  matches!(
527    upper.as_str(),
528    "UUID"
529      | "TEXT"
530      | "DATE"
531      | "TIME"
532      | "TIMESTAMP"
533      | "TIMESTAMPTZ"
534      | "BOOLEAN"
535      | "BOOL"
536      | "INT"
537      | "INTEGER"
538      | "BIGINT"
539      | "SMALLINT"
540      | "SERIAL"
541      | "BIGSERIAL"
542      | "SMALLSERIAL"
543      | "REAL"
544      | "JSON"
545      | "JSONB"
546      | "BYTEA"
547      | "INET"
548      | "CIDR"
549      | "MACADDR"
550      | "INTERVAL"
551      | "MONEY"
552  )
553}