use crate::create_index;
use crate::create_table;
use crate::detectors::*;
use crate::fallback;
use crate::item::Item;
use crate::json_path;
use crate::merge;
use crate::phase::{self, Phase};
use crate::source_tables;
use crate::sources;
use dsl_catalog::Catalog;
use dsl_parse::ParsedFile;
use dsl_resolve::Scope;
use text_size::TextSize;
pub(crate) fn stmt_slice_upper(source: &str, offset: TextSize) -> (String, String) {
let pos: usize = (u32::from(offset) as usize).min(source.len());
let stmt_start = source[..pos].rfind(';').map(|p| p + 1).unwrap_or(0);
let slice = source[stmt_start..pos].to_string();
let upper = slice.to_ascii_uppercase();
(slice, upper)
}
pub(crate) fn current_statement_span(source: &str, offset: TextSize) -> &str {
let pos: usize = (u32::from(offset) as usize).min(source.len());
let start = source[..pos].rfind(';').map(|p| p + 1).unwrap_or(0);
let end = source[pos..].find(';').map(|p| pos + p + 1).unwrap_or(source.len());
&source[start..end]
}
pub(crate) fn cursor_not_at_ws_boundary(source: &str, offset: TextSize) -> bool {
let pos: usize = (u32::from(offset) as usize).min(source.len());
let bytes = source.as_bytes();
pos < bytes.len() && !bytes[pos].is_ascii_whitespace()
}
pub(crate) fn push_keyword_kvs(out: &mut Vec<Item>, kws: &[(&'static str, &'static str)]) {
for (kw, doc) in kws {
out.push(Item {
label: (*kw).into(),
kind: crate::item::ItemKind::Keyword,
detail: Some((*doc).into()),
insert_text: (*kw).into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
}
pub fn complete(source: &str, file: &ParsedFile, scopes: &[Scope], catalog: &Catalog, offset: TextSize) -> Vec<Item> {
let derived = source_tables::from_source(file, source);
complete_with_derived(source, file, scopes, catalog, &derived, offset)
}
pub fn complete_with_derived(
source: &str,
file: &ParsedFile,
scopes: &[Scope],
catalog: &Catalog,
derived: &Catalog,
offset: TextSize,
) -> Vec<Item> {
let raw_off: usize = offset.into();
let off = floor_char_boundary(source, raw_off.min(source.len()));
let offset = TextSize::from(off as u32);
let cat = source_tables::merge(catalog, derived);
for detector in PRE_PHASE_DETECTORS {
if let Some(items) = detector(source, offset, file, scopes, &cat) {
return items;
}
}
if let Some(ix_phase) = create_index::detect(source, offset) {
return route_phase(ix_phase, file, scopes, source, &cat, offset);
}
if let Some(ct_phase) = create_table::detect(source, offset) {
return route_phase(ct_phase, file, scopes, source, &cat, offset);
}
let ph = phase::detect(source, offset);
route_phase(ph, file, scopes, source, &cat, offset)
}
fn detect_fresh_name_slot(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
_cat: &Catalog,
) -> Option<Vec<Item>> {
if !at_fresh_name_slot(source, offset) {
return None;
}
if let Some(label) = fresh_name_slot_optional_keyword(source, offset) {
return Some(vec![crate::item::Item {
label: label.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some("optional clarifier before the new object name".into()),
insert_text: label.into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
}]);
}
if let Some(kws) = txn_followup_next_keyword(source, offset) {
let mut out = Vec::with_capacity(kws.len());
push_keyword_kvs(&mut out, kws);
return Some(out);
}
if let Some(kws) = fetch_move_direction_keyword(source, offset) {
let mut out = Vec::with_capacity(kws.len());
push_keyword_kvs(&mut out, kws);
return Some(out);
}
if let Some(kws) = create_transform_next_keyword(source, offset) {
let mut out = Vec::with_capacity(kws.len());
push_keyword_kvs(&mut out, kws);
return Some(out);
}
if let Some(kws) = create_user_mapping_next_keyword(source, offset) {
let mut out = Vec::with_capacity(kws.len());
push_keyword_kvs(&mut out, kws);
return Some(out);
}
if let Some(kws) = alter_user_mapping_next_keyword(source, offset) {
let mut out = Vec::with_capacity(kws.len());
push_keyword_kvs(&mut out, kws);
return Some(out);
}
if let Some(kws) = select_fetch_offset_next_keyword(source, offset) {
let mut out = Vec::with_capacity(kws.len());
push_keyword_kvs(&mut out, kws);
return Some(out);
}
if let Some(kws) = partition_next_keyword(source, offset) {
let mut out = Vec::with_capacity(kws.len());
push_keyword_kvs(&mut out, kws);
return Some(out);
}
Some(Vec::new())
}
fn detect_json_path_key(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
cat: &Catalog,
) -> Option<Vec<Item>> {
let keys = json_path::json_path_keys_at_with_catalog(source, offset, cat)?;
let mut out = Vec::with_capacity(keys.len());
for k in keys {
out.push(crate::item::Item {
label: k.clone(),
kind: crate::item::ItemKind::Variable,
detail: Some("JSON key".into()),
description: Some("known JSON key".into()),
documentation_md: None,
insert_text: k,
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
Some(out)
}
fn detect_execute_dynamic_sql(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
cat: &Catalog,
) -> Option<Vec<Item>> {
let pos: usize = u32::from(offset) as usize;
let span = execute_dynamic_sql_span(source, pos)?;
let raw = &source[span.content_start..span.content_end];
let raw_cursor = pos - span.content_start;
let (content, inner_offset) =
if span.dollar_quoted { (raw.to_string(), raw_cursor) } else { unescape_single_quoted(raw, raw_cursor) };
let inner_file = dsl_parse::parse(&content, dsl_parse::Dialect::Postgres);
let inner_scopes = dsl_resolve::resolve_with_source(&inner_file.statements, &content);
Some(complete(&content, &inner_file, &inner_scopes, cat, TextSize::from(inner_offset as u32)))
}
fn detect_inert_span(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
_cat: &Catalog,
) -> Option<Vec<Item>> {
if cursor_in_inert_span(source, u32::from(offset) as usize) { Some(Vec::new()) } else { None }
}
fn detect_dot_context(
source: &str,
offset: TextSize,
file: &ParsedFile,
scopes: &[Scope],
cat: &Catalog,
) -> Option<Vec<Item>> {
let alias = dot_alias(source, offset)?;
let mut out = Vec::new();
let alias_upper = alias.to_ascii_uppercase();
if alias_upper == "NEW" || alias_upper == "OLD" {
let pos: usize = u32::from(offset) as usize;
let target = trigger_target_table(source).or_else(|| enclosing_fn_trigger_table(source, pos, cat));
if let Some(t) = target {
sources::columns_of_table(cat, None, &t, &mut out);
return Some(out);
}
return Some(out);
}
if alias_upper == "EXCLUDED" {
if let Some(t) = insert_target_table_name_only(source) {
sources::columns_of_table(cat, None, &t, &mut out);
}
return Some(out);
}
let stmt_scope = scope_for_offset(file, scopes, offset);
let count = stmt_scope.map(|s| sources::columns_of_alias(cat, s, &alias, &mut out)).unwrap_or(0);
if count == 0
&& let Some(fb) = fallback::scope_from_text(current_statement_span(source, offset))
{
sources::columns_of_alias(cat, &fb, &alias, &mut out);
}
if out.is_empty()
&& let Some(s) = stmt_scope
&& let Some(cols) = s.cte_columns_of(&alias)
{
for col in cols {
out.push(crate::item::Item {
label: col.clone(),
kind: crate::item::ItemKind::Column,
detail: Some(format!("CTE {alias}")),
description: None,
documentation_md: None,
insert_text: col.clone(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
}
if out.is_empty()
&& let Some(cols) = fallback::cte_columns_from_text(current_statement_span(source, offset), &alias)
{
for col in cols {
out.push(crate::item::Item {
label: col.clone(),
kind: crate::item::ItemKind::Column,
detail: Some(format!("CTE {alias}")),
description: None,
documentation_md: None,
insert_text: col.clone(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
}
if out.is_empty() {
let pos: usize = u32::from(offset) as usize;
let locals = crate::plpgsql_locals::extract(source, pos);
if let Some(ty) = crate::plpgsql_locals::type_of(&locals, &alias) {
let bare = ty.split('%').next().unwrap_or(&ty).trim().trim_end_matches(';').trim();
if cat.find_table(None, bare).is_some() {
sources::columns_of_table(cat, None, bare, &mut out);
}
}
}
if out.is_empty() {
sources::tables_in_schema(cat, &alias, &mut out);
sources::functions_in_schema(cat, &alias, &mut out);
}
if out.is_empty() && cat.find_table(None, &alias).is_some() {
sources::columns_of_table(cat, None, &alias, &mut out);
}
let used = used_columns_in_clause(source, offset);
if !used.is_empty() {
out.retain(|it| !is_column_listed(it, &used));
}
Some(out)
}
fn detect_grouping_sets_inner_paren(
source: &str,
offset: TextSize,
file: &ParsedFile,
scopes: &[Scope],
cat: &Catalog,
) -> Option<Vec<Item>> {
if !grouping_sets_inner_paren_expects_column(source, offset) {
return None;
}
let mut out = Vec::new();
let mut tables: Vec<(Option<String>, String)> = Vec::new();
if let Some(scope) = scope_for_offset(file, scopes, offset) {
for b in scope.tables() {
tables.push((b.table.schema.clone(), b.table.name.clone()));
}
}
if tables.is_empty()
&& let Some(fb) = fallback::scope_from_text(current_statement_span(source, offset))
{
for b in fb.tables() {
tables.push((b.table.schema.clone(), b.table.name.clone()));
}
}
let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
for (schema, name) in tables {
let key = format!("{}.{}", schema.as_deref().unwrap_or(""), name.to_ascii_lowercase());
if !seen.insert(key) {
continue;
}
sources::columns_of_table(cat, schema.as_deref(), &name, &mut out);
}
push_aliases(file, scopes, source, offset, &mut out);
Some(out)
}
fn detect_contexts(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
cat: &Catalog,
) -> Option<Vec<Item>> {
crate::contexts::detect(source, offset, cat)
}
const PRE_PHASE_DETECTORS: &[Detector] = &[
detect_fresh_name_slot,
detect_json_path_key,
detect_execute_dynamic_sql,
detect_inert_span,
detect_dot_context,
detect_grouping_sets_inner_paren,
detect_contexts,
];
type Detector = fn(&str, TextSize, &ParsedFile, &[Scope], &Catalog) -> Option<Vec<Item>>;
fn detect_set_op_followup(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
_cat: &Catalog,
) -> Option<Vec<Item>> {
let kws = set_op_followup_next_keyword(source, offset)?;
let mut out = Vec::new();
push_keyword_kvs(&mut out, kws);
Some(out)
}
fn detect_json_table_fresh_column_slot(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
_cat: &Catalog,
) -> Option<Vec<Item>> {
if !json_table_fresh_column_slot(source, offset) {
return None;
}
let mut out = Vec::new();
push_keyword_kvs(&mut out, &[("FOR", "<name> FOR ORDINALITY -- 1-based row-number column")]);
Some(out)
}
fn detect_json_table_column_slot(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
_cat: &Catalog,
) -> Option<Vec<Item>> {
json_table_column_slot_items(source, offset)
}
fn detect_create_transform(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
_cat: &Catalog,
) -> Option<Vec<Item>> {
let kws = create_transform_next_keyword(source, offset)?;
let mut out = Vec::new();
push_keyword_kvs(&mut out, kws);
Some(out)
}
fn detect_tablesample_after_paren(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
_cat: &Catalog,
) -> Option<Vec<Item>> {
let kws = tablesample_after_paren_next_keyword(source, offset)?;
let mut out = Vec::new();
push_keyword_kvs(&mut out, kws);
Some(out)
}
fn detect_filter_clause(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
_cat: &Catalog,
) -> Option<Vec<Item>> {
let kws = filter_clause_next_keyword(source, offset)?;
let mut out = Vec::new();
push_keyword_kvs(&mut out, kws);
Some(out)
}
fn detect_policy_expr(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
cat: &Catalog,
) -> Option<Vec<Item>> {
policy_expr_items(source, offset, cat)
}
fn detect_table_function_with_ordinality(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
_cat: &Catalog,
) -> Option<Vec<Item>> {
let kws = table_function_with_ordinality_next_keyword(source, offset)?;
let mut out = Vec::new();
push_keyword_kvs(&mut out, kws);
Some(out)
}
fn detect_select_fetch_offset(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
_cat: &Catalog,
) -> Option<Vec<Item>> {
let kws = select_fetch_offset_next_keyword(source, offset)?;
let (_, upper) = stmt_slice_upper(source, offset);
let words: Vec<&str> = upper.split_ascii_whitespace().collect();
let last = words.last().copied();
if !matches!(last, Some("FETCH") | Some("FIRST") | Some("NEXT") | Some("ROW") | Some("ROWS")) {
return None;
}
let mut out = Vec::new();
push_keyword_kvs(&mut out, kws);
Some(out)
}
fn detect_create_view_post_name(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
_cat: &Catalog,
) -> Option<Vec<Item>> {
let kws = create_view_post_name_next_keyword(source, offset)?;
let (_, upper) = stmt_slice_upper(source, offset);
let words: Vec<&str> = upper.split_ascii_whitespace().collect();
let last = words.last().copied();
if !matches!(last, Some("WITH") | Some("NO") | Some("CASCADED") | Some("LOCAL")) {
return None;
}
let mut out = Vec::new();
push_keyword_kvs(&mut out, kws);
Some(out)
}
fn detect_window_clause_frame_bound(
source: &str,
offset: TextSize,
_file: &ParsedFile,
_scopes: &[Scope],
_cat: &Catalog,
) -> Option<Vec<Item>> {
if window_clause_partition_or_order_by_expects_column(source, offset) {
return None;
}
let kws = window_clause_as_paren_keyword(source, offset)?;
let pos: usize = (u32::from(offset) as usize).min(source.len());
let pre = &source[..pos];
let stmt_start_idx = pre.rfind(';').map(|i| i + 1).unwrap_or(0);
let words: Vec<&str> = pre[stmt_start_idx..].split_ascii_whitespace().collect();
let last_up = words.last().map(|s| s.to_ascii_uppercase()).unwrap_or_default();
if !matches!(
last_up.as_str(),
"RANGE" | "ROWS" | "GROUPS" | "BETWEEN" | "AND" | "PRECEDING" | "FOLLOWING" | "UNBOUNDED" | "CURRENT" | "EXCLUDE"
) {
return None;
}
let mut out = Vec::new();
push_keyword_kvs(&mut out, kws);
Some(out)
}
const POST_PHASE_DETECTORS: &[Detector] = &[
detect_set_op_followup,
detect_json_table_fresh_column_slot,
detect_json_table_column_slot,
detect_create_transform,
detect_tablesample_after_paren,
detect_filter_clause,
detect_policy_expr,
detect_table_function_with_ordinality,
detect_select_fetch_offset,
detect_create_view_post_name,
detect_window_clause_frame_bound,
];
fn route_phase(
ph: Phase,
file: &ParsedFile,
scopes: &[Scope],
source: &str,
cat: &Catalog,
offset: TextSize,
) -> Vec<Item> {
for detector in POST_PHASE_DETECTORS {
if let Some(items) = detector(source, offset, file, scopes, cat) {
return items;
}
}
let mut out = Vec::new();
match ph {
Phase::Start => {
if at_create_view_body_start(source, offset) {
for (kw, doc) in [
("SELECT", "the projection-list query that defines the view"),
("WITH", "CTE list then SELECT"),
("VALUES", "VALUES (...) literal-row source"),
("TABLE", "TABLE <name> -- shorthand for SELECT * FROM <name>"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
insert_text: kw.into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
} else {
sources::statement_keywords(&mut out);
}
},
Phase::SelectProjection | Phase::InProjection | Phase::NextProjection => {
push_scope_columns_or_all(file, scopes, source, cat, offset, &mut out);
push_aliases(file, scopes, source, offset, &mut out);
push_all_functions(cat, &mut out);
sources::expression_keywords(&mut out);
},
Phase::AfterStar | Phase::ProjectionAlias => {
if cast_as_expects_type(source, offset) {
sources::types_only(&mut out);
} else {
sources::after_projection_keywords(&mut out);
}
},
Phase::ExpectTable => {
sources::tables(cat, &mut out);
push_cte_names(file, scopes, source, offset, &mut out);
},
Phase::AfterTable | Phase::JoinModifier | Phase::JoinComplete => {
if window_clause_partition_or_order_by_expects_column(source, offset) {
push_scope_columns_or_all(file, scopes, source, cat, offset, &mut out);
push_aliases(file, scopes, source, offset, &mut out);
return dedup_items(out);
}
if window_clause_paren_expects_subclause(source, offset) {
for (kw, doc) in [
("PARTITION BY", "PARTITION BY <expr>[, ...] -- frame partitioning"),
("ORDER BY", "ORDER BY <expr>[, ...] -- frame ordering"),
("ROWS", "ROWS BETWEEN ... -- row-relative frame"),
("RANGE", "RANGE BETWEEN ... -- value-relative frame"),
("GROUPS", "GROUPS BETWEEN ... -- peer-group frame"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
description: None,
documentation_md: None,
insert_text: kw.into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
return dedup_items(out);
}
if lateral_target_expected(source, offset) {
for (label, doc) in LATERAL_TARGETS {
out.push(crate::item::Item {
label: (*label).into(),
kind: crate::item::ItemKind::Function,
detail: Some((*doc).into()),
description: None,
documentation_md: None,
insert_text: (*label).into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
sources::tables(cat, &mut out);
return dedup_items(out);
}
if tablesample_expects_method(source, offset) {
push_keyword_kvs(
&mut out,
&[
("BERNOULLI", "TABLESAMPLE BERNOULLI (<percent>) -- row-level uniform sample"),
("SYSTEM", "TABLESAMPLE SYSTEM (<percent>) -- page-level random sample"),
],
);
return dedup_items(out);
}
if let Some(kws) = select_for_locking_keywords(source, offset) {
for (kw, doc) in kws {
out.push(crate::item::Item {
label: (*kw).into(),
kind: crate::item::ItemKind::Keyword,
detail: Some((*doc).into()),
description: None,
documentation_md: None,
insert_text: (*kw).into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else {
push_aliases(file, scopes, source, offset, &mut out);
sources::after_table_keywords(&mut out);
}
},
Phase::OnClause | Phase::WhereClause | Phase::InPredicate | Phase::HavingClause => {
if let Some(kws) = is_predicate_continuation_keywords(source, offset) {
for (kw, doc) in kws {
out.push(crate::item::Item {
label: (*kw).into(),
kind: crate::item::ItemKind::Keyword,
detail: Some((*doc).into()),
description: None,
documentation_md: None,
insert_text: (*kw).into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else {
push_scope_columns_or_all(file, scopes, source, cat, offset, &mut out);
push_aliases(file, scopes, source, offset, &mut out);
push_all_functions(cat, &mut out);
sources::expression_keywords(&mut out);
}
},
Phase::UsingClause => {
push_scope_columns_or_all(file, scopes, source, cat, offset, &mut out);
push_aliases(file, scopes, source, offset, &mut out);
},
Phase::AfterGroup | Phase::AfterOrder => {
sources::after_table_keywords(&mut out);
},
Phase::GroupByList => {
if let Some(kws) = group_by_set_op_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
}
push_scope_columns_or_all(file, scopes, source, cat, offset, &mut out);
push_aliases(file, scopes, source, offset, &mut out);
push_all_functions(cat, &mut out);
},
Phase::OrderByList => {
if order_by_nulls_expects_first_last(source, offset) {
push_keyword_kvs(
&mut out,
&[
("FIRST", "NULLS FIRST -- NULLs sort before non-NULL values"),
("LAST", "NULLS LAST -- NULLs sort after non-NULL values"),
],
);
} else {
push_scope_columns_or_all(file, scopes, source, cat, offset, &mut out);
push_aliases(file, scopes, source, offset, &mut out);
push_all_functions(cat, &mut out);
sources::order_modifiers(&mut out);
}
},
Phase::LimitClause => {
out.push(crate::item::Item {
label: "OFFSET".into(),
kind: crate::item::ItemKind::Keyword,
detail: Some("OFFSET <n> -- skip the first n rows".into()),
description: None,
documentation_md: None,
insert_text: "OFFSET".into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
},
Phase::OffsetClause => {
},
Phase::AfterInsert => {
sources::after_projection_keywords(&mut out);
},
Phase::AfterInsertTable => {
sources::tables(cat, &mut out);
},
Phase::InsertColumnList => {
if let Some(kws) = insert_overriding_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
return out;
}
if let Some(kws) = insert_into_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
return out;
}
if let Some(target) = insert_target_table(source, offset) {
sources::columns_of_table(cat, None, &target, &mut out);
let used = used_columns_in_clause(source, offset);
if !used.is_empty() {
out.retain(|it| !is_column_listed(it, &used));
}
} else {
push_scope_columns_or_all(file, scopes, source, cat, offset, &mut out);
push_aliases(file, scopes, source, offset, &mut out);
}
},
Phase::InsertExpectValues | Phase::InsertValuesList => {
if let Some(kws) = on_conflict_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if on_conflict_expects_target_column(source, offset) {
if let Some(target) = dml_target_table(source, offset) {
sources::columns_of_table(cat, None, &target, &mut out);
let used = used_columns_in_clause(source, offset);
if !used.is_empty() {
out.retain(|it| !is_column_listed(it, &used));
}
}
} else if insert_after_values_tuple(source, offset) {
for (kw, doc) in [
("RETURNING", "RETURNING <cols> -- return inserted rows"),
("ON CONFLICT", "ON CONFLICT (cols) DO ... -- upsert handling"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
insert_text: kw.into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
} else {
out.push(
crate::item::Item {
label: "DEFAULT".into(),
kind: crate::item::ItemKind::Keyword,
detail: Some("use this column's DEFAULT value".into()),
insert_text: "DEFAULT".into(),
..Default::default()
}
.with_sort(0),
);
push_aliases(file, scopes, source, offset, &mut out);
push_all_functions(cat, &mut out);
sources::expression_keywords(&mut out);
}
},
Phase::AfterUpdate => {
sources::tables(cat, &mut out);
},
Phase::AfterUpdateTable => {
push_aliases(file, scopes, source, offset, &mut out);
sources::after_table_keywords(&mut out);
},
Phase::UpdateAssignment => {
if update_set_at_column_slot(source, offset) {
if let Some(target) = dml_target_table(source, offset) {
sources::columns_of_table(cat, None, &target, &mut out);
} else {
push_scope_columns_or_all(file, scopes, source, cat, offset, &mut out);
}
let used = used_columns_in_clause(source, offset);
if !used.is_empty() {
out.retain(|it| !used.contains(&it.label.to_ascii_lowercase()));
}
} else {
push_scope_columns_or_all(file, scopes, source, cat, offset, &mut out);
push_aliases(file, scopes, source, offset, &mut out);
push_all_functions(cat, &mut out);
sources::expression_keywords(&mut out);
}
},
Phase::AfterDelete => {
sources::after_projection_keywords(&mut out);
},
Phase::ReturningClause => {
let (_, ret_upper) = stmt_slice_upper(source, offset);
let ret_words: Vec<&str> = ret_upper.split_ascii_whitespace().collect();
let last_returning_word = ret_words.last().copied();
if last_returning_word == Some("AS") {
return out;
}
if let Some(target) = dml_target_table(source, offset) {
sources::columns_of_table(cat, None, &target, &mut out);
} else {
push_scope_columns_or_all(file, scopes, source, cat, offset, &mut out);
}
push_aliases(file, scopes, source, offset, &mut out);
push_all_functions(cat, &mut out);
sources::expression_keywords(&mut out);
let used = used_columns_in_clause(source, offset);
if !used.is_empty() {
out.retain(|it| !is_column_listed(it, &used));
}
},
Phase::CtlExpectTableName => {
out.push(crate::item::Item {
label: "IF NOT EXISTS".into(),
kind: crate::item::ItemKind::Keyword,
detail: Some("CREATE TABLE IF NOT EXISTS <name> -- skip silently if already present".into()),
insert_text: "IF NOT EXISTS".into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
},
Phase::CtlBodyStart => {
if let Some(kws) = exclude_constraint_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else {
sources::create_table_entry_starters(&mut out);
sources::types_only(&mut out);
}
},
Phase::CtlExpectType => {
if let Some(kws) = exclude_constraint_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else {
sources::types_only(&mut out);
}
},
Phase::CtlExpectColumnConstraint => {
if let Some(kws) = identity_paren_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = column_generated_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = column_constraint_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if ctl_column_constraint_after_default(source, offset) {
for (kw, doc) in DEFAULT_EXPRESSION_SUGGESTIONS {
out.push(crate::item::Item {
label: (*kw).into(),
kind: crate::item::ItemKind::Keyword,
detail: Some((*doc).into()),
insert_text: (*kw).into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
push_all_functions(cat, &mut out);
sources::expression_keywords(&mut out);
} else {
sources::column_constraint_keywords(&mut out);
push_all_functions(cat, &mut out);
sources::expression_keywords(&mut out);
}
},
Phase::CtlExpectConstraintName => {
},
Phase::CtlExpectConstraintKind => {
sources::constraint_kinds(&mut out);
},
Phase::CtlExpectFkTable {} => {
sources::tables(cat, &mut out);
},
Phase::CtlCheckExpr { ref table } => {
if let Some(t) = table.as_ref() {
sources::columns_of_table(cat, None, t, &mut out);
if out.is_empty() {
for name in crate::source_tables::buffer_column_names(source, t) {
out.push(crate::item::Item {
label: name.clone(),
kind: crate::item::ItemKind::Column,
detail: Some(format!("column of `{t}` (buffer)")),
description: None,
documentation_md: None,
insert_text: name,
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
}
}
push_all_functions(cat, &mut out);
sources::expression_keywords(&mut out);
},
Phase::CtlExpectFkColumn { ref table } => {
sources::columns_of_table(cat, None, table, &mut out);
if out.is_empty() {
for name in crate::source_tables::buffer_column_names(source, table) {
out.push(crate::item::Item {
label: name.clone(),
kind: crate::item::ItemKind::Column,
detail: Some(format!("{}.<column>", table)),
description: Some("buffer".into()),
documentation_md: None,
insert_text: name,
is_snippet: false,
kb_entry: None,
sort_priority: 5,
});
}
}
},
Phase::PlpgsqlBody => {
if let Some(items) = plpgsql_body_targeted_items(source, offset, file, scopes, cat) {
out = items;
} else {
let locals = crate::plpgsql_locals::extract(source, u32::from(offset) as usize);
crate::plpgsql_locals::push_items(&locals, &mut out);
sources::plpgsql_keywords(&mut out);
push_aliases(file, scopes, source, offset, &mut out);
push_all_functions(cat, &mut out);
sources::new_old_aliases(&mut out);
sources::tables(cat, &mut out);
sources::columns(cat, &mut out);
}
},
Phase::PlpgsqlAssignRhs => {
let locals = crate::plpgsql_locals::extract(source, u32::from(offset) as usize);
crate::plpgsql_locals::push_items(&locals, &mut out);
sources::new_old_aliases(&mut out);
push_aliases(file, scopes, source, offset, &mut out);
push_all_functions(cat, &mut out);
sources::expression_keywords(&mut out);
},
Phase::CastType => {
sources::types(&mut out);
sources::db_types(cat, &mut out);
},
Phase::AfterAlterTableExpectName => {
sources::tables(cat, &mut out);
},
Phase::AfterAlterTableName => {
if let Some(target) = alter_table_existing_column_target(source, offset) {
sources::columns_of_table(cat, None, &target, &mut out);
} else if alter_table_expects_type(source, offset) {
sources::types_only(&mut out);
} else if let Some(kws) = alter_column_set_value_keywords(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if alter_column_after_set_subkeyword_expects_silence(source, offset) {
} else if let Some(kind) = alter_column_action_kind(source, offset) {
let kws: &[(&str, &str)] = match kind {
AlterColumnAction::Set => &[
("DEFAULT", "SET DEFAULT <expr>"),
("NOT NULL", "SET NOT NULL"),
("DATA TYPE", "SET DATA TYPE <type>"),
("STATISTICS", "SET STATISTICS <int>"),
("STORAGE", "SET STORAGE PLAIN|EXTERNAL|EXTENDED|MAIN"),
("COMPRESSION", "SET COMPRESSION pglz|lz4|default"),
],
AlterColumnAction::Drop => &[
("DEFAULT", "DROP DEFAULT"),
("NOT NULL", "DROP NOT NULL"),
("IDENTITY", "DROP IDENTITY"),
("EXPRESSION", "DROP EXPRESSION"),
],
};
for (kw, doc) in kws {
out.push(crate::item::Item {
label: (*kw).into(),
kind: crate::item::ItemKind::Keyword,
detail: Some((*doc).into()),
description: None,
documentation_md: None,
insert_text: (*kw).into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else if let Some(kws) = column_constraint_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = column_generated_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = check_constraint_no_inherit_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = replica_identity_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if alter_table_add_column_after_default(source, offset) {
for (kw, doc) in DEFAULT_EXPRESSION_SUGGESTIONS {
out.push(crate::item::Item {
label: (*kw).into(),
kind: crate::item::ItemKind::Keyword,
detail: Some((*doc).into()),
insert_text: (*kw).into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
} else if alter_table_inherit_expects_parent(source, offset) {
sources::tables(cat, &mut out);
} else if let Some(kws) = alter_table_subaction_at(source, offset) {
push_keyword_kvs(&mut out, kws);
} else {
sources::alter_table_actions(&mut out);
}
},
Phase::AfterGrantOrRevoke => {
if let Some(kws) = grant_revoke_followup_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else {
sources::grant_privileges(&mut out);
out.push(crate::item::Item {
label: "ON".into(),
kind: crate::item::ItemKind::Keyword,
detail: Some("ON <object_class> <name>".into()),
insert_text: "ON".into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
let (_, upper) = stmt_slice_upper(source, offset);
if upper.trim_start().starts_with("REVOKE") {
out.push(crate::item::Item {
label: "GRANT OPTION FOR".into(),
kind: crate::item::ItemKind::Keyword,
detail: Some("REVOKE GRANT OPTION FOR <priv> -- drop forwarding right, keep the priv".into()),
insert_text: "GRANT OPTION FOR".into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
}
},
Phase::AfterGrantOn => {
sources::grant_object_classes(&mut out);
sources::tables(cat, &mut out);
},
Phase::AfterGrantTo => {
if let Some(kws) = set_role_auth_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
}
sources::grant_roles(cat, &mut out);
for (kw, doc) in [
("WITH GRANT OPTION", "GRANT ... WITH GRANT OPTION -- grantee can re-grant"),
("WITH ADMIN OPTION", "GRANT <role> ... WITH ADMIN OPTION -- grantee can add other members"),
("GRANTED BY", "GRANT ... GRANTED BY <role> -- record the grantor explicitly"),
("CASCADE", "REVOKE ... CASCADE -- also revoke dependent grants"),
("RESTRICT", "REVOKE ... RESTRICT -- fail if dependent grants exist (default)"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
insert_text: kw.into(),
kb_entry: None,
sort_priority: 1,
..Default::default()
});
}
},
Phase::AfterCreate | Phase::AfterAlter | Phase::AfterDrop | Phase::Unknown => {
if let Some(kws) = copy_paren_options_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if merge::merge_insert_col_list_slot(source, offset) {
let (tgt, _) = merge::merge_target_and_source(source);
if let Some(t) = tgt.as_deref() {
sources::columns_of_table(cat, None, t, &mut out);
}
} else if merge::merge_update_set_lhs_slot(source, offset) {
let (tgt, _) = merge::merge_target_and_source(source);
if let Some(t) = tgt.as_deref() {
sources::columns_of_table(cat, None, t, &mut out);
}
} else if grouping_sets_inner_paren_expects_column(source, offset) {
push_scope_columns_or_all(file, scopes, source, cat, offset, &mut out);
push_aliases(file, scopes, source, offset, &mut out);
} else if vacuum_paren_expects_option(source, offset) {
for (kw, doc) in [
("FULL", "FULL -- rewrite the table (locks it)"),
("FREEZE", "FREEZE -- mark tuples as committed eagerly"),
("VERBOSE", "VERBOSE -- per-relation progress"),
("ANALYZE", "ANALYZE -- update planner stats too"),
("SKIP_LOCKED", "SKIP_LOCKED -- don't wait for locks"),
("INDEX_CLEANUP", "INDEX_CLEANUP AUTO|ON|OFF"),
("PROCESS_TOAST", "PROCESS_TOAST [true|false]"),
("PROCESS_MAIN", "PROCESS_MAIN [true|false] -- PG16+"),
("TRUNCATE", "TRUNCATE [true|false] -- shrink the table file"),
("DISABLE_PAGE_SKIPPING", "DISABLE_PAGE_SKIPPING [true|false]"),
("BUFFER_USAGE_LIMIT", "BUFFER_USAGE_LIMIT '<size>' -- ring-buffer cap"),
("PARALLEL", "PARALLEL <int> -- workers for index vacuum (PG13+)"),
("SKIP_DATABASE_STATS", "SKIP_DATABASE_STATS [true|false] -- PG16+"),
("ONLY_DATABASE_STATS", "ONLY_DATABASE_STATS [true|false] -- PG16+"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
description: None,
documentation_md: None,
insert_text: kw.into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else if dml_drop_or_truncate_expects_table(source, offset) {
let (_, upper_drop) = stmt_slice_upper(source, offset);
let words: Vec<&str> = upper_drop.split_ascii_whitespace().collect();
let starts_drop = matches!(words.first().copied(), Some("DROP"));
let has_if_exists = words.windows(2).any(|w| w[0] == "IF" && w[1] == "EXISTS");
if starts_drop && !has_if_exists {
out.push(crate::item::Item {
label: "IF EXISTS".into(),
kind: crate::item::ItemKind::Keyword,
detail: Some("IF EXISTS -- skip silently when the target does not exist".into()),
insert_text: "IF EXISTS".into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
sources::tables(cat, &mut out);
} else if command_expects_role_name(source, offset) {
sources::grant_roles(cat, &mut out);
} else if reset_expects_subkeyword(source, offset) {
for (kw, doc) in
[("ALL", "RESET ALL -- reset every GUC to its default"), ("ROLE", "RESET ROLE -- undo a SET ROLE")]
{
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
description: None,
documentation_md: None,
insert_text: kw.into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else if raise_expects_level_keyword(source, offset) {
for (kw, doc) in [
("DEBUG", "RAISE DEBUG '...' -- developer-visible diagnostic"),
("LOG", "RAISE LOG '...' -- to server log only"),
("INFO", "RAISE INFO '...' -- to client always"),
("NOTICE", "RAISE NOTICE '...' -- default level"),
("WARNING", "RAISE WARNING '...' -- always to client"),
("EXCEPTION", "RAISE EXCEPTION '...' -- abort transaction (default if no level)"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
description: None,
documentation_md: None,
insert_text: kw.into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else if discard_expects_subkeyword(source, offset) {
for (kw, doc) in [
("ALL", "DISCARD ALL -- session reset"),
("PLANS", "DISCARD PLANS -- drop cached plans"),
("SEQUENCES", "DISCARD SEQUENCES -- forget session sequence state"),
("TEMP", "DISCARD TEMP -- drop temporary tables"),
("TEMPORARY", "DISCARD TEMPORARY -- same as DISCARD TEMP"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
description: None,
documentation_md: None,
insert_text: kw.into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else if create_index_expects_on(source, offset) {
out.push(crate::item::Item {
label: "ON".into(),
kind: crate::item::ItemKind::Keyword,
detail: Some("ON <table> (<col> [, ...])".into()),
description: None,
documentation_md: None,
insert_text: "ON".into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
} else if create_policy_expects_on(source, offset) {
out.push(crate::item::Item {
label: "ON".into(),
kind: crate::item::ItemKind::Keyword,
detail: Some("ON <table> -- attach the policy to a table".into()),
description: None,
documentation_md: None,
insert_text: "ON".into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
} else if create_policy_expects_table(source, offset) {
sources::tables(cat, &mut out);
} else if create_trigger_expects_timing(source, offset) {
for (kw, doc) in [
("BEFORE", "BEFORE <event> ON <table>"),
("AFTER", "AFTER <event> ON <table>"),
("INSTEAD OF", "INSTEAD OF <event> ON <view>"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
description: None,
documentation_md: None,
insert_text: kw.into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else if create_function_expects_return_type(source, offset) {
sources::types_only(&mut out);
} else if declare_cursor_for_expects_statement(source, offset) {
sources::statement_keywords(&mut out);
} else if with_cte_after_as_expects_materialized(source, offset) {
for (kw, doc) in [
("MATERIALIZED", "AS MATERIALIZED (...) -- always materialize the CTE"),
("NOT MATERIALIZED", "AS NOT MATERIALIZED (...) -- inline when possible"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
description: None,
documentation_md: None,
insert_text: kw.into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else if do_expects_language_or_body(source, offset) {
out.push(crate::item::Item {
label: "LANGUAGE".into(),
kind: crate::item::ItemKind::Keyword,
detail: Some("LANGUAGE plpgsql -- explicit body language (default is plpgsql)".into()),
description: None,
documentation_md: None,
insert_text: "LANGUAGE".into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
} else if create_sequence_expects_option(source, offset) {
for (kw, doc) in [
("AS", "AS <type> -- smallint / integer / bigint"),
("INCREMENT", "INCREMENT [BY] <n>"),
("MINVALUE", "MINVALUE <n> | NO MINVALUE"),
("MAXVALUE", "MAXVALUE <n> | NO MAXVALUE"),
("START", "START [WITH] <n>"),
("CACHE", "CACHE <n> -- preallocate n values per session"),
("CYCLE", "CYCLE -- wrap around at MAXVALUE/MINVALUE"),
("NO CYCLE", "NO CYCLE -- error at the limit (default)"),
("OWNED BY", "OWNED BY <table>.<column> -- auto-drop with the column"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
description: None,
documentation_md: None,
insert_text: kw.into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else if create_type_enum_or_range_body(source, offset) {
} else if create_type_as_expects_kind(source, offset) {
for (kw, doc) in [
("ENUM", "CREATE TYPE t AS ENUM ('a', 'b', ...) -- discrete labels"),
("RANGE", "CREATE TYPE t AS RANGE (SUBTYPE = ...) -- value-range type"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
description: None,
documentation_md: None,
insert_text: kw.into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else if explain_paren_format_value(source, offset) {
for (kw, doc) in [
("TEXT", "TEXT -- default human-readable"),
("JSON", "JSON -- machine-parseable"),
("XML", "XML"),
("YAML", "YAML"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
insert_text: kw.into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
} else if explain_paren_serialize_value(source, offset) {
for (kw, doc) in [
("none", "none -- skip output serialization"),
("text", "text -- text-protocol serialization (default)"),
("binary", "binary -- binary-protocol serialization"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
insert_text: kw.into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
} else if explain_paren_expects_option(source, offset) {
for (kw, doc) in [
("ANALYZE", "ANALYZE -- actually run and time the query"),
("VERBOSE", "VERBOSE -- include extra plan detail"),
("COSTS", "COSTS [true|false] -- show estimated start/total cost"),
("BUFFERS", "BUFFERS -- include buffer-use stats (requires ANALYZE)"),
("WAL", "WAL -- include WAL stats (requires ANALYZE)"),
("TIMING", "TIMING [true|false] -- include per-node timing"),
("SUMMARY", "SUMMARY [true|false] -- include planning/exec totals"),
("SETTINGS", "SETTINGS -- include any non-default GUCs"),
("FORMAT", "FORMAT TEXT|XML|JSON|YAML -- output format"),
("GENERIC_PLAN", "GENERIC_PLAN [true|false] -- show a parameter-free plan"),
("SERIALIZE", "SERIALIZE [text|binary|none] -- include output serialization cost (PG17+, requires ANALYZE)"),
("MEMORY", "MEMORY [true|false] -- per-node peak memory used (PG17+)"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
description: None,
documentation_md: None,
insert_text: kw.into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else if explain_expects_statement(source, offset) {
sources::statement_keywords(&mut out);
for (kw, doc) in [
("ANALYZE", "EXPLAIN ANALYZE -- actually run, report timing"),
("VERBOSE", "EXPLAIN VERBOSE -- include extra detail"),
("(", "EXPLAIN (FORMAT JSON, ANALYZE, ...) <stmt>"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
insert_text: kw.into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
} else if let Some(kw_list) = set_statement_completion(source, offset) {
for (kw, doc) in kw_list {
out.push(crate::item::Item {
label: (*kw).into(),
kind: crate::item::ItemKind::Keyword,
detail: Some((*doc).into()),
description: None,
documentation_md: None,
insert_text: (*kw).into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else if let Some(kws) = txn_followup_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kw_list) = transaction_control_completion(source, offset) {
for (kw, doc) in kw_list {
out.push(crate::item::Item {
label: (*kw).into(),
kind: crate::item::ItemKind::Keyword,
detail: Some((*doc).into()),
description: None,
documentation_md: None,
insert_text: (*kw).into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else if comment_on_expects_class_keyword(source, offset) {
for (kw, doc) in COMMENT_ON_CLASSES {
out.push(crate::item::Item {
label: (*kw).into(),
kind: crate::item::ItemKind::Keyword,
detail: Some((*doc).into()),
description: None,
documentation_md: None,
insert_text: (*kw).into(),
is_snippet: false,
kb_entry: None,
sort_priority: 0,
});
}
} else if let Some(kws) = set_role_auth_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = show_or_set_guc_names(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = comment_on_is_value_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = security_label_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = vacuum_paren_value_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = tablesample_after_paren_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = window_clause_as_paren_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_table_attach_detach_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = partition_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = ctas_with_data_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = insert_into_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = update_from_set_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = delete_using_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = group_by_set_op_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = select_fetch_offset_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = insert_overriding_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = on_conflict_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = prepare_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = declare_cursor_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = truncate_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = grant_revoke_followup_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = set_op_followup_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = with_cte_after_paren_close_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_table_post_body_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_database_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_database_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_tablespace_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_user_mapping_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_user_mapping_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_language_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_language_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_server_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_fdw_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = drop_user_mapping_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = exclude_constraint_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_function_attribute_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = set_role_auth_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = fetch_move_direction_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = lock_mode_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = deallocate_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_default_privileges_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_extension_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_transform_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_large_object_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_rule_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_trigger_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_access_method_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_conversion_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_conversion_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_operator_family_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_operator_class_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_event_trigger_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_operator_class_family_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_text_search_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_statistics_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_index_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_sequence_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_policy_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_domain_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_collation_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_access_method_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_operator_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_aggregate_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_publication_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_subscription_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_schema_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_text_search_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_extension_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_function_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_view_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_tablespace_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_aggregate_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_cast_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_rule_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_statistics_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_type_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_event_trigger_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_server_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_operator_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_domain_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_collation_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_system_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = cte_search_cycle_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = column_constraint_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = column_generated_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_index_trailing_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = check_constraint_no_inherit_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = refresh_mv_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = identity_paren_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = replica_identity_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_foreign_table_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_subscription_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_view_post_name_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_materialized_view_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_publication_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = cluster_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = import_foreign_schema_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_schema_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = reindex_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = set_transaction_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if call_expects_procedure(source, offset) {
for f in &cat.functions {
out.push(crate::item::Item {
label: f.name.clone(),
kind: crate::item::ItemKind::Function,
detail: Some("procedure / function".into()),
insert_text: format!("{}(", f.name),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
} else if let Some(kws) = reassign_owned_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = release_savepoint_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = txn_followup_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = copy_paren_options_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = copy_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = do_block_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = role_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(_tbl) = create_trigger_when_table(source, offset) {
for (kw, doc) in [
("NEW", "NEW row alias (INSERT / UPDATE triggers) -- type `NEW.<col>` to access columns"),
("OLD", "OLD row alias (UPDATE / DELETE triggers) -- type `OLD.<col>` to access columns"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
insert_text: kw.into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
for (kw, doc) in [
("IS DISTINCT FROM", "row-level distinctness comparison (NULL-safe)"),
("IS NOT DISTINCT FROM", "row-level equality comparison (NULL-safe)"),
("AND", "boolean AND"),
("OR", "boolean OR"),
("NOT", "boolean NOT"),
("IS NULL", "null-ness predicate"),
("IS NOT NULL", "null-ness predicate (negated)"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
insert_text: kw.into(),
kb_entry: None,
sort_priority: 1,
..Default::default()
});
}
} else if let Some(kws) = create_trigger_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_index_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = alter_type_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if merge::merge_update_set_lhs_slot(source, offset) {
let (tgt, _) = merge::merge_target_and_source(source);
if let Some(t) = tgt.as_deref() {
sources::columns_of_table(cat, None, t, &mut out);
}
} else if merge::merge_when_matched_and_predicate_slot(source, offset)
|| merge::merge_update_set_rhs_expr_slot(source, offset)
{
let (tgt, src_tbl) = merge::merge_target_and_source(source);
if let Some(t) = tgt.as_deref() {
sources::columns_of_table(cat, None, t, &mut out);
}
if let Some(s) = src_tbl.as_deref() {
sources::columns_of_table(cat, None, s, &mut out);
}
for alias in merge::merge_aliases(source) {
out.push(crate::item::Item {
label: alias.clone(),
kind: crate::item::ItemKind::Table,
detail: Some("MERGE alias".into()),
insert_text: alias,
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
push_all_functions(cat, &mut out);
sources::expression_keywords(&mut out);
} else if let Some(kws) = merge::merge_next_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = after_top_level_create_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = create_class_expects_if_not_exists(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = after_top_level_alter_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if let Some(kws) = after_top_level_drop_keyword(source, offset) {
push_keyword_kvs(&mut out, kws);
} else if drop_target_trailing_slot(source, offset) {
for (kw, doc) in [
("CASCADE", "DROP ... CASCADE -- drop dependent objects too"),
("RESTRICT", "DROP ... RESTRICT -- refuse if dependents exist (default)"),
] {
out.push(crate::item::Item {
label: kw.into(),
kind: crate::item::ItemKind::Keyword,
detail: Some(doc.into()),
insert_text: kw.into(),
kb_entry: None,
sort_priority: 0,
..Default::default()
});
}
} else {
sources::keywords(&mut out);
sources::types(&mut out);
sources::functions(&mut out);
sources::tables(cat, &mut out);
sources::columns(cat, &mut out);
}
},
}
if matches!(
ph,
Phase::SelectProjection
| Phase::InProjection
| Phase::NextProjection
| Phase::GroupByList
| Phase::OrderByList
| Phase::InsertColumnList
| Phase::UpdateAssignment
) {
let used = used_columns_in_clause(source, offset);
if !used.is_empty() {
out.retain(|it| !is_column_listed(it, &used));
}
}
dedup_items(out)
}