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
20 changes: 20 additions & 0 deletions crates/squawk_ide/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ fn bind_stmt(b: &mut Binder, stmt: ast::Stmt) {
ast::Stmt::CreateServer(create_server) => bind_create_server(b, create_server),
ast::Stmt::CreateExtension(create_extension) => bind_create_extension(b, create_extension),
ast::Stmt::Declare(declare) => bind_declare_cursor(b, declare),
ast::Stmt::Prepare(prepare) => bind_prepare(b, prepare),
ast::Stmt::Set(set) => bind_set(b, set),
_ => {}
}
Expand Down Expand Up @@ -568,6 +569,25 @@ fn bind_declare_cursor(b: &mut Binder, declare: ast::Declare) {
b.scopes[root].insert(cursor_name, cursor_id);
}

fn bind_prepare(b: &mut Binder, prepare: ast::Prepare) {
let Some(name) = prepare.name() else {
return;
};

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

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

let root = b.root_scope();
b.scopes[root].insert(statement_name, statement_id);
}

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

Expand Down
8 changes: 8 additions & 0 deletions crates/squawk_ide/src/classify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub(crate) enum NameRefClass {
AttachPartition,
NamedArgParameter,
Cursor,
PreparedStatement,
}

fn is_special_fn(kind: SyntaxKind) -> bool {
Expand Down Expand Up @@ -362,6 +363,9 @@ pub(crate) fn classify_name_ref(name_ref: &ast::NameRef) -> Option<NameRefClass>
{
return Some(NameRefClass::Cursor);
}
if ast::Execute::can_cast(ancestor.kind()) || ast::Deallocate::can_cast(ancestor.kind()) {
return Some(NameRefClass::PreparedStatement);
}
if ast::DropTable::can_cast(ancestor.kind()) {
return Some(NameRefClass::DropTable);
}
Expand Down Expand Up @@ -724,6 +728,7 @@ pub(crate) enum NameClass {
},
CreateView(ast::CreateView),
DeclareCursor(ast::Declare),
PrepareStatement(ast::Prepare),
}

pub(crate) fn classify_name(name: &ast::Name) -> Option<NameClass> {
Expand Down Expand Up @@ -790,6 +795,9 @@ pub(crate) fn classify_name(name: &ast::Name) -> Option<NameClass> {
if let Some(declare) = ast::Declare::cast(ancestor.clone()) {
return Some(NameClass::DeclareCursor(declare));
}
if let Some(prepare) = ast::Prepare::cast(ancestor.clone()) {
return Some(NameClass::PrepareStatement(prepare));
}
}

if let Some(with_table) = with_table_parent {
Expand Down
39 changes: 39 additions & 0 deletions crates/squawk_ide/src/document_symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum DocumentSymbolKind {
Column,
Variant,
Cursor,
PreparedStatement,
}

#[derive(Debug)]
Expand Down Expand Up @@ -87,6 +88,11 @@ pub fn document_symbols(file: &ast::SourceFile) -> Vec<DocumentSymbol> {
symbols.push(symbol);
}
}
ast::Stmt::Prepare(prepare) => {
if let Some(symbol) = create_prepare_symbol(prepare) {
symbols.push(symbol);
}
}
ast::Stmt::Select(select) => {
symbols.extend(cte_table_symbols(select));
}
Expand Down Expand Up @@ -444,6 +450,23 @@ fn create_declare_cursor_symbol(declare: ast::Declare) -> Option<DocumentSymbol>
})
}

fn create_prepare_symbol(prepare: ast::Prepare) -> Option<DocumentSymbol> {
let name_node = prepare.name()?;
let name = name_node.syntax().text().to_string();

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

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

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -494,6 +517,7 @@ mod tests {
DocumentSymbolKind::Column => "column",
DocumentSymbolKind::Variant => "variant",
DocumentSymbolKind::Cursor => "cursor",
DocumentSymbolKind::PreparedStatement => "prepared statement",
};

let title = if let Some(detail) = &symbol.detail {
Expand Down Expand Up @@ -906,6 +930,21 @@ declare c scroll cursor for select * from t;
");
}

#[test]
fn prepare_statement() {
assert_snapshot!(symbols("
prepare stmt as select 1;
"), @r"
info: prepared statement: stmt
╭▸
2 │ prepare stmt as select 1;
│ ┬───────┯━━━────────────
│ │ │
│ │ focus range
╰╴full range
");
}

#[test]
fn empty_file() {
symbols_not_found("")
Expand Down
28 changes: 28 additions & 0 deletions crates/squawk_ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,34 @@ move forward 10 from c$0;
");
}

#[test]
fn goto_execute_prepared_statement() {
assert_snapshot!(goto("
prepare stmt as select 1;
execute stmt$0;
"), @r"
╭▸
2 │ prepare stmt as select 1;
│ ──── 2. destination
3 │ execute stmt;
╰╴ ─ 1. source
");
}

#[test]
fn goto_deallocate_prepared_statement() {
assert_snapshot!(goto("
prepare stmt as select 1;
deallocate stmt$0;
"), @r"
╭▸
2 │ prepare stmt as select 1;
│ ──── 2. destination
3 │ deallocate stmt;
╰╴ ─ 1. source
");
}

#[test]
fn goto_delete_where_current_of_cursor() {
assert_snapshot!(goto("
Expand Down
69 changes: 69 additions & 0 deletions crates/squawk_ide/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ pub fn hover(file: &ast::SourceFile, offset: TextSize) -> Option<String> {
NameRefClass::Cursor => {
return hover_cursor(root, &name_ref, &binder);
}
NameRefClass::PreparedStatement => {
return hover_prepared_statement(root, &name_ref, &binder);
}
}
}

Expand Down Expand Up @@ -225,6 +228,9 @@ pub fn hover(file: &ast::SourceFile, offset: TextSize) -> Option<String> {
NameClass::DeclareCursor(declare) => {
return format_declare_cursor(&declare);
}
NameClass::PrepareStatement(prepare) => {
return format_prepare(&prepare);
}
}
}

Expand Down Expand Up @@ -794,6 +800,21 @@ fn hover_cursor(
format_declare_cursor(&declare)
}

fn hover_prepared_statement(
root: &SyntaxNode,
name_ref: &ast::NameRef,
binder: &binder::Binder,
) -> Option<String> {
let statement_ptr = resolve::resolve_name_ref(binder, root, name_ref)?
.into_iter()
.next()?;
let statement_name_node = statement_ptr.to_node(root);
let prepare = statement_name_node
.ancestors()
.find_map(ast::Prepare::cast)?;
format_prepare(&prepare)
}

fn hover_type(
root: &SyntaxNode,
name_ref: &ast::NameRef,
Expand All @@ -820,6 +841,16 @@ fn format_declare_cursor(declare: &ast::Declare) -> Option<String> {
))
}

fn format_prepare(prepare: &ast::Prepare) -> Option<String> {
let name = prepare.name()?;
let stmt = prepare.preparable_stmt()?;
Some(format!(
"prepare {} as {}",
name.syntax().text(),
stmt.syntax().text()
))
}

fn format_create_table(
create_table: &impl ast::HasCreateTable,
binder: &binder::Binder,
Expand Down Expand Up @@ -4013,4 +4044,42 @@ move forward 10 from c$0;
╰╴ ─ hover
");
}

#[test]
fn hover_on_prepare_statement() {
assert_snapshot!(check_hover("
prepare stmt$0 as select 1;
"), @r"
hover: prepare stmt as select 1
╭▸
2 │ prepare stmt as select 1;
╰╴ ─ hover
");
}

#[test]
fn hover_on_execute_prepared_statement() {
assert_snapshot!(check_hover("
prepare stmt as select 1;
execute stmt$0;
"), @r"
hover: prepare stmt as select 1
╭▸
3 │ execute stmt;
╰╴ ─ hover
");
}

#[test]
fn hover_on_deallocate_prepared_statement() {
assert_snapshot!(check_hover("
prepare stmt as select 1;
deallocate stmt$0;
"), @r"
hover: prepare stmt as select 1
╭▸
3 │ deallocate stmt;
╰╴ ─ hover
");
}
}
11 changes: 11 additions & 0 deletions crates/squawk_ide/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ pub(crate) fn resolve_name_ref(
.lookup(&cursor_name, SymbolKind::Cursor)
.map(|ptr| smallvec![ptr])
}
NameRefClass::PreparedStatement => {
let statement_name = Name::from_node(name_ref);
resolve_prepared_statement_name_ptr(binder, &statement_name).map(|ptr| smallvec![ptr])
}
NameRefClass::SelectFromTable
| NameRefClass::UpdateFromTable
| NameRefClass::MergeUsingTable
Expand Down Expand Up @@ -492,6 +496,13 @@ fn resolve_tablespace_name_ptr(binder: &Binder, tablespace_name: &Name) -> Optio
binder.lookup(tablespace_name, SymbolKind::Tablespace)
}

fn resolve_prepared_statement_name_ptr(
binder: &Binder,
statement_name: &Name,
) -> Option<SyntaxNodePtr> {
binder.lookup(statement_name, SymbolKind::PreparedStatement)
}

fn resolve_database_name_ptr(binder: &Binder, database_name: &Name) -> Option<SyntaxNodePtr> {
binder.lookup(database_name, SymbolKind::Database)
}
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_ide/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub(crate) enum SymbolKind {
View,
Sequence,
Cursor,
PreparedStatement,
Tablespace,
Database,
Server,
Expand Down
1 change: 1 addition & 0 deletions crates/squawk_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ fn handle_document_symbol(
DocumentSymbolKind::Column => SymbolKind::FIELD,
DocumentSymbolKind::Variant => SymbolKind::ENUM_MEMBER,
DocumentSymbolKind::Cursor => SymbolKind::VARIABLE,
DocumentSymbolKind::PreparedStatement => SymbolKind::VARIABLE,
},
tags: None,
range,
Expand Down
3 changes: 3 additions & 0 deletions crates/squawk_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ fn convert_document_symbol(
squawk_ide::document_symbols::DocumentSymbolKind::Column => "column",
squawk_ide::document_symbols::DocumentSymbolKind::Variant => "variant",
squawk_ide::document_symbols::DocumentSymbolKind::Cursor => "cursor",
squawk_ide::document_symbols::DocumentSymbolKind::PreparedStatement => {
"prepared_statement"
}
}
.to_string(),
start_line: full_start_wide.line,
Expand Down
Loading