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
28 changes: 28 additions & 0 deletions crates/squawk_ide/src/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ fn bind_stmt(b: &mut Binder, stmt: ast::Stmt) {
ast::Stmt::CreateProcedure(create_procedure) => bind_create_procedure(b, create_procedure),
ast::Stmt::CreateSchema(create_schema) => bind_create_schema(b, create_schema),
ast::Stmt::CreateType(create_type) => bind_create_type(b, create_type),
ast::Stmt::CreateView(create_view) => bind_create_view(b, create_view),
ast::Stmt::Set(set) => bind_set(b, set),
_ => {}
}
Expand Down Expand Up @@ -265,6 +266,33 @@ fn bind_create_type(b: &mut Binder, create_type: ast::CreateType) {
b.scopes[root].insert(type_name, type_id);
}

fn bind_create_view(b: &mut Binder, create_view: ast::CreateView) {
let Some(path) = create_view.path() else {
return;
};

let Some(view_name) = item_name(&path) else {
return;
};

let name_ptr = path_to_ptr(&path);
let is_temp = create_view.temp_token().is_some() || create_view.temporary_token().is_some();

let Some(schema) = schema_name(b, &path, is_temp) else {
return;
};

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

let root = b.root_scope();
b.scopes[root].insert(view_name, view_id);
}

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

Expand Down
203 changes: 203 additions & 0 deletions crates/squawk_ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,209 @@ drop type int4_range$0;
");
}

#[test]
fn goto_drop_view() {
assert_snapshot!(goto("
create view v as select 1;
drop view v$0;
"), @r"
╭▸
2 │ create view v as select 1;
│ ─ 2. destination
3 │ drop view v;
╰╴ ─ 1. source
");
}

#[test]
fn goto_drop_view_with_schema() {
assert_snapshot!(goto("
create view public.v as select 1;
drop view v$0;
"), @r"
╭▸
2 │ create view public.v as select 1;
│ ─ 2. destination
3 │ drop view v;
╰╴ ─ 1. source
");

assert_snapshot!(goto("
create view foo.v as select 1;
drop view foo.v$0;
"), @r"
╭▸
2 │ create view foo.v as select 1;
│ ─ 2. destination
3 │ drop view foo.v;
╰╴ ─ 1. source
");

goto_not_found(
"
create view v as select 1;
drop view foo.v$0;
",
);
}

#[test]
fn goto_drop_temp_view() {
assert_snapshot!(goto("
create temp view v as select 1;
drop view v$0;
"), @r"
╭▸
2 │ create temp view v as select 1;
│ ─ 2. destination
3 │ drop view v;
╰╴ ─ 1. source
");
}

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

#[test]
fn goto_select_from_view_with_schema() {
assert_snapshot!(goto("
create view public.v as select 1;
select * from public.v$0;
"), @r"
╭▸
2 │ create view public.v as select 1;
│ ─ 2. destination
3 │ select * from public.v;
╰╴ ─ 1. source
");
}

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

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

#[test]
fn goto_view_with_explicit_column_list() {
assert_snapshot!(goto("
create view v(col1) as select 1;
select * from v$0;
"), @r"
╭▸
2 │ create view v(col1) as select 1;
│ ─ 2. destination
3 │ select * from v;
╰╴ ─ 1. source
");
}

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

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

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

#[test]
fn goto_view_column_from_table() {
assert_snapshot!(goto("
create table t(x int, y int);
create view v as select x, y from t;
select x$0 from v;
"), @r"
╭▸
3 │ create view v as select x, y from t;
│ ─ 2. destination
4 │ select x from v;
╰╴ ─ 1. source
");
}

#[test]
fn goto_view_column_with_table_preference() {
assert_snapshot!(goto("
create table v(a int);
create view vw as select 1 as a;
select a$0 from v;
"), @r"
╭▸
2 │ create table v(a int);
│ ─ 2. destination
3 │ create view vw as select 1 as a;
4 │ select a from v;
╰╴ ─ 1. source
");
}

#[test]
fn goto_cast_operator() {
assert_snapshot!(goto("
Expand Down
Loading
Loading