Skip to content

Commit 58f2d34

Browse files
authored
ide: goto def with create view (#802)
1 parent d2f9e27 commit 58f2d34

File tree

6 files changed

+376
-36
lines changed

6 files changed

+376
-36
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ fn bind_stmt(b: &mut Binder, stmt: ast::Stmt) {
8484
ast::Stmt::CreateProcedure(create_procedure) => bind_create_procedure(b, create_procedure),
8585
ast::Stmt::CreateSchema(create_schema) => bind_create_schema(b, create_schema),
8686
ast::Stmt::CreateType(create_type) => bind_create_type(b, create_type),
87+
ast::Stmt::CreateView(create_view) => bind_create_view(b, create_view),
8788
ast::Stmt::Set(set) => bind_set(b, set),
8889
_ => {}
8990
}
@@ -265,6 +266,33 @@ fn bind_create_type(b: &mut Binder, create_type: ast::CreateType) {
265266
b.scopes[root].insert(type_name, type_id);
266267
}
267268

269+
fn bind_create_view(b: &mut Binder, create_view: ast::CreateView) {
270+
let Some(path) = create_view.path() else {
271+
return;
272+
};
273+
274+
let Some(view_name) = item_name(&path) else {
275+
return;
276+
};
277+
278+
let name_ptr = path_to_ptr(&path);
279+
let is_temp = create_view.temp_token().is_some() || create_view.temporary_token().is_some();
280+
281+
let Some(schema) = schema_name(b, &path, is_temp) else {
282+
return;
283+
};
284+
285+
let view_id = b.symbols.alloc(Symbol {
286+
kind: SymbolKind::View,
287+
ptr: name_ptr,
288+
schema,
289+
params: None,
290+
});
291+
292+
let root = b.root_scope();
293+
b.scopes[root].insert(view_name, view_id);
294+
}
295+
268296
fn item_name(path: &ast::Path) -> Option<Name> {
269297
let segment = path.segment()?;
270298

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

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,209 @@ drop type int4_range$0;
457457
");
458458
}
459459

460+
#[test]
461+
fn goto_drop_view() {
462+
assert_snapshot!(goto("
463+
create view v as select 1;
464+
drop view v$0;
465+
"), @r"
466+
β•­β–Έ
467+
2 β”‚ create view v as select 1;
468+
β”‚ ─ 2. destination
469+
3 β”‚ drop view v;
470+
β•°β•΄ ─ 1. source
471+
");
472+
}
473+
474+
#[test]
475+
fn goto_drop_view_with_schema() {
476+
assert_snapshot!(goto("
477+
create view public.v as select 1;
478+
drop view v$0;
479+
"), @r"
480+
β•­β–Έ
481+
2 β”‚ create view public.v as select 1;
482+
β”‚ ─ 2. destination
483+
3 β”‚ drop view v;
484+
β•°β•΄ ─ 1. source
485+
");
486+
487+
assert_snapshot!(goto("
488+
create view foo.v as select 1;
489+
drop view foo.v$0;
490+
"), @r"
491+
β•­β–Έ
492+
2 β”‚ create view foo.v as select 1;
493+
β”‚ ─ 2. destination
494+
3 β”‚ drop view foo.v;
495+
β•°β•΄ ─ 1. source
496+
");
497+
498+
goto_not_found(
499+
"
500+
create view v as select 1;
501+
drop view foo.v$0;
502+
",
503+
);
504+
}
505+
506+
#[test]
507+
fn goto_drop_temp_view() {
508+
assert_snapshot!(goto("
509+
create temp view v as select 1;
510+
drop view v$0;
511+
"), @r"
512+
β•­β–Έ
513+
2 β”‚ create temp view v as select 1;
514+
β”‚ ─ 2. destination
515+
3 β”‚ drop view v;
516+
β•°β•΄ ─ 1. source
517+
");
518+
}
519+
520+
#[test]
521+
fn goto_select_from_view() {
522+
assert_snapshot!(goto("
523+
create view v as select 1;
524+
select * from v$0;
525+
"), @r"
526+
β•­β–Έ
527+
2 β”‚ create view v as select 1;
528+
β”‚ ─ 2. destination
529+
3 β”‚ select * from v;
530+
β•°β•΄ ─ 1. source
531+
");
532+
}
533+
534+
#[test]
535+
fn goto_select_from_view_with_schema() {
536+
assert_snapshot!(goto("
537+
create view public.v as select 1;
538+
select * from public.v$0;
539+
"), @r"
540+
β•­β–Έ
541+
2 β”‚ create view public.v as select 1;
542+
β”‚ ─ 2. destination
543+
3 β”‚ select * from public.v;
544+
β•°β•΄ ─ 1. source
545+
");
546+
}
547+
548+
#[test]
549+
fn goto_view_column() {
550+
assert_snapshot!(goto("
551+
create view v as select 1 as a;
552+
select a$0 from v;
553+
"), @r"
554+
β•­β–Έ
555+
2 β”‚ create view v as select 1 as a;
556+
β”‚ ─ 2. destination
557+
3 β”‚ select a from v;
558+
β•°β•΄ ─ 1. source
559+
");
560+
}
561+
562+
#[test]
563+
fn goto_view_column_qualified() {
564+
assert_snapshot!(goto("
565+
create view v as select 1 as a;
566+
select v.a$0 from v;
567+
"), @r"
568+
β•­β–Έ
569+
2 β”‚ create view v as select 1 as a;
570+
β”‚ ─ 2. destination
571+
3 β”‚ select v.a from v;
572+
β•°β•΄ ─ 1. source
573+
");
574+
}
575+
576+
#[test]
577+
fn goto_view_with_explicit_column_list() {
578+
assert_snapshot!(goto("
579+
create view v(col1) as select 1;
580+
select * from v$0;
581+
"), @r"
582+
β•­β–Έ
583+
2 β”‚ create view v(col1) as select 1;
584+
β”‚ ─ 2. destination
585+
3 β”‚ select * from v;
586+
β•°β•΄ ─ 1. source
587+
");
588+
}
589+
590+
#[test]
591+
fn goto_view_column_with_explicit_column_list() {
592+
assert_snapshot!(goto("
593+
create view v(col1) as select 1;
594+
select col1$0 from v;
595+
"), @r"
596+
β•­β–Έ
597+
2 β”‚ create view v(col1) as select 1;
598+
β”‚ ──── 2. destination
599+
3 β”‚ select col1 from v;
600+
β•°β•΄ ─ 1. source
601+
");
602+
}
603+
604+
#[test]
605+
fn goto_view_column_with_schema() {
606+
assert_snapshot!(goto("
607+
create view public.v as select 1 as a;
608+
select a$0 from public.v;
609+
"), @r"
610+
β•­β–Έ
611+
2 β”‚ create view public.v as select 1 as a;
612+
β”‚ ─ 2. destination
613+
3 β”‚ select a from public.v;
614+
β•°β•΄ ─ 1. source
615+
");
616+
}
617+
618+
#[test]
619+
fn goto_view_multiple_columns() {
620+
assert_snapshot!(goto("
621+
create view v as select 1 as a, 2 as b;
622+
select b$0 from v;
623+
"), @r"
624+
β•­β–Έ
625+
2 β”‚ create view v as select 1 as a, 2 as b;
626+
β”‚ ─ 2. destination
627+
3 β”‚ select b from v;
628+
β•°β•΄ ─ 1. source
629+
");
630+
}
631+
632+
#[test]
633+
fn goto_view_column_from_table() {
634+
assert_snapshot!(goto("
635+
create table t(x int, y int);
636+
create view v as select x, y from t;
637+
select x$0 from v;
638+
"), @r"
639+
β•­β–Έ
640+
3 β”‚ create view v as select x, y from t;
641+
β”‚ ─ 2. destination
642+
4 β”‚ select x from v;
643+
β•°β•΄ ─ 1. source
644+
");
645+
}
646+
647+
#[test]
648+
fn goto_view_column_with_table_preference() {
649+
assert_snapshot!(goto("
650+
create table v(a int);
651+
create view vw as select 1 as a;
652+
select a$0 from v;
653+
"), @r"
654+
β•­β–Έ
655+
2 β”‚ create table v(a int);
656+
β”‚ ─ 2. destination
657+
3 β”‚ create view vw as select 1 as a;
658+
4 β”‚ select a from v;
659+
β•°β•΄ ─ 1. source
660+
");
661+
}
662+
460663
#[test]
461664
fn goto_cast_operator() {
462665
assert_snapshot!(goto("

0 commit comments

Comments
Β (0)