Skip to content

Commit a672bab

Browse files
authored
ide: goto def support for tablespace (#814)
1 parent 436add0 commit a672bab

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

crates/squawk_ide/src/binder.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ fn bind_stmt(b: &mut Binder, stmt: ast::Stmt) {
8989
bind_create_materialized_view(b, create_view)
9090
}
9191
ast::Stmt::CreateSequence(create_sequence) => bind_create_sequence(b, create_sequence),
92+
ast::Stmt::CreateTablespace(create_tablespace) => {
93+
bind_create_tablespace(b, create_tablespace)
94+
}
9295
ast::Stmt::Set(set) => bind_set(b, set),
9396
_ => {}
9497
}
@@ -360,6 +363,27 @@ fn bind_create_sequence(b: &mut Binder, create_sequence: ast::CreateSequence) {
360363
b.scopes[root].insert(sequence_name, sequence_id);
361364
}
362365

366+
fn bind_create_tablespace(b: &mut Binder, create_tablespace: ast::CreateTablespace) {
367+
let Some(name) = create_tablespace.name() else {
368+
return;
369+
};
370+
371+
let tablespace_name = Name::from_node(&name);
372+
let name_ptr = SyntaxNodePtr::new(name.syntax());
373+
374+
let tablespace_id = b.symbols.alloc(Symbol {
375+
kind: SymbolKind::Tablespace,
376+
ptr: name_ptr,
377+
// TODO: tablespaces don't actually have schemas so we should think
378+
// about this more
379+
schema: Schema::new("pg_tablespace"),
380+
params: None,
381+
});
382+
383+
let root = b.root_scope();
384+
b.scopes[root].insert(tablespace_name, tablespace_id);
385+
}
386+
363387
fn item_name(path: &ast::Path) -> Option<Name> {
364388
let segment = path.segment()?;
365389

crates/squawk_ide/src/classify.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub(crate) enum NameRefClass {
99
DropView,
1010
DropMaterializedView,
1111
DropSequence,
12+
Tablespace,
1213
ForeignKeyTable,
1314
ForeignKeyColumn,
1415
ForeignKeyLocalColumn,
@@ -190,6 +191,13 @@ pub(crate) fn classify_name_ref(name_ref: &ast::NameRef) -> Option<NameRefClass>
190191
if ast::DropSequence::can_cast(ancestor.kind()) {
191192
return Some(NameRefClass::DropSequence);
192193
}
194+
if ast::DropTablespace::can_cast(ancestor.kind())
195+
|| ast::Tablespace::can_cast(ancestor.kind())
196+
|| ast::SetTablespace::can_cast(ancestor.kind())
197+
|| ast::ConstraintIndexTablespace::can_cast(ancestor.kind())
198+
{
199+
return Some(NameRefClass::Tablespace);
200+
}
193201
if let Some(foreign_key) = ast::ForeignKeyConstraint::cast(ancestor.clone()) {
194202
if in_column_list {
195203
// TODO: ast is too "flat" here, we need a unique node for to
@@ -362,6 +370,7 @@ pub(crate) enum NameClass {
362370
WithTable(ast::WithTable),
363371
CreateIndex(ast::CreateIndex),
364372
CreateSequence(ast::CreateSequence),
373+
CreateTablespace(ast::CreateTablespace),
365374
CreateType(ast::CreateType),
366375
CreateFunction(ast::CreateFunction),
367376
CreateAggregate(ast::CreateAggregate),
@@ -399,6 +408,9 @@ pub(crate) fn classify_name(name: &ast::Name) -> Option<NameClass> {
399408
if let Some(create_sequence) = ast::CreateSequence::cast(ancestor.clone()) {
400409
return Some(NameClass::CreateSequence(create_sequence));
401410
}
411+
if let Some(create_tablespace) = ast::CreateTablespace::cast(ancestor.clone()) {
412+
return Some(NameClass::CreateTablespace(create_tablespace));
413+
}
402414
if let Some(create_type) = ast::CreateType::cast(ancestor.clone()) {
403415
return Some(NameClass::CreateType(create_type));
404416
}

crates/squawk_ide/src/goto_definition.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,34 @@ drop sequence s$0;
246246
");
247247
}
248248

249+
#[test]
250+
fn goto_drop_tablespace() {
251+
assert_snapshot!(goto("
252+
create tablespace ts location '/tmp/ts';
253+
drop tablespace ts$0;
254+
"), @r"
255+
╭▸
256+
2 │ create tablespace ts location '/tmp/ts';
257+
│ ── 2. destination
258+
3 │ drop tablespace ts;
259+
╰╴ ─ 1. source
260+
");
261+
}
262+
263+
#[test]
264+
fn goto_create_table_tablespace() {
265+
assert_snapshot!(goto("
266+
create tablespace bar location '/tmp/ts';
267+
create table t (a int) tablespace b$0ar;
268+
"), @r"
269+
╭▸
270+
2 │ create tablespace bar location '/tmp/ts';
271+
│ ─── 2. destination
272+
3 │ create table t (a int) tablespace bar;
273+
╰╴ ─ 1. source
274+
");
275+
}
276+
249277
#[test]
250278
fn goto_drop_sequence_with_schema() {
251279
assert_snapshot!(goto("

crates/squawk_ide/src/hover.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub fn hover(file: &ast::SourceFile, offset: TextSize) -> Option<String> {
6767
return hover_column(file, &name_ref, &binder);
6868
}
6969
NameRefClass::DropSequence => return hover_sequence(file, &name_ref, &binder),
70+
NameRefClass::Tablespace => return hover_tablespace(file, &name_ref, &binder),
7071
NameRefClass::DropIndex => return hover_index(file, &name_ref, &binder),
7172
NameRefClass::DropFunction => return hover_function(file, &name_ref, &binder),
7273
NameRefClass::DropAggregate => return hover_aggregate(file, &name_ref, &binder),
@@ -107,6 +108,9 @@ pub fn hover(file: &ast::SourceFile, offset: TextSize) -> Option<String> {
107108
NameClass::CreateSequence(create_sequence) => {
108109
return format_create_sequence(&create_sequence, &binder);
109110
}
111+
NameClass::CreateTablespace(create_tablespace) => {
112+
return format_create_tablespace(&create_tablespace);
113+
}
110114
NameClass::CreateType(create_type) => {
111115
return format_create_type(&create_type, &binder);
112116
}
@@ -322,6 +326,17 @@ fn hover_sequence(
322326
format_create_sequence(&create_sequence, binder)
323327
}
324328

329+
fn hover_tablespace(
330+
file: &ast::SourceFile,
331+
name_ref: &ast::NameRef,
332+
binder: &binder::Binder,
333+
) -> Option<String> {
334+
let tablespace_ptr = resolve::resolve_name_ref(binder, name_ref)?;
335+
let root = file.syntax();
336+
let tablespace_name_node = tablespace_ptr.to_node(root);
337+
Some(format!("tablespace {}", tablespace_name_node.text()))
338+
}
339+
325340
fn hover_type(
326341
file: &ast::SourceFile,
327342
name_ref: &ast::NameRef,
@@ -474,6 +489,11 @@ fn format_create_sequence(
474489
Some(format!("sequence {}.{}", schema, sequence_name))
475490
}
476491

492+
fn format_create_tablespace(create_tablespace: &ast::CreateTablespace) -> Option<String> {
493+
let name = create_tablespace.name()?.syntax().text().to_string();
494+
Some(format!("tablespace {}", name))
495+
}
496+
477497
fn index_schema(create_index: &ast::CreateIndex, binder: &binder::Binder) -> Option<String> {
478498
let position = create_index.syntax().text_range().start();
479499
let search_path = binder.search_path_at(position);

crates/squawk_ide/src/resolve.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ pub(crate) fn resolve_name_ref(binder: &Binder, name_ref: &ast::NameRef) -> Opti
9898
let position = name_ref.syntax().text_range().start();
9999
resolve_sequence(binder, &sequence_name, &schema, position)
100100
}
101+
NameRefClass::Tablespace => {
102+
let tablespace_name = Name::from_node(name_ref);
103+
resolve_tablespace(binder, &tablespace_name)
104+
}
101105
NameRefClass::ForeignKeyTable => {
102106
let foreign_key = name_ref
103107
.syntax()
@@ -420,6 +424,15 @@ fn resolve_sequence(
420424
)
421425
}
422426

427+
fn resolve_tablespace(binder: &Binder, tablespace_name: &Name) -> Option<SyntaxNodePtr> {
428+
let symbols = binder.scopes[binder.root_scope()].get(tablespace_name)?;
429+
let symbol_id = symbols.iter().copied().find(|id| {
430+
let symbol = &binder.symbols[*id];
431+
symbol.kind == SymbolKind::Tablespace
432+
})?;
433+
Some(binder.symbols[symbol_id].ptr)
434+
}
435+
423436
fn resolve_for_kind(
424437
binder: &Binder,
425438
name: &Name,

crates/squawk_ide/src/symbols.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub(crate) enum SymbolKind {
5353
Type,
5454
View,
5555
Sequence,
56+
Tablespace,
5657
}
5758

5859
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)