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
31 changes: 13 additions & 18 deletions crates/squawk_ide/src/code_actions/rewrite_timestamp_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,23 @@ pub(super) fn rewrite_timestamp_type(
actions: &mut Vec<CodeAction>,
) -> Option<()> {
let token = token_from_offset(db, position)?;
let time_type = token.parent_ancestors().find_map(ast::TimeType::cast)?;

let replacement = match time_type.timezone()? {
ast::Timezone::WithoutTimezone(_) => {
if time_type.timestamp_token().is_some() {
"timestamp"
} else {
"time"
}
}
ast::Timezone::WithTimezone(_) => {
if time_type.timestamp_token().is_some() {
"timestamptz"
} else {
"timetz"
}
}
let ty = token.parent_ancestors().find_map(ast::Type::cast)?;

let replacement = match &ty {
ast::Type::TimeType(time_type) => match time_type.timezone()? {
ast::Timezone::WithoutTimezone(_) => "time",
ast::Timezone::WithTimezone(_) => "timetz",
},
ast::Type::TimestampType(timestamp_type) => match timestamp_type.timezone()? {
ast::Timezone::WithoutTimezone(_) => "timestamp",
ast::Timezone::WithTimezone(_) => "timestamptz",
},
_ => return None,
};

actions.push(CodeAction {
title: format!("Rewrite as `{replacement}`"),
edits: vec![Edit::replace(time_type.syntax().text_range(), replacement)],
edits: vec![Edit::replace(ty.syntax().text_range(), replacement)],
kind: ActionKind::RefactorRewrite,
});

Expand Down
4 changes: 3 additions & 1 deletion crates/squawk_ide/src/find_references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ fn is_reference_node(node: &SyntaxNode) -> bool {
if let Some(ty) = ast::Type::cast(node.clone()) {
return match ty {
ast::Type::BitType(_)
| ast::Type::BitVaryingType(_)
| ast::Type::VarcharType(_)
| ast::Type::CharacterType(_)
| ast::Type::DoubleType(_)
| ast::Type::IntervalType(_)
| ast::Type::TimeType(_) => true,
| ast::Type::TimeType(_)
| ast::Type::TimestampType(_) => true,
ast::Type::ArrayType(_)
| ast::Type::ExprType(_)
| ast::Type::PathType(_)
Expand Down
2 changes: 1 addition & 1 deletion crates/squawk_ide/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub(crate) fn infer_type_from_expr(expr: &ast::Expr) -> Option<Type> {
pub(crate) fn infer_type_from_ty(ty: &ast::Type) -> Option<Type> {
match ty {
ast::Type::VarcharType(_) | ast::Type::CharacterType(_) => Some(Type::Text),
ast::Type::BitType(_) => Some(Type::Bit),
ast::Type::BitType(_) | ast::Type::BitVaryingType(_) => Some(Type::Bit),
ast::Type::PathType(path_type) => {
let name = path_type.path_ref()?.segment()?;
Some(Type::Other(name.syntax().text().to_string()))
Expand Down
30 changes: 16 additions & 14 deletions crates/squawk_ide/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,8 @@ pub(crate) fn schema_and_type_name(ty: &ast::Type) -> Option<(Option<Schema>, Na
let inner = array_type.ty()?;
schema_and_type_name(&inner)
}
ast::Type::BitType(bit_type) => {
let name = if bit_type.varying_token().is_some() {
"varbit"
} else {
"bit"
};
Some((None, Name::from_string(name)))
}
ast::Type::BitType(_) => Some((None, Name::from_string("bit"))),
ast::Type::BitVaryingType(_) => Some((None, Name::from_string("varbit"))),
ast::Type::IntervalType(_) => Some((None, Name::from_string("interval"))),
ast::Type::PathType(path_type) => {
let path = path_type.path_ref()?;
Expand All @@ -234,14 +228,22 @@ pub(crate) fn schema_and_type_name(ty: &ast::Type) -> Option<(Option<Schema>, Na
ast::Type::CharacterType(_) => Some((None, Name::from_string("bpchar"))),
ast::Type::DoubleType(_) => Some((None, Name::from_string("float8"))),
ast::Type::TimeType(time_type) => {
let mut name = if time_type.timestamp_token().is_some() {
"timestamp".to_string()
let name = if matches!(time_type.timezone(), Some(ast::Timezone::WithTimezone(_))) {
"timetz"
} else {
"time".to_string()
"time"
};
Some((None, Name::from_string(name)))
}
ast::Type::TimestampType(timestamp_type) => {
let name = if matches!(
timestamp_type.timezone(),
Some(ast::Timezone::WithTimezone(_))
) {
"timestamptz"
} else {
"timestamp"
};
if let Some(ast::Timezone::WithTimezone(_)) = time_type.timezone() {
name.push_str("tz");
}
Some((None, Name::from_string(name)))
}
ast::Type::PercentType(_) => None,
Expand Down
79 changes: 49 additions & 30 deletions crates/squawk_ide/src/semantic_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,33 @@ fn highlight_param_mode(out: &mut SemanticTokenBuilder, mode: ast::ParamMode) {
}
}

fn highlight_timezone(out: &mut SemanticTokenBuilder, timezone: ast::Timezone) {
match timezone {
ast::Timezone::WithTimezone(with_timezone) => {
if let Some(token) = with_timezone.with_token() {
out.push_type(token.into());
}
if let Some(token) = with_timezone.time_token() {
out.push_type(token.into());
}
if let Some(token) = with_timezone.zone_token() {
out.push_type(token.into());
}
}
ast::Timezone::WithoutTimezone(without_timezone) => {
if let Some(token) = without_timezone.without_token() {
out.push_type(token.into());
}
if let Some(token) = without_timezone.time_token() {
out.push_type(token.into());
}
if let Some(token) = without_timezone.zone_token() {
out.push_type(token.into());
}
}
}
}

fn highlight_type(out: &mut SemanticTokenBuilder, ty: ast::Type) {
match ty {
ast::Type::ArrayType(_) => (),
Expand All @@ -51,7 +78,15 @@ fn highlight_type(out: &mut SemanticTokenBuilder, ty: ast::Type) {
if let Some(token) = bit_type.bit_token() {
out.push_type(token.into());
}
if let Some(token) = bit_type.varying_token() {
}
ast::Type::BitVaryingType(bit_varying_type) => {
if let Some(token) = bit_varying_type.setof_token() {
out.push_type(token.into());
}
if let Some(token) = bit_varying_type.bit_token() {
out.push_type(token.into());
}
if let Some(token) = bit_varying_type.varying_token() {
out.push_type(token.into());
}
}
Expand Down Expand Up @@ -119,38 +154,22 @@ fn highlight_type(out: &mut SemanticTokenBuilder, ty: ast::Type) {
if let Some(token) = time_type.setof_token() {
out.push_type(token.into());
}
if let Some(token) = time_type
.timestamp_token()
.or_else(|| time_type.time_token())
{
if let Some(token) = time_type.time_token() {
out.push_type(token.into());
}

if let Some(timezone) = time_type.timezone() {
match timezone {
ast::Timezone::WithTimezone(with_timezone) => {
if let Some(token) = with_timezone.with_token() {
out.push_type(token.into());
}
if let Some(token) = with_timezone.time_token() {
out.push_type(token.into());
}
if let Some(token) = with_timezone.zone_token() {
out.push_type(token.into());
}
}
ast::Timezone::WithoutTimezone(without_timezone) => {
if let Some(token) = without_timezone.without_token() {
out.push_type(token.into());
}
if let Some(token) = without_timezone.time_token() {
out.push_type(token.into());
}
if let Some(token) = without_timezone.zone_token() {
out.push_type(token.into());
}
}
}
highlight_timezone(out, timezone);
}
}
ast::Type::TimestampType(timestamp_type) => {
if let Some(token) = timestamp_type.setof_token() {
out.push_type(token.into());
}
if let Some(token) = timestamp_type.timestamp_token() {
out.push_type(token.into());
}
if let Some(timezone) = timestamp_type.timezone() {
highlight_timezone(out, timezone);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/squawk_linter/src/rules/prefer_bigint_over_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ fn create_bigint_fix(ty: &ast::Type) -> Option<Fix> {
ast::Type::ArrayType(array_type) => return create_bigint_fix(&array_type.ty()?),
ast::Type::PathType(path_type) => path_type.path_ref()?.segment()?,
ast::Type::BitType(_)
| ast::Type::BitVaryingType(_)
| ast::Type::VarcharType(_)
| ast::Type::CharacterType(_)
| ast::Type::DoubleType(_)
| ast::Type::ExprType(_)
| ast::Type::PercentType(_)
| ast::Type::TimeType(_)
| ast::Type::TimestampType(_)
| ast::Type::IntervalType(_) => return None,
};
let int_type = name.text();
Expand Down
2 changes: 2 additions & 0 deletions crates/squawk_linter/src/rules/prefer_bigint_over_smallint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ fn create_bigint_fix(ty: &ast::Type) -> Option<Fix> {
ast::Type::ArrayType(array_type) => return create_bigint_fix(&array_type.ty()?),
ast::Type::PathType(path_type) => path_type.path_ref()?.segment()?,
ast::Type::BitType(_)
| ast::Type::BitVaryingType(_)
| ast::Type::VarcharType(_)
| ast::Type::CharacterType(_)
| ast::Type::DoubleType(_)
| ast::Type::ExprType(_)
| ast::Type::PercentType(_)
| ast::Type::TimeType(_)
| ast::Type::TimestampType(_)
| ast::Type::IntervalType(_) => return None,
};
let i64 = smallint_to_bigint(&name.text());
Expand Down
2 changes: 2 additions & 0 deletions crates/squawk_linter/src/rules/prefer_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ fn create_identity_fix(ty: &ast::Type) -> Option<Fix> {
ast::Type::ArrayType(array_type) => return create_identity_fix(&array_type.ty()?),
ast::Type::PathType(path_type) => path_type.path_ref()?.segment()?,
ast::Type::BitType(_)
| ast::Type::BitVaryingType(_)
| ast::Type::VarcharType(_)
| ast::Type::CharacterType(_)
| ast::Type::DoubleType(_)
| ast::Type::ExprType(_)
| ast::Type::PercentType(_)
| ast::Type::TimeType(_)
| ast::Type::TimestampType(_)
| ast::Type::IntervalType(_) => return None,
};
let text = replace_serial(&name.text());
Expand Down
4 changes: 2 additions & 2 deletions crates/squawk_linter/src/rules/prefer_text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ fn is_not_allowed_varchar(ty: &ast::Type) -> bool {
}
ast::Type::VarcharType(varchar_type) => varchar_type.arg_list().is_some(),
ast::Type::CharacterType(_) => false,
ast::Type::BitType(_) => false,
ast::Type::BitType(_) | ast::Type::BitVaryingType(_) => false,
ast::Type::DoubleType(_) => false,
ast::Type::TimeType(_) => false,
ast::Type::TimeType(_) | ast::Type::TimestampType(_) => false,
ast::Type::IntervalType(_) => false,
ast::Type::ExprType(_) => false,
}
Expand Down
17 changes: 7 additions & 10 deletions crates/squawk_linter/src/rules/prefer_timestamptz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,21 @@ pub fn is_not_allowed_timestamp(ty: &ast::Type) -> bool {
ty_name == "varchar" && path_type.arg_list().is_some()
}
ast::Type::VarcharType(_) | ast::Type::CharacterType(_) => false,
ast::Type::BitType(_) => false,
ast::Type::BitType(_) | ast::Type::BitVaryingType(_) => false,
ast::Type::DoubleType(_) => false,
ast::Type::TimeType(time_type) => {
if time_type.timestamp_token().is_some()
&& !matches!(time_type.timezone(), Some(ast::Timezone::WithTimezone(_)))
{
return true;
}
false
}
ast::Type::TimeType(_) => false,
ast::Type::TimestampType(timestamp_type) => !matches!(
timestamp_type.timezone(),
Some(ast::Timezone::WithTimezone(_))
),
ast::Type::IntervalType(_) => false,
ast::Type::ExprType(_) => false,
}
}

fn fix_timestamp(ty: &ast::Type) -> Option<Fix> {
match ty {
ast::Type::TimeType(_) => {
ast::Type::TimestampType(_) => {
let range = ty.syntax().text_range();
let edit = Edit::replace(range, "timestamptz");
Some(Fix::new("Replace with `timestamptz`", vec![edit]))
Expand Down
4 changes: 2 additions & 2 deletions crates/squawk_linter/src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub(crate) fn is_not_valid_int_type(
invalid_type_names.contains(ty_name.as_str())
}
ast::Type::VarcharType(_) | ast::Type::CharacterType(_) => false,
ast::Type::BitType(_) => false,
ast::Type::BitType(_) | ast::Type::BitVaryingType(_) => false,
ast::Type::DoubleType(_) => false,
ast::Type::TimeType(_) => false,
ast::Type::TimeType(_) | ast::Type::TimestampType(_) => false,
ast::Type::IntervalType(_) => false,
ast::Type::ExprType(_) => false,
}
Expand Down
2 changes: 2 additions & 0 deletions crates/squawk_parser/src/generated/syntax_kind.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 24 additions & 8 deletions crates/squawk_parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2264,22 +2264,30 @@ fn opt_type_name_with(p: &mut Parser<'_>, type_args_enabled: bool) -> Option<Com
let wrapper_type = match p.current() {
BIT_KW => {
p.bump(BIT_KW);
p.eat(VARYING_KW);
BIT_TYPE
if p.eat(VARYING_KW) {
BIT_VARYING_TYPE
} else {
BIT_TYPE
}
}
NATIONAL_KW if matches!(p.nth(1), CHAR_KW | CHARACTER_KW) => {
p.bump(NATIONAL_KW);
char_type(p)
}
CHARACTER_KW | CHAR_KW | NCHAR_KW | VARCHAR_KW => char_type(p),
TIMESTAMP_KW | TIME_KW => {
p.bump_any();
let kind = if p.eat(TIMESTAMP_KW) {
TIMESTAMP_TYPE
} else {
p.bump(TIME_KW);
TIME_TYPE
};
if p.eat(L_PAREN) {
expr(p);
p.expect(R_PAREN);
}
opt_with_timezone(p);
TIME_TYPE
kind
}
INTERVAL_KW => {
p.bump(INTERVAL_KW);
Expand Down Expand Up @@ -2476,20 +2484,28 @@ fn name_ref_(p: &mut Parser<'_>) -> Option<CompletedMarker> {
let m = p.start();
let kind = match p.current() {
TIMESTAMP_KW | TIME_KW => {
p.bump_any();
let kind = if p.eat(TIMESTAMP_KW) {
TIMESTAMP_TYPE
} else {
p.bump(TIME_KW);
TIME_TYPE
};
if p.eat(L_PAREN) {
if opt_numeric_literal(p).is_none() {
p.error("expected numeric literal");
}
p.expect(R_PAREN);
}
opt_with_timezone(p);
TIME_TYPE
kind
}
BIT_KW => {
p.bump(BIT_KW);
p.eat(VARYING_KW);
BIT_TYPE
if p.eat(VARYING_KW) {
BIT_VARYING_TYPE
} else {
BIT_TYPE
}
}
NATIONAL_KW if matches!(p.nth(1), CHAR_KW | CHARACTER_KW) => {
p.bump(NATIONAL_KW);
Expand Down
Loading
Loading