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
227 changes: 227 additions & 0 deletions crates/squawk_ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,44 @@ select a$0 from t;
");
}

#[test]
fn goto_cte_with_partial_column_list() {
assert_snapshot!(goto("
with t(x) as (select 1 as a, 2 as b)
select b$0 from t;
"), @r"
╭▸
2 │ with t(x) as (select 1 as a, 2 as b)
│ ─ 2. destination
3 │ select b from t;
╰╴ ─ 1. source
");
}

#[test]
fn goto_cte_with_partial_column_list_renamed() {
assert_snapshot!(goto("
with t(x) as (select 1 as a, 2 as b)
select x$0 from t;
"), @r"
╭▸
2 │ with t(x) as (select 1 as a, 2 as b)
│ ─ 2. destination
3 │ select x from t;
╰╴ ─ 1. source
");
}

#[test]
fn goto_cte_column_list_overwrites_column() {
goto_not_found(
"
with t(x) as (select 1 as a)
select a$0 from t;
",
);
}

#[test]
fn goto_cte_shadows_table() {
assert_snapshot!(goto("
Expand All @@ -1438,6 +1476,50 @@ select a from t;
");
}

#[test]
fn goto_subquery_column() {
assert_snapshot!(goto("
select a$0 from (select 1 a);
"), @r"
╭▸
2 │ select a from (select 1 a);
╰╴ ─ 1. source ─ 2. destination
");
}

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

#[test]
fn goto_subquery_column_multiple_columns() {
assert_snapshot!(goto("
select b$0 from (select 1 a, 2 b);
"), @r"
╭▸
2 │ select b from (select 1 a, 2 b);
╰╴ ─ 1. source ─ 2. destination
");
}

#[test]
fn goto_subquery_column_nested_parens() {
assert_snapshot!(goto("
select a$0 from ((select 1 a));
"), @r"
╭▸
2 │ select a from ((select 1 a));
╰╴ ─ 1. source ─ 2. destination
");
}

#[test]
fn goto_insert_table() {
assert_snapshot!(goto("
Expand Down Expand Up @@ -2046,6 +2128,94 @@ select foo.t.a$0 from t;
");
}

#[test]
fn goto_cte_values_column1() {
assert_snapshot!(goto("
with t as (
values (1, 2), (3, 4)
)
select column1$0, column2 from t;
"), @r"
╭▸
3 │ values (1, 2), (3, 4)
│ ─ 2. destination
4 │ )
5 │ select column1, column2 from t;
╰╴ ─ 1. source
");
}

#[test]
fn goto_cte_values_column2() {
assert_snapshot!(goto("
with t as (
values (1, 2), (3, 4)
)
select column1, column2$0 from t;
"), @r"
╭▸
3 │ values (1, 2), (3, 4)
│ ─ 2. destination
4 │ )
5 │ select column1, column2 from t;
╰╴ ─ 1. source
");
}

#[test]
fn goto_cte_values_single_column() {
assert_snapshot!(goto("
with t as (
values (1), (2), (3)
)
select column1$0 from t;
"), @r"
╭▸
3 │ values (1), (2), (3)
│ ─ 2. destination
4 │ )
5 │ select column1 from t;
╰╴ ─ 1. source
");
}

#[test]
fn goto_cte_values_multiple_rows() {
assert_snapshot!(goto("
with t as (
values
(1, 2, 3),
(4, 5, 6),
(7, 8, 9)
)
select column3$0 from t;
"), @r"
╭▸
4 │ (1, 2, 3),
│ ─ 2. destination
8 │ select column3 from t;
╰╴ ─ 1. source
");
}

#[test]
fn goto_cte_values_uppercase_column_names() {
assert_snapshot!(goto("
with t as (
values (1, 2), (3, 4)
)
select COLUMN1$0, COLUMN2 from t;
"), @r"
╭▸
3 │ values (1, 2), (3, 4)
│ ─ 2. destination
4 │ )
5 │ select COLUMN1, COLUMN2 from t;
╰╴ ─ 1. source
");
}

#[test]
fn goto_qualified_column_with_schema_in_from_table() {
assert_snapshot!(goto("
Expand Down Expand Up @@ -2073,4 +2243,61 @@ select t.a$0 from foo.t;
╰╴ ─ 1. source
");
}

#[test]
fn goto_cte_union_all_column() {
assert_snapshot!(goto("
with t as (
select 1 as a, 2 as b
union all
select 3, 4
)
select a$0, b from t;
"), @r"
╭▸
3 │ select 1 as a, 2 as b
│ ─ 2. destination
7 │ select a, b from t;
╰╴ ─ 1. source
");
}

#[test]
fn goto_cte_union_all_column_second() {
assert_snapshot!(goto("
with t as (
select 1 as a, 2 as b
union all
select 3, 4
)
select a, b$0 from t;
"), @r"
╭▸
3 │ select 1 as a, 2 as b
│ ─ 2. destination
7 │ select a, b from t;
╰╴ ─ 1. source
");
}

#[test]
fn goto_cte_union_column() {
assert_snapshot!(goto("
with t as (
select 1 as a, 2 as b
union
select 3, 4
)
select a$0 from t;
"), @r"
╭▸
3 │ select 1 as a, 2 as b
│ ─ 2. destination
7 │ select a from t;
╰╴ ─ 1. source
");
}
}
88 changes: 86 additions & 2 deletions crates/squawk_ide/src/hover.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::binder;
use crate::offsets::token_from_offset;
use crate::resolve;
use crate::{binder, symbols::Name};
use rowan::TextSize;
use squawk_syntax::ast::{self, AstNode};

Expand Down Expand Up @@ -69,6 +69,10 @@ pub fn hover(file: &ast::SourceFile, offset: TextSize) -> Option<String> {
return format_create_table(&create_table, &binder);
}

if let Some(with_table) = name.syntax().parent().and_then(ast::WithTable::cast) {
return format_with_table(&with_table);
}

if let Some(create_index) = name.syntax().ancestors().find_map(ast::CreateIndex::cast) {
return format_create_index(&create_index, &binder);
}
Expand Down Expand Up @@ -110,7 +114,14 @@ fn hover_column(

if let Some(with_table) = column_name_node.ancestors().find_map(ast::WithTable::cast) {
let cte_name = with_table.name()?.syntax().text().to_string();
let column_name = column_name_node.text().to_string();
let column_name = if column_name_node
.ancestors()
.any(|a| ast::Values::can_cast(a.kind()))
{
Name::new(name_ref.syntax().text().to_string())
} else {
Name::new(column_name_node.text().to_string())
};
return Some(format!("column {}.{}", cte_name, column_name));
}

Expand Down Expand Up @@ -1595,4 +1606,77 @@ select a$0 from t;
╰╴ ─ hover
");
}

#[test]
fn hover_on_cte_definition() {
assert_snapshot!(check_hover("
with t$0 as (select 1 a)
select a from t;
"), @r"
hover: with t as (select 1 a)
╭▸
2 │ with t as (select 1 a)
╰╴ ─ hover
");
}

#[test]
fn hover_on_cte_values_column1() {
assert_snapshot!(check_hover("
with t as (
values (1, 2), (3, 4)
)
select column1$0, column2 from t;
"), @r"
hover: column t.column1
╭▸
5 │ select column1, column2 from t;
╰╴ ─ hover
");
}

#[test]
fn hover_on_cte_values_column2() {
assert_snapshot!(check_hover("
with t as (
values (1, 2), (3, 4)
)
select column1, column2$0 from t;
"), @r"
hover: column t.column2
╭▸
5 │ select column1, column2 from t;
╰╴ ─ hover
");
}

#[test]
fn hover_on_cte_values_single_column() {
assert_snapshot!(check_hover("
with t as (
values (1), (2), (3)
)
select column1$0 from t;
"), @r"
hover: column t.column1
╭▸
5 │ select column1 from t;
╰╴ ─ hover
");
}

#[test]
fn hover_on_cte_values_uppercase_column_names() {
assert_snapshot!(check_hover("
with t as (
values (1, 2), (3, 4)
)
select COLUMN1$0, COLUMN2 from t;
"), @r"
hover: column t.column1
╭▸
5 │ select COLUMN1, COLUMN2 from t;
╰╴ ─ hover
");
}
}
Loading
Loading