Skip to content

Commit be84166

Browse files
authored
ide: fix column name infer & related goto def (#977)
1 parent 2ef51b1 commit be84166

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,18 @@ fn name_from_name_ref(name_ref: ast::NameRef, in_type: bool) -> Option<(ColumnNa
168168
name_ref.syntax().clone(),
169169
));
170170
}
171+
SyntaxKind::BOOLEAN_KW => {
172+
return Some((
173+
ColumnName::Column("bool".to_owned()),
174+
name_ref.syntax().clone(),
175+
));
176+
}
177+
SyntaxKind::DECIMAL_KW => {
178+
return Some((
179+
ColumnName::Column("numeric".to_owned()),
180+
name_ref.syntax().clone(),
181+
));
182+
}
171183
SyntaxKind::INT_KW | SyntaxKind::INTEGER_KW => {
172184
return Some((
173185
ColumnName::Column("int4".to_owned()),
@@ -569,6 +581,8 @@ fn examples() {
569581
assert_snapshot!(name("col_name::text"), @"col_name");
570582
assert_snapshot!(name("col_name::int::text"), @"col_name");
571583
assert_snapshot!(name("'1'::bigint"), @"int8");
584+
assert_snapshot!(name("'1'::decimal"), @"numeric");
585+
assert_snapshot!(name("'1'::boolean"), @"bool");
572586
assert_snapshot!(name("'1'::int"), @"int4");
573587
assert_snapshot!(name("'1'::smallint"), @"int2");
574588
assert_snapshot!(name("'{{1, 2}, {3, 4}}'::bigint[][]"), @"int8");

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,6 +2882,48 @@ select '{[1.234, 5.678]}'::floatmultirangerange$0;
28822882
");
28832883
}
28842884

2885+
#[test]
2886+
fn goto_cast_boolean_falls_back_to_bool() {
2887+
assert_snapshot!(goto("
2888+
create type pg_catalog.bool;
2889+
select '1'::boolean$0;
2890+
"), @"
2891+
β•­β–Έ
2892+
2 β”‚ create type pg_catalog.bool;
2893+
β”‚ ──── 2. destination
2894+
3 β”‚ select '1'::boolean;
2895+
β•°β•΄ ─ 1. source
2896+
");
2897+
}
2898+
2899+
#[test]
2900+
fn goto_cast_decimal_falls_back_to_numeric() {
2901+
assert_snapshot!(goto("
2902+
create type pg_catalog.numeric;
2903+
select 1::decimal$0(10, 2);
2904+
"), @"
2905+
β•­β–Έ
2906+
2 β”‚ create type pg_catalog.numeric;
2907+
β”‚ ─────── 2. destination
2908+
3 β”‚ select 1::decimal(10, 2);
2909+
β•°β•΄ ─ 1. source
2910+
");
2911+
}
2912+
2913+
#[test]
2914+
fn goto_cast_float_falls_back_to_float8() {
2915+
assert_snapshot!(goto("
2916+
create type pg_catalog.float8;
2917+
select 1::float$0;
2918+
"), @"
2919+
β•­β–Έ
2920+
2 β”‚ create type pg_catalog.float8;
2921+
β”‚ ────── 2. destination
2922+
3 β”‚ select 1::float;
2923+
β•°β•΄ ─ 1. source
2924+
");
2925+
}
2926+
28852927
#[test]
28862928
fn goto_cast_bigint_falls_back_to_int8() {
28872929
assert_snapshot!(goto("
@@ -2896,6 +2938,20 @@ select 1::bigint$0;
28962938
");
28972939
}
28982940

2941+
#[test]
2942+
fn goto_cast_real_falls_back_to_float4() {
2943+
assert_snapshot!(goto("
2944+
create type pg_catalog.float4;
2945+
select 1::real$0;
2946+
"), @"
2947+
β•­β–Έ
2948+
2 β”‚ create type pg_catalog.float4;
2949+
β”‚ ────── 2. destination
2950+
3 β”‚ select 1::real;
2951+
β•°β•΄ ─ 1. source
2952+
");
2953+
}
2954+
28992955
#[test]
29002956
fn goto_cast_bigint_prefers_user_type() {
29012957
assert_snapshot!(goto("

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,11 @@ fn type_name_and_schema_from_type(ty: &ast::Type) -> Option<(Name, Option<Schema
607607
fn fallback_type_alias(type_name: &Name) -> Option<Name> {
608608
match type_name.0.as_str() {
609609
"bigint" | "bigserial" | "serial8" => Some(Name::from_string("int8")),
610+
"boolean" => Some(Name::from_string("bool")),
611+
"decimal" => Some(Name::from_string("numeric")),
612+
"float" => Some(Name::from_string("float8")),
610613
"int" | "integer" | "serial" | "serial4" => Some(Name::from_string("int4")),
614+
"real" => Some(Name::from_string("float4")),
611615
"smallint" | "smallserial" | "serial2" => Some(Name::from_string("int2")),
612616
_ => None,
613617
}

0 commit comments

Comments
Β (0)