Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions crates/squawk_ide/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,34 @@ impl Binder {
None
}

pub(crate) fn lookup_with_table(
&self,
name: &Name,
kind: SymbolKind,
position: TextSize,
schema: &Option<Schema>,
table: &Option<Name>,
) -> Option<SyntaxNodePtr> {
let symbols = self.scopes[self.root_scope()].get(name)?;

let search_paths = match schema {
Some(s) => std::slice::from_ref(s),
None => self.search_path_at(position),
};

for search_schema in search_paths {
if let Some(symbol_id) = symbols.iter().copied().find(|id| {
let symbol = &self.symbols[*id];
symbol.kind == kind
&& symbol.schema.as_ref() == Some(search_schema)
&& &symbol.table == table
}) {
return Some(self.symbols[symbol_id].ptr);
}
}
None
}

pub(crate) fn lookup_info(
&self,
name_str: String,
Expand Down Expand Up @@ -186,6 +214,7 @@ fn bind_stmt(b: &mut Binder, stmt: ast::Stmt) {
bind_create_materialized_view(b, create_view)
}
ast::Stmt::CreateSequence(create_sequence) => bind_create_sequence(b, create_sequence),
ast::Stmt::CreateTrigger(create_trigger) => bind_create_trigger(b, create_trigger),
ast::Stmt::CreateTablespace(create_tablespace) => {
bind_create_tablespace(b, create_tablespace)
}
Expand Down Expand Up @@ -217,13 +246,15 @@ fn bind_create_table(b: &mut Binder, create_table: impl ast::HasCreateTable) {
ptr: name_ptr,
schema: Some(schema.clone()),
params: None,
table: None,
});

let type_id = b.symbols.alloc(Symbol {
kind: SymbolKind::Type,
ptr: name_ptr,
schema: Some(schema),
params: None,
table: None,
});

let root = b.root_scope();
Expand All @@ -248,6 +279,7 @@ fn bind_create_index(b: &mut Binder, create_index: ast::CreateIndex) {
ptr: name_ptr,
schema: Some(schema),
params: None,
table: None,
});

let root = b.root_scope();
Expand Down Expand Up @@ -276,6 +308,7 @@ fn bind_create_function(b: &mut Binder, create_function: ast::CreateFunction) {
ptr: name_ptr,
schema: Some(schema),
params,
table: None,
});

let root = b.root_scope();
Expand Down Expand Up @@ -304,6 +337,7 @@ fn bind_create_aggregate(b: &mut Binder, create_aggregate: ast::CreateAggregate)
ptr: name_ptr,
schema: Some(schema),
params,
table: None,
});

let root = b.root_scope();
Expand Down Expand Up @@ -332,6 +366,7 @@ fn bind_create_procedure(b: &mut Binder, create_procedure: ast::CreateProcedure)
ptr: name_ptr,
schema: Some(schema),
params,
table: None,
});

let root = b.root_scope();
Expand Down Expand Up @@ -360,6 +395,7 @@ fn bind_create_schema(b: &mut Binder, create_schema: ast::CreateSchema) {
ptr: name_ptr,
schema: Some(Schema(schema_name.clone())),
params: None,
table: None,
});

let root = b.root_scope();
Expand All @@ -386,6 +422,7 @@ fn bind_create_type(b: &mut Binder, create_type: ast::CreateType) {
ptr: name_ptr,
schema: Some(schema),
params: None,
table: None,
});

let root = b.root_scope();
Expand Down Expand Up @@ -413,6 +450,7 @@ fn bind_create_view(b: &mut Binder, create_view: ast::CreateView) {
ptr: name_ptr,
schema: Some(schema),
params: None,
table: None,
});

let root = b.root_scope();
Expand Down Expand Up @@ -440,6 +478,7 @@ fn bind_create_materialized_view(b: &mut Binder, create_view: ast::CreateMateria
ptr: name_ptr,
schema: Some(schema),
params: None,
table: None,
});

let root = b.root_scope();
Expand Down Expand Up @@ -468,12 +507,45 @@ fn bind_create_sequence(b: &mut Binder, create_sequence: ast::CreateSequence) {
ptr: name_ptr,
schema: Some(schema),
params: None,
table: None,
});

let root = b.root_scope();
b.scopes[root].insert(sequence_name, sequence_id);
}

fn bind_create_trigger(b: &mut Binder, create_trigger: ast::CreateTrigger) {
let Some(name) = create_trigger.name() else {
return;
};

let trigger_name = Name::from_node(&name);
let name_ptr = SyntaxNodePtr::new(name.syntax());

let Some(table_path) = create_trigger.on_table().and_then(|on| on.path()) else {
return;
};

let Some(table_name) = item_name(&table_path) else {
return;
};

let Some(schema) = schema_name(b, &table_path, false) else {
return;
};

let trigger_id = b.symbols.alloc(Symbol {
kind: SymbolKind::Trigger,
ptr: name_ptr,
schema: Some(schema),
params: None,
table: Some(table_name),
});

let root = b.root_scope();
b.scopes[root].insert(trigger_name, trigger_id);
}

fn bind_create_tablespace(b: &mut Binder, create_tablespace: ast::CreateTablespace) {
let Some(name) = create_tablespace.name() else {
return;
Expand All @@ -487,6 +559,7 @@ fn bind_create_tablespace(b: &mut Binder, create_tablespace: ast::CreateTablespa
ptr: name_ptr,
schema: None,
params: None,
table: None,
});

let root = b.root_scope();
Expand All @@ -506,6 +579,7 @@ fn bind_create_database(b: &mut Binder, create_database: ast::CreateDatabase) {
ptr: name_ptr,
schema: None,
params: None,
table: None,
});

let root = b.root_scope();
Expand All @@ -525,6 +599,7 @@ fn bind_create_server(b: &mut Binder, create_server: ast::CreateServer) {
ptr: name_ptr,
schema: None,
params: None,
table: None,
});

let root = b.root_scope();
Expand All @@ -544,6 +619,7 @@ fn bind_create_extension(b: &mut Binder, create_extension: ast::CreateExtension)
ptr: name_ptr,
schema: None,
params: None,
table: None,
});

let root = b.root_scope();
Expand All @@ -563,6 +639,7 @@ fn bind_declare_cursor(b: &mut Binder, declare: ast::Declare) {
ptr: name_ptr,
schema: None,
params: None,
table: None,
});

let root = b.root_scope();
Expand All @@ -582,6 +659,7 @@ fn bind_prepare(b: &mut Binder, prepare: ast::Prepare) {
ptr: name_ptr,
schema: None,
params: None,
table: None,
});

let root = b.root_scope();
Expand Down
26 changes: 26 additions & 0 deletions crates/squawk_ide/src/classify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use squawk_syntax::{
#[derive(Debug)]
pub(crate) enum NameRefClass {
DropTable,
DropForeignTable,
Table,
DropIndex,
DropType,
DropView,
DropMaterializedView,
DropSequence,
DropTrigger,
SequenceOwnedByColumn,
Tablespace,
DropDatabase,
Expand Down Expand Up @@ -94,6 +96,8 @@ pub(crate) enum NameRefClass {
NamedArgParameter,
Cursor,
PreparedStatement,
TriggerFunctionCall,
TriggerProcedureCall,
}

fn is_special_fn(kind: SyntaxKind) -> bool {
Expand Down Expand Up @@ -369,6 +373,9 @@ pub(crate) fn classify_name_ref(name_ref: &ast::NameRef) -> Option<NameRefClass>
if ast::DropTable::can_cast(ancestor.kind()) {
return Some(NameRefClass::DropTable);
}
if ast::DropForeignTable::can_cast(ancestor.kind()) {
return Some(NameRefClass::DropForeignTable);
}
if ast::Truncate::can_cast(ancestor.kind()) {
return Some(NameRefClass::TruncateTable);
}
Expand All @@ -387,6 +394,9 @@ pub(crate) fn classify_name_ref(name_ref: &ast::NameRef) -> Option<NameRefClass>
if ast::AlterTable::can_cast(ancestor.kind()) {
return Some(NameRefClass::AlterTable);
}
if ast::OnTable::can_cast(ancestor.kind()) {
return Some(NameRefClass::Table);
}
if ast::AttachPartition::can_cast(ancestor.kind()) {
return Some(NameRefClass::AttachPartition);
}
Expand Down Expand Up @@ -428,6 +438,9 @@ pub(crate) fn classify_name_ref(name_ref: &ast::NameRef) -> Option<NameRefClass>
if ast::DropSequence::can_cast(ancestor.kind()) {
return Some(NameRefClass::DropSequence);
}
if ast::DropTrigger::can_cast(ancestor.kind()) {
return Some(NameRefClass::DropTrigger);
}
if ast::DropDatabase::can_cast(ancestor.kind()) {
return Some(NameRefClass::DropDatabase);
}
Expand Down Expand Up @@ -573,6 +586,15 @@ pub(crate) fn classify_name_ref(name_ref: &ast::NameRef) -> Option<NameRefClass>
}
return Some(NameRefClass::CreateIndex);
}
if let Some(create_trigger) = ast::CreateTrigger::cast(ancestor.clone())
&& in_call_expr
&& !in_arg_list
{
if create_trigger.procedure_token().is_some() {
return Some(NameRefClass::TriggerProcedureCall);
}
return Some(NameRefClass::TriggerFunctionCall);
}
if in_partition_item && ast::CreateTableLike::can_cast(ancestor.kind()) {
return Some(NameRefClass::PartitionByColumn);
}
Expand Down Expand Up @@ -713,6 +735,7 @@ pub(crate) enum NameClass {
WithTable(ast::WithTable),
CreateIndex(ast::CreateIndex),
CreateSequence(ast::CreateSequence),
CreateTrigger(ast::CreateTrigger),
CreateTablespace(ast::CreateTablespace),
CreateDatabase(ast::CreateDatabase),
CreateServer(ast::CreateServer),
Expand Down Expand Up @@ -756,6 +779,9 @@ pub(crate) fn classify_name(name: &ast::Name) -> Option<NameClass> {
if let Some(create_sequence) = ast::CreateSequence::cast(ancestor.clone()) {
return Some(NameClass::CreateSequence(create_sequence));
}
if let Some(create_trigger) = ast::CreateTrigger::cast(ancestor.clone()) {
return Some(NameClass::CreateTrigger(create_trigger));
}
if let Some(create_tablespace) = ast::CreateTablespace::cast(ancestor.clone()) {
return Some(NameClass::CreateTablespace(create_tablespace));
}
Expand Down
Loading
Loading