Skip to content

Commit 1a1dc1f

Browse files
authored
ide: support goto def on schema (#774)
1 parent 5eb5114 commit 1a1dc1f

File tree

5 files changed

+698
-1
lines changed

5 files changed

+698
-1
lines changed

β€Žcrates/squawk_ide/src/binder.rsβ€Ž

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ fn bind_stmt(b: &mut Binder, stmt: ast::Stmt) {
8080
ast::Stmt::CreateTable(create_table) => bind_create_table(b, create_table),
8181
ast::Stmt::CreateIndex(create_index) => bind_create_index(b, create_index),
8282
ast::Stmt::CreateFunction(create_function) => bind_create_function(b, create_function),
83+
ast::Stmt::CreateSchema(create_schema) => bind_create_schema(b, create_schema),
8384
ast::Stmt::Set(set) => bind_set(b, set),
8485
_ => {}
8586
}
@@ -155,6 +156,24 @@ fn bind_create_function(b: &mut Binder, create_function: ast::CreateFunction) {
155156
b.scopes[root].insert(function_name, function_id);
156157
}
157158

159+
fn bind_create_schema(b: &mut Binder, create_schema: ast::CreateSchema) {
160+
let Some(schema_name_node) = create_schema.name() else {
161+
return;
162+
};
163+
164+
let schema_name = Name::new(schema_name_node.syntax().text().to_string());
165+
let name_ptr = SyntaxNodePtr::new(schema_name_node.syntax());
166+
167+
let schema_id = b.symbols.alloc(Symbol {
168+
kind: SymbolKind::Schema,
169+
ptr: name_ptr,
170+
schema: Schema(schema_name.clone()),
171+
});
172+
173+
let root = b.root_scope();
174+
b.scopes[root].insert(schema_name, schema_id);
175+
}
176+
158177
fn item_name(path: &ast::Path) -> Option<Name> {
159178
let segment = path.segment()?;
160179

β€Žcrates/squawk_ide/src/goto_definition.rsβ€Ž

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,4 +1397,284 @@ select id$0 from users;
13971397
β•°β•΄ ─ 1. source
13981398
");
13991399
}
1400+
1401+
#[test]
1402+
fn goto_select_table_as_column() {
1403+
assert_snapshot!(goto("
1404+
create table t(x bigint, y bigint);
1405+
select t$0 from t;
1406+
"), @r"
1407+
β•­β–Έ
1408+
2 β”‚ create table t(x bigint, y bigint);
1409+
β”‚ ─ 2. destination
1410+
3 β”‚ select t from t;
1411+
β•°β•΄ ─ 1. source
1412+
");
1413+
}
1414+
1415+
#[test]
1416+
fn goto_select_table_as_column_with_schema() {
1417+
assert_snapshot!(goto("
1418+
create table public.t(x bigint, y bigint);
1419+
select t$0 from public.t;
1420+
"), @r"
1421+
β•­β–Έ
1422+
2 β”‚ create table public.t(x bigint, y bigint);
1423+
β”‚ ─ 2. destination
1424+
3 β”‚ select t from public.t;
1425+
β•°β•΄ ─ 1. source
1426+
");
1427+
}
1428+
1429+
#[test]
1430+
fn goto_select_table_as_column_with_search_path() {
1431+
assert_snapshot!(goto("
1432+
set search_path to foo;
1433+
create table foo.users(id int, email text);
1434+
select users$0 from users;
1435+
"), @r"
1436+
β•­β–Έ
1437+
3 β”‚ create table foo.users(id int, email text);
1438+
β”‚ ───── 2. destination
1439+
4 β”‚ select users from users;
1440+
β•°β•΄ ─ 1. source
1441+
");
1442+
}
1443+
1444+
#[test]
1445+
fn goto_select_column_with_same_name_as_table() {
1446+
assert_snapshot!(goto("
1447+
create table t(t int);
1448+
select t$0 from t;
1449+
"), @r"
1450+
β•­β–Έ
1451+
2 β”‚ create table t(t int);
1452+
β”‚ ─ 2. destination
1453+
3 β”‚ select t from t;
1454+
β•°β•΄ ─ 1. source
1455+
");
1456+
}
1457+
1458+
#[test]
1459+
fn goto_drop_schema() {
1460+
assert_snapshot!(goto("
1461+
create schema foo;
1462+
drop schema foo$0;
1463+
"), @r"
1464+
β•­β–Έ
1465+
2 β”‚ create schema foo;
1466+
β”‚ ─── 2. destination
1467+
3 β”‚ drop schema foo;
1468+
β•°β•΄ ─ 1. source
1469+
");
1470+
}
1471+
1472+
#[test]
1473+
fn goto_drop_schema_defined_after() {
1474+
assert_snapshot!(goto("
1475+
drop schema foo$0;
1476+
create schema foo;
1477+
"), @r"
1478+
β•­β–Έ
1479+
2 β”‚ drop schema foo;
1480+
β”‚ ─ 1. source
1481+
3 β”‚ create schema foo;
1482+
β•°β•΄ ─── 2. destination
1483+
");
1484+
}
1485+
1486+
#[test]
1487+
fn goto_schema_qualifier_in_table() {
1488+
assert_snapshot!(goto("
1489+
create schema foo;
1490+
create table foo$0.t(a int);
1491+
"), @r"
1492+
β•­β–Έ
1493+
2 β”‚ create schema foo;
1494+
β”‚ ─── 2. destination
1495+
3 β”‚ create table foo.t(a int);
1496+
β•°β•΄ ─ 1. source
1497+
");
1498+
}
1499+
1500+
#[test]
1501+
fn goto_schema_qualifier_in_drop_table() {
1502+
assert_snapshot!(goto("
1503+
create schema foo;
1504+
create table foo.t(a int);
1505+
drop table foo$0.t;
1506+
"), @r"
1507+
β•­β–Έ
1508+
2 β”‚ create schema foo;
1509+
β”‚ ─── 2. destination
1510+
3 β”‚ create table foo.t(a int);
1511+
4 β”‚ drop table foo.t;
1512+
β•°β•΄ ─ 1. source
1513+
");
1514+
}
1515+
1516+
#[test]
1517+
fn goto_schema_qualifier_multiple_schemas() {
1518+
assert_snapshot!(goto("
1519+
create schema foo;
1520+
create schema bar;
1521+
create table bar$0.t(a int);
1522+
"), @r"
1523+
β•­β–Έ
1524+
3 β”‚ create schema bar;
1525+
β”‚ ─── 2. destination
1526+
4 β”‚ create table bar.t(a int);
1527+
β•°β•΄ ─ 1. source
1528+
");
1529+
}
1530+
1531+
#[test]
1532+
fn goto_schema_qualifier_in_function_call() {
1533+
assert_snapshot!(goto(r#"
1534+
create schema foo;
1535+
create function foo.bar() returns int as $$ begin return 1; end; $$ language plpgsql;
1536+
select foo$0.bar();
1537+
"#), @r"
1538+
β•­β–Έ
1539+
2 β”‚ create schema foo;
1540+
β”‚ ─── 2. destination
1541+
3 β”‚ create function foo.bar() returns int as $$ begin return 1; end; $$ language plpgsql;
1542+
4 β”‚ select foo.bar();
1543+
β•°β•΄ ─ 1. source
1544+
");
1545+
}
1546+
1547+
#[test]
1548+
fn goto_schema_qualifier_in_function_call_from_clause() {
1549+
assert_snapshot!(goto(r#"
1550+
create schema myschema;
1551+
create function myschema.get_data() returns table(id int) as $$ begin return query select 1; end; $$ language plpgsql;
1552+
select * from myschema$0.get_data();
1553+
"#), @r"
1554+
β•­β–Έ
1555+
2 β”‚ create schema myschema;
1556+
β”‚ ──────── 2. destination
1557+
3 β”‚ create function myschema.get_data() returns table(id int) as $$ begin return query select 1; end; $$ language plpgsql;
1558+
4 β”‚ select * from myschema.get_data();
1559+
β•°β•΄ ─ 1. source
1560+
");
1561+
}
1562+
1563+
#[test]
1564+
fn goto_schema_qualifier_in_select_from() {
1565+
assert_snapshot!(goto("
1566+
create schema foo;
1567+
create table foo.t(x int);
1568+
select x from foo$0.t;
1569+
"), @r"
1570+
β•­β–Έ
1571+
2 β”‚ create schema foo;
1572+
β”‚ ─── 2. destination
1573+
3 β”‚ create table foo.t(x int);
1574+
4 β”‚ select x from foo.t;
1575+
β•°β•΄ ─ 1. source
1576+
");
1577+
}
1578+
1579+
#[test]
1580+
fn goto_qualified_column_table() {
1581+
assert_snapshot!(goto("
1582+
create table t(a int);
1583+
select t$0.a from t;
1584+
"), @r"
1585+
β•­β–Έ
1586+
2 β”‚ create table t(a int);
1587+
β”‚ ─ 2. destination
1588+
3 β”‚ select t.a from t;
1589+
β•°β•΄ ─ 1. source
1590+
");
1591+
}
1592+
1593+
#[test]
1594+
fn goto_qualified_column_column() {
1595+
assert_snapshot!(goto("
1596+
create table t(a int);
1597+
select t.a$0 from t;
1598+
"), @r"
1599+
β•­β–Έ
1600+
2 β”‚ create table t(a int);
1601+
β”‚ ─ 2. destination
1602+
3 β”‚ select t.a from t;
1603+
β•°β•΄ ─ 1. source
1604+
");
1605+
}
1606+
1607+
#[test]
1608+
fn goto_three_part_qualified_column_schema() {
1609+
assert_snapshot!(goto("
1610+
create schema foo;
1611+
create table foo.t(a int);
1612+
select foo$0.t.a from t;
1613+
"), @r"
1614+
β•­β–Έ
1615+
2 β”‚ create schema foo;
1616+
β”‚ ─── 2. destination
1617+
3 β”‚ create table foo.t(a int);
1618+
4 β”‚ select foo.t.a from t;
1619+
β•°β•΄ ─ 1. source
1620+
");
1621+
}
1622+
1623+
#[test]
1624+
fn goto_three_part_qualified_column_table() {
1625+
assert_snapshot!(goto("
1626+
create schema foo;
1627+
create table foo.t(a int);
1628+
select foo.t$0.a from t;
1629+
"), @r"
1630+
β•­β–Έ
1631+
3 β”‚ create table foo.t(a int);
1632+
β”‚ ─ 2. destination
1633+
4 β”‚ select foo.t.a from t;
1634+
β•°β•΄ ─ 1. source
1635+
");
1636+
}
1637+
1638+
#[test]
1639+
fn goto_three_part_qualified_column_column() {
1640+
assert_snapshot!(goto("
1641+
create schema foo;
1642+
create table foo.t(a int);
1643+
select foo.t.a$0 from t;
1644+
"), @r"
1645+
β•­β–Έ
1646+
3 β”‚ create table foo.t(a int);
1647+
β”‚ ─ 2. destination
1648+
4 β”‚ select foo.t.a from t;
1649+
β•°β•΄ ─ 1. source
1650+
");
1651+
}
1652+
1653+
#[test]
1654+
fn goto_qualified_column_with_schema_in_from_table() {
1655+
assert_snapshot!(goto("
1656+
create table foo.t(a int, b int);
1657+
select t$0.a from foo.t;
1658+
"), @r"
1659+
β•­β–Έ
1660+
2 β”‚ create table foo.t(a int, b int);
1661+
β”‚ ─ 2. destination
1662+
3 β”‚ select t.a from foo.t;
1663+
β•°β•΄ ─ 1. source
1664+
");
1665+
}
1666+
1667+
#[test]
1668+
fn goto_qualified_column_with_schema_in_from_column() {
1669+
assert_snapshot!(goto("
1670+
create table foo.t(a int, b int);
1671+
select t.a$0 from foo.t;
1672+
"), @r"
1673+
β•­β–Έ
1674+
2 β”‚ create table foo.t(a int, b int);
1675+
β”‚ ─ 2. destination
1676+
3 β”‚ select t.a from foo.t;
1677+
β•°β•΄ ─ 1. source
1678+
");
1679+
}
14001680
}

0 commit comments

Comments
Β (0)