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
46 changes: 42 additions & 4 deletions crates/squawk_ide/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ fn bind_stmt(b: &mut Binder, stmt: ast::Stmt) {
ast::Stmt::CreateSchema(create_schema) => bind_create_schema(b, create_schema),
ast::Stmt::CreateType(create_type) => bind_create_type(b, create_type),
ast::Stmt::CreateView(create_view) => bind_create_view(b, create_view),
ast::Stmt::CreateMaterializedView(create_view) => {
bind_create_materialized_view(b, create_view)
}
ast::Stmt::Set(set) => bind_set(b, set),
_ => {}
}
Expand Down Expand Up @@ -222,13 +225,22 @@ fn bind_create_procedure(b: &mut Binder, create_procedure: ast::CreateProcedure)
}

fn bind_create_schema(b: &mut Binder, create_schema: ast::CreateSchema) {
let Some(schema_name_node) = create_schema.name() else {
let (schema_name, name_ptr) = if let Some(schema_name_node) = create_schema.name() {
let schema_name = Name::from_node(&schema_name_node);
let name_ptr = SyntaxNodePtr::new(schema_name_node.syntax());
(schema_name, name_ptr)
} else if let Some(schema_name_ref) = create_schema
.schema_authorization()
.and_then(|authorization| authorization.role())
.and_then(|role| role.name_ref())
{
let schema_name = Name::from_node(&schema_name_ref);
let name_ptr = SyntaxNodePtr::new(schema_name_ref.syntax());
(schema_name, name_ptr)
} else {
return;
};

let schema_name = Name::from_node(&schema_name_node);
let name_ptr = SyntaxNodePtr::new(schema_name_node.syntax());

let schema_id = b.symbols.alloc(Symbol {
kind: SymbolKind::Schema,
ptr: name_ptr,
Expand Down Expand Up @@ -293,6 +305,32 @@ fn bind_create_view(b: &mut Binder, create_view: ast::CreateView) {
b.scopes[root].insert(view_name, view_id);
}

fn bind_create_materialized_view(b: &mut Binder, create_view: ast::CreateMaterializedView) {
let Some(path) = create_view.path() else {
return;
};

let Some(view_name) = item_name(&path) else {
return;
};

let name_ptr = path_to_ptr(&path);

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

let view_id = b.symbols.alloc(Symbol {
kind: SymbolKind::View,
ptr: name_ptr,
schema,
params: None,
});

let root = b.root_scope();
b.scopes[root].insert(view_name, view_id);
}

fn item_name(path: &ast::Path) -> Option<Name> {
let segment = path.segment()?;

Expand Down
15 changes: 15 additions & 0 deletions crates/squawk_ide/src/classify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ pub(crate) enum NameRefClass {
DropIndex,
DropType,
DropView,
DropMaterializedView,
DropFunction,
DropAggregate,
DropProcedure,
DropRoutine,
CallProcedure,
DropSchema,
CreateSchema,
CreateIndex,
CreateIndexColumn,
SelectFunctionCall,
Expand Down Expand Up @@ -139,13 +141,17 @@ pub(crate) fn classify_name_ref(name_ref: &ast::NameRef) -> Option<NameRefClass>
}

let mut in_type = false;
let mut in_schema_authorization = false;
for ancestor in name_ref.syntax().ancestors() {
if ast::PathType::can_cast(ancestor.kind()) || ast::ExprType::can_cast(ancestor.kind()) {
in_type = true;
}
if in_type {
return Some(NameRefClass::TypeReference);
}
if ast::SchemaAuthorization::can_cast(ancestor.kind()) {
in_schema_authorization = true;
}
if ast::DropTable::can_cast(ancestor.kind()) {
return Some(NameRefClass::DropTable);
}
Expand All @@ -161,6 +167,9 @@ pub(crate) fn classify_name_ref(name_ref: &ast::NameRef) -> Option<NameRefClass>
if ast::DropView::can_cast(ancestor.kind()) {
return Some(NameRefClass::DropView);
}
if ast::DropMaterializedView::can_cast(ancestor.kind()) {
return Some(NameRefClass::DropMaterializedView);
}
if ast::CastExpr::can_cast(ancestor.kind()) && in_type {
return Some(NameRefClass::TypeReference);
}
Expand All @@ -182,6 +191,12 @@ pub(crate) fn classify_name_ref(name_ref: &ast::NameRef) -> Option<NameRefClass>
if ast::DropSchema::can_cast(ancestor.kind()) {
return Some(NameRefClass::DropSchema);
}
if in_schema_authorization
&& let Some(create_schema) = ast::CreateSchema::cast(ancestor.clone())
&& create_schema.name().is_none()
{
return Some(NameRefClass::CreateSchema);
}
if ast::PartitionItem::can_cast(ancestor.kind()) {
in_partition_item = true;
}
Expand Down
153 changes: 152 additions & 1 deletion crates/squawk_ide/src/document_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ use squawk_syntax::ast::{self, AstNode};

use crate::binder::{self, extract_string_literal};
use crate::resolve::{
resolve_function_info, resolve_table_info, resolve_type_info, resolve_view_info,
resolve_aggregate_info, resolve_function_info, resolve_materialized_view_info,
resolve_procedure_info, resolve_table_info, resolve_type_info, resolve_view_info,
};

#[derive(Debug)]
pub enum DocumentSymbolKind {
Table,
View,
MaterializedView,
Function,
Aggregate,
Procedure,
Type,
Enum,
Column,
Expand Down Expand Up @@ -46,6 +50,16 @@ pub fn document_symbols(file: &ast::SourceFile) -> Vec<DocumentSymbol> {
symbols.push(symbol);
}
}
ast::Stmt::CreateAggregate(create_aggregate) => {
if let Some(symbol) = create_aggregate_symbol(&binder, create_aggregate) {
symbols.push(symbol);
}
}
ast::Stmt::CreateProcedure(create_procedure) => {
if let Some(symbol) = create_procedure_symbol(&binder, create_procedure) {
symbols.push(symbol);
}
}
ast::Stmt::CreateType(create_type) => {
if let Some(symbol) = create_type_symbol(&binder, create_type) {
symbols.push(symbol);
Expand All @@ -56,6 +70,11 @@ pub fn document_symbols(file: &ast::SourceFile) -> Vec<DocumentSymbol> {
symbols.push(symbol);
}
}
ast::Stmt::CreateMaterializedView(create_view) => {
if let Some(symbol) = create_materialized_view_symbol(&binder, create_view) {
symbols.push(symbol);
}
}
ast::Stmt::Select(select) => {
symbols.extend(cte_table_symbols(select));
}
Expand Down Expand Up @@ -184,6 +203,39 @@ fn create_view_symbol(
})
}

fn create_materialized_view_symbol(
binder: &binder::Binder,
create_view: ast::CreateMaterializedView,
) -> Option<DocumentSymbol> {
let path = create_view.path()?;
let segment = path.segment()?;
let name_node = segment.name()?;

let (schema, view_name) = resolve_materialized_view_info(binder, &path)?;
let name = format!("{}.{}", schema.0, view_name);

let full_range = create_view.syntax().text_range();
let focus_range = name_node.syntax().text_range();

let mut children = vec![];
if let Some(column_list) = create_view.column_list() {
for column in column_list.columns() {
if let Some(column_symbol) = create_column_symbol(column) {
children.push(column_symbol);
}
}
}

Some(DocumentSymbol {
name,
detail: None,
kind: DocumentSymbolKind::MaterializedView,
full_range,
focus_range,
children,
})
}

fn create_function_symbol(
binder: &binder::Binder,
create_function: ast::CreateFunction,
Expand All @@ -208,6 +260,54 @@ fn create_function_symbol(
})
}

fn create_aggregate_symbol(
binder: &binder::Binder,
create_aggregate: ast::CreateAggregate,
) -> Option<DocumentSymbol> {
let path = create_aggregate.path()?;
let segment = path.segment()?;
let name_node = segment.name()?;

let (schema, aggregate_name) = resolve_aggregate_info(binder, &path)?;
let name = format!("{}.{}", schema.0, aggregate_name);

let full_range = create_aggregate.syntax().text_range();
let focus_range = name_node.syntax().text_range();

Some(DocumentSymbol {
name,
detail: None,
kind: DocumentSymbolKind::Aggregate,
full_range,
focus_range,
children: vec![],
})
}

fn create_procedure_symbol(
binder: &binder::Binder,
create_procedure: ast::CreateProcedure,
) -> Option<DocumentSymbol> {
let path = create_procedure.path()?;
let segment = path.segment()?;
let name_node = segment.name()?;

let (schema, procedure_name) = resolve_procedure_info(binder, &path)?;
let name = format!("{}.{}", schema.0, procedure_name);

let full_range = create_procedure.syntax().text_range();
let focus_range = name_node.syntax().text_range();

Some(DocumentSymbol {
name,
detail: None,
kind: DocumentSymbolKind::Procedure,
full_range,
focus_range,
children: vec![],
})
}

fn create_type_symbol(
binder: &binder::Binder,
create_type: ast::CreateType,
Expand Down Expand Up @@ -327,7 +427,10 @@ mod tests {
let kind = match symbol.kind {
DocumentSymbolKind::Table => "table",
DocumentSymbolKind::View => "view",
DocumentSymbolKind::MaterializedView => "materialized view",
DocumentSymbolKind::Function => "function",
DocumentSymbolKind::Aggregate => "aggregate",
DocumentSymbolKind::Procedure => "procedure",
DocumentSymbolKind::Type => "type",
DocumentSymbolKind::Enum => "enum",
DocumentSymbolKind::Column => "column",
Expand Down Expand Up @@ -443,6 +546,54 @@ create table users (
);
}

#[test]
fn create_materialized_view() {
assert_snapshot!(
symbols("create materialized view reports as select 1;"),
@r"
info: materialized view: public.reports
╭▸
1 │ create materialized view reports as select 1;
│ ┬────────────────────────┯━━━━━━────────────
│ │ │
│ │ focus range
╰╴full range
"
);
}

#[test]
fn create_aggregate() {
assert_snapshot!(
symbols("create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);"),
@r"
info: aggregate: public.myavg
╭▸
1 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
│ ┬────────────────┯━━━━─────────────────────────────────────────────
│ │ │
│ │ focus range
╰╴full range
"
);
}

#[test]
fn create_procedure() {
assert_snapshot!(
symbols("create procedure hello() language sql as $$ select 1; $$;"),
@r"
info: procedure: public.hello
╭▸
1 │ create procedure hello() language sql as $$ select 1; $$;
│ ┬────────────────┯━━━━──────────────────────────────────
│ │ │
│ │ focus range
╰╴full range
"
);
}

#[test]
fn multiple_symbols() {
assert_snapshot!(symbols("
Expand Down
Loading
Loading