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
1 change: 1 addition & 0 deletions crates/squawk_ide/src/classify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ pub(crate) fn classify_name_ref(name_ref: &ast::NameRef) -> Option<NameRefClass>
}
}
if let Some(references_constraint) = ast::ReferencesConstraint::cast(ancestor.clone()) {
// TODO: the ast is too flat here
if let Some(column_ref) = references_constraint.column()
&& column_ref
.syntax()
Expand Down
2 changes: 1 addition & 1 deletion crates/squawk_ide/src/expand_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ mod tests {
let root = file.syntax();

let mut range = TextRange::empty(offset);
let mut results = Vec::new();
let mut results = vec![];

for _ in 0..20 {
let new_range = extend_selection(root, range);
Expand Down
10 changes: 6 additions & 4 deletions crates/squawk_ide/src/find_references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::offsets::token_from_offset;
use crate::resolve;
use rowan::{TextRange, TextSize};
use smallvec::{SmallVec, smallvec};
use squawk_syntax::SyntaxNode;
use squawk_syntax::{
SyntaxNodePtr,
ast::{self, AstNode},
Expand All @@ -11,17 +12,17 @@ use squawk_syntax::{

pub fn find_references(file: &ast::SourceFile, offset: TextSize) -> Vec<TextRange> {
let binder = binder::bind(file);
let Some(targets) = find_targets(file, offset, &binder) else {
let root = file.syntax();
let Some(targets) = find_targets(file, root, offset, &binder) else {
return vec![];
};

let mut refs = vec![];

for node in file.syntax().descendants() {
match_ast! {
match node {
ast::NameRef(name_ref) => {
if let Some(found_refs) = resolve::resolve_name_ref(&binder, &name_ref)
if let Some(found_refs) = resolve::resolve_name_ref(&binder, root, &name_ref)
&& found_refs.iter().any(|ptr| targets.contains(ptr))
{
refs.push(name_ref.syntax().text_range());
Expand All @@ -44,6 +45,7 @@ pub fn find_references(file: &ast::SourceFile, offset: TextSize) -> Vec<TextRang

fn find_targets(
file: &ast::SourceFile,
root: &SyntaxNode,
offset: TextSize,
binder: &Binder,
) -> Option<SmallVec<[SyntaxNodePtr; 1]>> {
Expand All @@ -55,7 +57,7 @@ fn find_targets(
}

if let Some(name_ref) = ast::NameRef::cast(parent.clone()) {
return resolve::resolve_name_ref(binder, &name_ref);
return resolve::resolve_name_ref(binder, root, &name_ref);
}

None
Expand Down
127 changes: 126 additions & 1 deletion crates/squawk_ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ pub fn goto_definition(file: ast::SourceFile, offset: TextSize) -> SmallVec<[Tex

if let Some(name_ref) = ast::NameRef::cast(parent.clone()) {
let binder_output = binder::bind(&file);
if let Some(ptrs) = resolve::resolve_name_ref(&binder_output, &name_ref) {
let root = file.syntax();
if let Some(ptrs) = resolve::resolve_name_ref(&binder_output, root, &name_ref) {
return ptrs
.iter()
.map(|ptr| ptr.to_node(file.syntax()).text_range())
Expand Down Expand Up @@ -243,6 +244,34 @@ drop table t$0;
");
}

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

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

#[test]
fn goto_drop_sequence() {
assert_snapshot!(goto("
Expand Down Expand Up @@ -2415,6 +2444,32 @@ select a$0 from x;
");
}

#[test]
fn goto_cte_qualified_column_prefers_cte_over_table() {
assert_snapshot!(goto("
create table u(id int, b int);
with u as (select 1 id, 2 b)
select u.id$0 from u;
"), @r"
╭▸
3 │ with u as (select 1 id, 2 b)
│ ── 2. destination
4 │ select u.id from u;
╰╴ ─ 1. source
");
}

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

#[test]
fn goto_cte_multiple_columns() {
assert_snapshot!(goto("
Expand Down Expand Up @@ -2474,6 +2529,50 @@ select a$0 from y;
");
}

#[test]
fn goto_cte_qualified_star_join_column() {
assert_snapshot!(goto("
create table u(id int, b int);
create table t(id int, a int);

with k as (
select u.* from t join u on a = b
)
select b$0 from k;
"), @r"
╭▸
2 │ create table u(id int, b int);
│ ─ 2. destination
8 │ select b from k;
╰╴ ─ 1. source
");
}

#[test]
fn goto_cte_qualified_star_join_column_with_partial_column_list() {
assert_snapshot!(goto("
with
u as (
select 1 id, 2 b
),
t as (
select 1 id, 2 a
),
k(x) as (
select u.* from t join u on a = b
)
select b$0 from k;
"), @r"
╭▸
4 │ select 1 id, 2 b
│ ─ 2. destination
12 │ select b from k;
╰╴ ─ 1. source
");
}

#[test]
fn goto_cte_reference_inside_cte() {
assert_snapshot!(goto("
Expand Down Expand Up @@ -2614,6 +2713,32 @@ select a$0 from (select * from foo.t);
");
}

#[test]
fn goto_subquery_column_qualified_star_join() {
assert_snapshot!(goto("
create table t(a int);
create table u(b int);
select b$0 from (select u.* from t join u on a = b);
"), @r"
╭▸
3 │ create table u(b int);
│ ─ 2. destination
4 │ select b from (select u.* from t join u on a = b);
╰╴ ─ 1. source
");
}

#[test]
fn goto_subquery_column_qualified_star_join_not_found() {
goto_not_found(
"
create table t(a int);
create table u(b int);
select a$0 from (select u.* from t join u on a = b);
",
);
}

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