Skip to content

Commit 553a2b0

Browse files
authored
ide: add hover to cte table & columns (#784)
1 parent 8768011 commit 553a2b0

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

crates/squawk_ide/src/hover.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ fn hover_column(
108108
let root = file.syntax();
109109
let column_name_node = column_ptr.to_node(root);
110110

111+
if let Some(with_table) = column_name_node.ancestors().find_map(ast::WithTable::cast) {
112+
let cte_name = with_table.name()?.syntax().text().to_string();
113+
let column_name = column_name_node.text().to_string();
114+
return Some(format!("column {}.{}", cte_name, column_name));
115+
}
116+
111117
let column = column_name_node.ancestors().find_map(ast::Column::cast)?;
112118
let column_name = column.name()?.syntax().text().to_string();
113119
let ty = column.ty()?;
@@ -167,6 +173,10 @@ fn hover_table(
167173
let root = file.syntax();
168174
let table_name_node = table_ptr.to_node(root);
169175

176+
if let Some(with_table) = table_name_node.ancestors().find_map(ast::WithTable::cast) {
177+
return format_with_table(&with_table);
178+
}
179+
170180
let create_table = table_name_node
171181
.ancestors()
172182
.find_map(ast::CreateTable::cast)?;
@@ -208,6 +218,12 @@ fn format_create_table(create_table: &ast::CreateTable, binder: &binder::Binder)
208218
Some(format!("table {}.{}{}", schema, table_name, args))
209219
}
210220

221+
fn format_with_table(with_table: &ast::WithTable) -> Option<String> {
222+
let name = with_table.name()?.syntax().text().to_string();
223+
let query = with_table.query()?.syntax().text().to_string();
224+
Some(format!("with {} as ({})", name, query))
225+
}
226+
211227
fn table_schema(create_table: &ast::CreateTable, binder: &binder::Binder) -> Option<String> {
212228
let is_temp = create_table.temp_token().is_some() || create_table.temporary_token().is_some();
213229
if is_temp {
@@ -1499,4 +1515,70 @@ create schema foo;
14991515
╰╴ ─ hover
15001516
");
15011517
}
1518+
1519+
#[test]
1520+
fn hover_on_cte_table() {
1521+
assert_snapshot!(check_hover("
1522+
with t as (select 1 a)
1523+
select a from t$0;
1524+
"), @r"
1525+
hover: with t as (select 1 a)
1526+
╭▸
1527+
3 │ select a from t;
1528+
╰╴ ─ hover
1529+
");
1530+
}
1531+
1532+
#[test]
1533+
fn hover_on_cte_column() {
1534+
assert_snapshot!(check_hover("
1535+
with t as (select 1 a)
1536+
select a$0 from t;
1537+
"), @r"
1538+
hover: column t.a
1539+
╭▸
1540+
3 │ select a from t;
1541+
╰╴ ─ hover
1542+
");
1543+
}
1544+
1545+
#[test]
1546+
fn hover_on_cte_with_multiple_columns() {
1547+
assert_snapshot!(check_hover("
1548+
with t as (select 1 a, 2 b)
1549+
select b$0 from t;
1550+
"), @r"
1551+
hover: column t.b
1552+
╭▸
1553+
3 │ select b from t;
1554+
╰╴ ─ hover
1555+
");
1556+
}
1557+
1558+
#[test]
1559+
fn hover_on_cte_with_column_list() {
1560+
assert_snapshot!(check_hover("
1561+
with t(a) as (select 1)
1562+
select a$0 from t;
1563+
"), @r"
1564+
hover: column t.a
1565+
╭▸
1566+
3 │ select a from t;
1567+
╰╴ ─ hover
1568+
");
1569+
}
1570+
1571+
#[test]
1572+
fn hover_on_nested_cte() {
1573+
assert_snapshot!(check_hover("
1574+
with x as (select 1 a),
1575+
y as (select a from x)
1576+
select a$0 from y;
1577+
"), @r"
1578+
hover: column y.a
1579+
╭▸
1580+
4 │ select a from y;
1581+
╰╴ ─ hover
1582+
");
1583+
}
15021584
}

0 commit comments

Comments
 (0)