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
143 changes: 143 additions & 0 deletions crates/squawk_ide/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub fn hover(file: &ast::SourceFile, offset: TextSize) -> Option<String> {

let binder = binder::bind(file);

// TODO: can we use the classify_name_ref_context function from goto def here?
if let Some(name_ref) = ast::NameRef::cast(parent.clone()) {
if is_column_ref(&name_ref) {
return hover_column(file, &name_ref, &binder);
Expand All @@ -36,6 +37,10 @@ pub fn hover(file: &ast::SourceFile, offset: TextSize) -> Option<String> {
return hover_table(file, &name_ref, &binder);
}

if is_update_from_table(&name_ref) {
return hover_table(file, &name_ref, &binder);
}

if is_index_ref(&name_ref) {
return hover_index(file, &name_ref, &binder);
}
Expand Down Expand Up @@ -306,6 +311,7 @@ fn is_column_ref(name_ref: &ast::NameRef) -> bool {
let mut in_partition_item = false;
let mut in_column_list = false;
let mut in_where_clause = false;
let mut in_set_clause = false;

for ancestor in name_ref.syntax().ancestors() {
if ast::PartitionItem::can_cast(ancestor.kind()) {
Expand All @@ -323,9 +329,15 @@ fn is_column_ref(name_ref: &ast::NameRef) -> bool {
if ast::WhereClause::can_cast(ancestor.kind()) {
in_where_clause = true;
}
if ast::SetClause::can_cast(ancestor.kind()) {
in_set_clause = true;
}
if ast::Delete::can_cast(ancestor.kind()) {
return in_where_clause;
}
if ast::Update::can_cast(ancestor.kind()) {
return in_where_clause || in_set_clause;
}
}
false
}
Expand All @@ -334,6 +346,8 @@ fn is_table_ref(name_ref: &ast::NameRef) -> bool {
let mut in_partition_item = false;
let mut in_column_list = false;
let mut in_where_clause = false;
let mut in_set_clause = false;
let mut in_from_clause = false;

for ancestor in name_ref.syntax().ancestors() {
if ast::DropTable::can_cast(ancestor.kind()) {
Expand All @@ -351,9 +365,18 @@ fn is_table_ref(name_ref: &ast::NameRef) -> bool {
if ast::WhereClause::can_cast(ancestor.kind()) {
in_where_clause = true;
}
if ast::SetClause::can_cast(ancestor.kind()) {
in_set_clause = true;
}
if ast::FromClause::can_cast(ancestor.kind()) {
in_from_clause = true;
}
if ast::Delete::can_cast(ancestor.kind()) {
return !in_where_clause;
}
if ast::Update::can_cast(ancestor.kind()) {
return !in_where_clause && !in_set_clause && !in_from_clause;
}
if ast::DropIndex::can_cast(ancestor.kind()) {
return false;
}
Expand Down Expand Up @@ -453,6 +476,20 @@ fn is_select_from_table(name_ref: &ast::NameRef) -> bool {
false
}

fn is_update_from_table(name_ref: &ast::NameRef) -> bool {
let mut in_from_clause = false;

for ancestor in name_ref.syntax().ancestors() {
if ast::FromClause::can_cast(ancestor.kind()) {
in_from_clause = true;
}
if ast::Update::can_cast(ancestor.kind()) && in_from_clause {
return true;
}
}
false
}

fn is_select_column(name_ref: &ast::NameRef) -> bool {
let mut in_call_expr = false;
let mut in_arg_list = false;
Expand Down Expand Up @@ -2237,4 +2274,110 @@ drop routine foo$0(int);
╰╴ ─ hover
");
}

#[test]
fn hover_on_update_table() {
assert_snapshot!(check_hover("
create table users(id int, email text);
update users$0 set email = 'new@example.com';
"), @r"
hover: table public.users(id int, email text)
╭▸
3 │ update users set email = 'new@example.com';
╰╴ ─ hover
");
}

#[test]
fn hover_on_update_table_with_schema() {
assert_snapshot!(check_hover("
create table public.users(id int, email text);
update public.users$0 set email = 'new@example.com';
"), @r"
hover: table public.users(id int, email text)
╭▸
3 │ update public.users set email = 'new@example.com';
╰╴ ─ hover
");
}

#[test]
fn hover_on_update_set_column() {
assert_snapshot!(check_hover("
create table users(id int, email text);
update users set email$0 = 'new@example.com' where id = 1;
"), @r"
hover: column public.users.email text
╭▸
3 │ update users set email = 'new@example.com' where id = 1;
╰╴ ─ hover
");
}

#[test]
fn hover_on_update_set_column_with_schema() {
assert_snapshot!(check_hover("
create table public.users(id int, email text);
update public.users set email$0 = 'new@example.com' where id = 1;
"), @r"
hover: column public.users.email text
╭▸
3 │ update public.users set email = 'new@example.com' where id = 1;
╰╴ ─ hover
");
}

#[test]
fn hover_on_update_where_column() {
assert_snapshot!(check_hover("
create table users(id int, email text);
update users set email = 'new@example.com' where id$0 = 1;
"), @r"
hover: column public.users.id int
╭▸
3 │ update users set email = 'new@example.com' where id = 1;
╰╴ ─ hover
");
}

#[test]
fn hover_on_update_where_column_with_schema() {
assert_snapshot!(check_hover("
create table public.users(id int, email text);
update public.users set email = 'new@example.com' where id$0 = 1;
"), @r"
hover: column public.users.id int
╭▸
3 │ update public.users set email = 'new@example.com' where id = 1;
╰╴ ─ hover
");
}

#[test]
fn hover_on_update_from_table() {
assert_snapshot!(check_hover("
create table users(id int, email text);
create table messages(id int, user_id int, email text);
update users set email = messages.email from messages$0 where users.id = messages.user_id;
"), @r"
hover: table public.messages(id int, user_id int, email text)
╭▸
4 │ update users set email = messages.email from messages where users.id = messages.user_id;
╰╴ ─ hover
");
}

#[test]
fn hover_on_update_from_table_with_schema() {
assert_snapshot!(check_hover("
create table users(id int, email text);
create table public.messages(id int, user_id int, email text);
update users set email = messages.email from public.messages$0 where users.id = messages.user_id;
"), @r"
hover: table public.messages(id int, user_id int, email text)
╭▸
4 │ update users set email = messages.email from public.messages where users.id = messages.user_id;
╰╴ ─ hover
");
}
}
14 changes: 7 additions & 7 deletions crates/squawk_ide/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,22 @@ fn inlay_hint_insert(
let row_list = values.row_list()?;

let columns: Vec<(Name, Option<TextRange>)> = if let Some(column_list) = insert.column_list() {
let table_arg_list = resolve::resolve_insert_table_columns(file, binder, &insert);

let create_table = resolve::resolve_insert_create_table(file, binder, &insert);
column_list
.columns()
.filter_map(|col| {
let col_name = resolve::extract_column_name(&col)?;
let target = table_arg_list
let target = create_table
.as_ref()
.and_then(|list| resolve::find_column_in_table(list, &col_name));
.and_then(|x| resolve::find_column_in_create_table(x, &col_name))
.map(|x| x.text_range());
Some((col_name, target))
})
.collect()
} else {
let table_arg_list = resolve::resolve_insert_table_columns(file, binder, &insert)?;

table_arg_list
let create_table = resolve::resolve_insert_create_table(file, binder, &insert)?;
create_table
.table_arg_list()?
.args()
.filter_map(|arg| {
if let ast::TableArg::Column(column) = arg
Expand Down
Loading
Loading