dsl-completion 0.1.0

Context-aware SQL completion for duck-sqllsp.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
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
//! CREATE TABLE sub-phase analyzer.
//!
//! When the cursor sits inside the parenthesised body of a CREATE TABLE
//! statement we want much narrower completion than the general SQL
//! state machine offers: types when writing a column type, column
//! constraint keywords (NOT NULL / DEFAULT / ...) after the type,
//! tables after REFERENCES, and so on. The user explicitly does NOT
//! want all-table-column completion when they are typing a column
//! identifier.
//!
//! Strategy:
//!   1. Locate every `CREATE TABLE [IF NOT EXISTS] <name> (` whose body
//!      contains the cursor.
//!   2. From the body-open paren up to the cursor, tokenize and walk an
//!      entry-level state machine that resets on each top-level `,`.
//!   3. Return a [`Phase`] reflecting what the user is about to type.

use crate::phase::Phase;
use text_size::TextSize;

/// Try to classify the cursor's position inside an enclosing CREATE
/// TABLE body. Returns None when the cursor is not inside one.
pub fn detect(source: &str, offset: TextSize) -> Option<Phase> {
  let pos: usize = offset.into();
  let pos = pos.min(source.len());

  // CREATE TABLE name?
  if let Some(p) = detect_expect_name(source, pos) {
    return Some(p);
  }

  let body = find_enclosing_body(source, pos)?;
  let entry = current_entry(source, body.open + 1, pos);
  let enclosing = enclosing_table_name(source, body.open);
  Some(classify_entry(source, &entry, enclosing.as_deref()))
}

/// Walk back from the body-open paren to capture the CREATE TABLE name.
/// Lets us suggest columns of THIS table inside `PRIMARY KEY (...)` etc.
fn enclosing_table_name(source: &str, open: usize) -> Option<String> {
  let before = &source[..open];
  let upper = before.to_ascii_uppercase();
  let kw_pos = upper.rfind("CREATE TABLE")?;
  let after = &before[kw_pos + "CREATE TABLE".len()..];
  let after_trim = after.trim_start();
  let after_trim = after_trim
    .strip_prefix("IF NOT EXISTS")
    .or_else(|| after_trim.strip_prefix("if not exists"))
    .map(|s| s.trim_start())
    .unwrap_or(after_trim);
  let name: String = after_trim.chars().take_while(|c| c.is_alphanumeric() || *c == '_' || *c == '.').collect();
  if name.is_empty() {
    return None;
  }
  // Strip schema qualifier `public.users` -> `users`.
  Some(name.rsplit('.').next().unwrap_or(&name).to_string())
}

/// Cursor is right after `CREATE TABLE [IF NOT EXISTS] ` -- expect a name.
fn detect_expect_name(source: &str, pos: usize) -> Option<Phase> {
  let before = &source[..pos];
  let trimmed_upper = strip_trailing_word(before).to_uppercase();
  if trimmed_upper.ends_with("CREATE TABLE") || trimmed_upper.ends_with("CREATE TABLE IF NOT EXISTS") {
    return Some(Phase::CtlExpectTableName);
  }
  None
}

struct Body {
  open: usize,
  /// Inclusive (exclusive paren index doesn't matter for our purposes;
  /// we only need open).
  #[allow(dead_code)]
  close: usize,
}

/// Scan source for `CREATE TABLE ... (` whose open-paren / close-paren
/// span contains `pos`. Returns the open paren index. Quotes and dollar
/// quotes are honoured so a `(` inside a literal does not confuse us.
fn find_enclosing_body(source: &str, pos: usize) -> Option<Body> {
  let bytes = source.as_bytes();
  let mut i = 0usize;
  let upper_src = source.to_ascii_uppercase();

  while i < bytes.len() {
    // Skip strings + comments quickly.
    let c = bytes[i] as char;
    if c == '-' && i + 1 < bytes.len() && bytes[i + 1] == b'-' {
      while i < bytes.len() && bytes[i] != b'\n' {
        i += 1;
      }
      continue;
    }
    if c == '/' && i + 1 < bytes.len() && bytes[i + 1] == b'*' {
      i += 2;
      while i + 1 < bytes.len() && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
        i += 1;
      }
      i = (i + 2).min(bytes.len());
      continue;
    }
    if c == '\'' {
      i += 1;
      while i < bytes.len() && !(bytes[i] == b'\'' && bytes[i - 1] != b'\\') {
        i += 1;
      }
      i = (i + 1).min(bytes.len());
      continue;
    }
    if c == '$'
      && let Some(end) = source[i + 1..].find('$')
    {
      let tag = &source[i + 1..i + 1 + end];
      if tag.chars().all(|ch| ch.is_alphanumeric() || ch == '_') {
        let closer = format!("${tag}$");
        let body_start = i + 1 + end + 1;
        if let Some(off) = source[body_start..].find(&closer) {
          i = body_start + off + closer.len();
          continue;
        }
        return None;
      }
    }
    // Look for "CREATE TABLE" as a whole word at i. Byte compare
    // so a non-ASCII byte at `i` (mid multi-byte char) can't
    // panic-slice the uppercased string.
    const CT: &[u8] = b"CREATE TABLE";
    if i + CT.len() <= bytes.len()
      && upper_src.as_bytes()[i..i + CT.len()].eq_ignore_ascii_case(CT)
      && (i == 0 || !is_word(bytes[i - 1] as char))
      && bytes.get(i + CT.len()).is_none_or(|b| !is_word(*b as char))
    {
      // Walk forward to the next '(' that is not inside a quote.
      let mut j = i + "CREATE TABLE".len();
      while j < bytes.len() && bytes[j] as char != '(' {
        if bytes[j] == b';' {
          break;
        }
        j += 1;
      }
      if j < bytes.len() && bytes[j] as char == '(' {
        let open = j;
        // Find matching close paren.
        let mut depth: i32 = 1;
        let mut k = open + 1;
        while k < bytes.len() && depth > 0 {
          match bytes[k] as char {
            '(' => depth += 1,
            ')' => depth -= 1,
            '\'' => {
              k += 1;
              while k < bytes.len() && !(bytes[k] == b'\'' && bytes[k - 1] != b'\\') {
                k += 1;
              }
            },
            _ => {},
          }
          k += 1;
        }
        // When the closing paren is missing (user still typing
        // the body), treat the end-of-source as the close so
        // any cursor inside still resolves to this body.
        let close = if depth > 0 { bytes.len() } else { k.saturating_sub(1) };
        if pos > open && pos <= close {
          return Some(Body { open, close });
        }
        i = close + 1;
        continue;
      }
    }
    i += 1;
  }
  None
}

/// Slice of body text since the last top-level comma (or the open paren).
struct Entry<'a> {
  text: &'a str,
}

fn current_entry<'a>(source: &'a str, body_start: usize, cursor: usize) -> Entry<'a> {
  let bytes = source.as_bytes();
  let end = cursor.min(bytes.len());
  let mut depth: i32 = 0;
  let mut last_split = body_start;
  let mut i = body_start;
  while i < end {
    let c = bytes[i] as char;
    // Skip line + block comments so a `-- foo` between columns doesn't
    // fake-extend the previous entry; without this the token count
    // includes comment words and misclassifies the column phase.
    if c == '-' && i + 1 < end && bytes[i + 1] == b'-' {
      while i < end && bytes[i] != b'\n' {
        i += 1;
      }
      continue;
    }
    if c == '/' && i + 1 < end && bytes[i + 1] == b'*' {
      i += 2;
      while i + 1 < end && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
        i += 1;
      }
      i = (i + 2).min(end);
      continue;
    }
    if c == '\'' {
      i += 1;
      while i < end && !(bytes[i] == b'\'' && bytes[i - 1] != b'\\') {
        i += 1;
      }
      i = (i + 1).min(end);
      continue;
    }
    if c == '(' {
      depth += 1;
    } else if c == ')' {
      depth -= 1;
    } else if c == ',' && depth == 0 {
      last_split = i + 1;
    }
    i += 1;
  }
  Entry { text: strip_entry_comments(&source[last_split..end]) }
}

/// Remove `-- ...` and `/* ... */` runs from a snippet so the token
/// counter in classify_entry only sees real SQL identifiers.
fn strip_entry_comments(s: &str) -> &str {
  // For simplicity, only strip when comments don't sit before non-ws:
  // common case is `id int -- pk` where the comment is at end. The
  // body-walk above already skipped fully-consumed comments; this is
  // a safety net for trailing-comment tails on the active entry. We
  // return the slice up to the first `--` or `/*` if found.
  if let Some(idx) = s.find("--") {
    return &s[..idx];
  }
  if let Some(idx) = s.find("/*") {
    return &s[..idx];
  }
  s
}

fn classify_entry(_source: &str, entry: &Entry<'_>, enclosing: Option<&str>) -> Phase {
  let raw = entry.text.trim_start();
  if raw.is_empty() {
    return Phase::CtlBodyStart;
  }
  // Was the user mid-typing a word right before the cursor? When the
  // last char is a word char and not separated by whitespace from what
  // came before, that token is "in progress" -- it belongs to the
  // next slot we expect the user to fill. Strip it so the phase
  // reflects what they are typing, not what they have committed.
  let bytes = raw.as_bytes();
  let trailing_partial = matches!(bytes.last(), Some(&b) if (b as char).is_alphanumeric() || b == b'_');
  let committed: &str = if trailing_partial {
    let mut end = raw.len();
    while end > 0 {
      let c = bytes[end - 1] as char;
      if c.is_alphanumeric() || c == '_' {
        end -= 1;
      } else {
        break;
      }
    }
    &raw[..end]
  } else {
    raw
  };
  let committed = committed.trim_end();
  let upper = committed.to_uppercase();

  // Constraint-line entries.
  if upper.starts_with("CONSTRAINT") {
    let rest = strip_kw(committed, "CONSTRAINT");
    // Strip the constraint name (single identifier) before classifying
    // the rest -- so `CONSTRAINT pk_users PRIMARY KEY (` lands in the
    // column-list arm below.
    let after_name = {
      let trimmed = rest.trim_start();
      let ident_len = trimmed.chars().take_while(|c| c.is_alphanumeric() || *c == '_').count();
      &trimmed[ident_len..]
    };
    let after_upper = after_name.trim_start().to_uppercase();
    if after_upper.starts_with("PRIMARY KEY") || after_upper.starts_with("UNIQUE") {
      if inside_paren(committed)
        && let Some(t) = enclosing.map(str::to_string)
      {
        return Phase::CtlExpectFkColumn { table: t };
      }
      return Phase::Unknown;
    }
    if after_upper.starts_with("CHECK") {
      // Named CHECK constraint: CONSTRAINT <name> CHECK ( <expr> )
      // -- inside the paren the user types an arbitrary expression,
      // so surface columns + the full PG function library +
      // expression keywords (sql, char_length, length, now, ...).
      if inside_paren(committed) {
        return Phase::CtlCheckExpr { table: enclosing.map(str::to_string) };
      }
      return Phase::Unknown;
    }
    if after_upper.starts_with("FOREIGN KEY") {
      if !after_upper.contains("REFERENCES")
        && inside_paren(committed)
        && let Some(t) = enclosing.map(str::to_string)
      {
        return Phase::CtlExpectFkColumn { table: t };
      }
      return fk_phase(&after_upper);
    }
    if let Some(pos) = after_upper.rfind("REFERENCES") {
      let after = after_upper[pos + "REFERENCES".len()..].trim_start();
      return fk_phase_after_references(after);
    }
    return if has_at_most_one_identifier(rest) {
      Phase::CtlExpectConstraintName
    } else {
      Phase::CtlExpectConstraintKind
    };
  }
  // Bare table-level PRIMARY KEY / UNIQUE clause -- once the cursor is
  // inside the parens, suggest columns of the table being created.
  if upper.starts_with("PRIMARY KEY") || upper.starts_with("UNIQUE") {
    if inside_paren(committed)
      && let Some(t) = enclosing.map(str::to_string)
    {
      return Phase::CtlExpectFkColumn { table: t };
    }
    return Phase::Unknown;
  }
  if upper.starts_with("CHECK") {
    if inside_paren(committed) {
      return Phase::CtlCheckExpr { table: enclosing.map(str::to_string) };
    }
    return Phase::Unknown;
  }
  if upper.starts_with("FOREIGN KEY") {
    // Inside the LEFT paren (the local-column list) but before REFERENCES
    // -- suggest columns of this table.
    if !upper.contains("REFERENCES")
      && inside_paren(committed)
      && let Some(t) = enclosing.map(str::to_string)
    {
      return Phase::CtlExpectFkColumn { table: t };
    }
    return fk_phase(&upper);
  }
  if let Some(pos) = upper.rfind("REFERENCES") {
    let after = upper[pos + "REFERENCES".len()..].trim_start();
    return fk_phase_after_references(after);
  }

  // Column declaration: <ident> [TYPE] [constraints ...]
  let committed_tokens = simple_tokens(committed);
  match committed_tokens.len() {
    // Nothing committed yet -- user is typing the column name. We
    // do NOT want catalog columns / functions to appear here, just
    // the alternative starter keywords (CONSTRAINT, PRIMARY KEY, ...).
    0 => Phase::CtlBodyStart,
    // One identifier committed -- now expecting the type.
    1 => Phase::CtlExpectType,
    // Identifier + type-word committed (or more). Past the type
    // stage: expecting column constraints next.
    _ => {
      // Column-level CHECK (...) / GENERATED ALWAYS AS (...): same
      // "arbitrary expression over this table's columns" need the
      // table-level CHECK entry above already gets right, but a
      // column-level one (no comma before it -- it's a modifier
      // directly after the column's type, not its own list entry)
      // never reached that check. Whatever was typed after CHECK(/
      // GENERATED...AS( just fell into the generic column-constraint
      // keyword phase below regardless of being mid-expression.
      if inside_paren(committed)
        && (upper.contains("CHECK(")
          || upper.contains("CHECK (")
          || (upper.contains("GENERATED") && (upper.contains("AS(") || upper.contains("AS ("))))
      {
        return Phase::CtlCheckExpr { table: enclosing.map(str::to_string) };
      }
      let second = committed_tokens[1].as_str();
      if is_complete_type_token(second) || committed_tokens.len() > 2 {
        Phase::CtlExpectColumnConstraint
      } else {
        Phase::CtlExpectType
      }
    },
  }
}

/// True when the most recently opened `(` in `text` is still unclosed --
/// i.e. the cursor is inside a paren group. Respects single-quoted
/// strings so an apostrophe doesn't fake-open the group.
fn inside_paren(text: &str) -> bool {
  let bytes = text.as_bytes();
  let n = bytes.len();
  let mut depth = 0i32;
  let mut i = 0usize;
  while i < n {
    match bytes[i] {
      b'\'' => {
        i += 1;
        while i < n {
          if bytes[i] == b'\'' {
            i += 1;
            break;
          }
          i += 1;
        }
      },
      b'(' => {
        depth += 1;
        i += 1;
      },
      b')' => {
        depth -= 1;
        i += 1;
      },
      _ => i += 1,
    }
  }
  depth > 0
}

fn fk_phase(upper: &str) -> Phase {
  if let Some(p) = upper.find("REFERENCES") {
    let after = upper[p + "REFERENCES".len()..].trim_start();
    return fk_phase_after_references(after);
  }
  Phase::Unknown
}

fn fk_phase_after_references(after_upper: &str) -> Phase {
  if after_upper.is_empty() {
    return Phase::CtlExpectFkTable {};
  }
  let table_tok = after_upper.split(|c: char| c.is_whitespace() || c == '(').next().unwrap_or("");
  if table_tok.is_empty() {
    return Phase::CtlExpectFkTable {};
  }
  let rest = after_upper[table_tok.len()..].trim_start();
  if let Some(body) = rest.strip_prefix('(') {
    // Only stay in column-suggest mode while the paren remains open.
    // Once `)` appears, the cursor sits past the column list (in an
    // ON DELETE / DEFERRABLE / ... trailing slot) -- let the phase
    // keyword detectors handle it.
    if body.contains(')') {
      return Phase::Unknown;
    }
    return Phase::CtlExpectFkColumn { table: table_tok.to_ascii_lowercase() };
  }
  Phase::CtlExpectFkTable {}
}

// ---------------------------------------------------------------------------
// helpers
// ---------------------------------------------------------------------------

fn is_word(c: char) -> bool {
  c.is_alphanumeric() || c == '_'
}

fn strip_trailing_word(s: &str) -> &str {
  let bytes = s.as_bytes();
  let mut end = bytes.len();
  while end > 0 && bytes[end - 1].is_ascii() && is_word(bytes[end - 1] as char) {
    end -= 1;
  }
  while end > 0 && bytes[end - 1].is_ascii_whitespace() {
    end -= 1;
  }
  // Ensure we land on a UTF-8 char boundary -- non-ASCII tails can
  // leave `end` inside a multi-byte sequence.
  while end > 0 && !s.is_char_boundary(end) {
    end -= 1;
  }
  &s[..end]
}

fn strip_kw<'a>(text: &'a str, kw: &str) -> &'a str {
  let kw_len = kw.len();
  if text.len() >= kw_len && text[..kw_len].eq_ignore_ascii_case(kw) {
    return &text[kw_len..];
  }
  text
}

fn has_at_most_one_identifier(s: &str) -> bool {
  simple_tokens(s).len() <= 1
}

/// Split into rough whitespace-bounded tokens, but respect parenthesised
/// blocks so `NUMERIC(10,2)` stays one token.
fn simple_tokens(s: &str) -> Vec<String> {
  let mut out: Vec<String> = Vec::new();
  let mut cur = String::new();
  let mut depth: i32 = 0;
  for c in s.chars() {
    match c {
      '(' => {
        depth += 1;
        cur.push(c);
      },
      ')' => {
        depth -= 1;
        cur.push(c);
      },
      _ if c.is_whitespace() && depth == 0 => {
        if !cur.is_empty() {
          out.push(std::mem::take(&mut cur));
        }
      },
      _ => cur.push(c),
    }
  }
  if !cur.is_empty() {
    out.push(cur);
  }
  out
}

fn is_complete_type_token(tok: &str) -> bool {
  let upper = tok.to_ascii_uppercase();
  if tok.ends_with(')') {
    return true;
  }
  matches!(
    upper.as_str(),
    "UUID"
      | "TEXT"
      | "DATE"
      | "TIME"
      | "TIMESTAMP"
      | "TIMESTAMPTZ"
      | "BOOLEAN"
      | "BOOL"
      | "INT"
      | "INTEGER"
      | "BIGINT"
      | "SMALLINT"
      | "SERIAL"
      | "BIGSERIAL"
      | "SMALLSERIAL"
      | "REAL"
      | "JSON"
      | "JSONB"
      | "BYTEA"
      | "INET"
      | "CIDR"
      | "MACADDR"
      | "INTERVAL"
      | "MONEY"
  )
}