From 7191d6c46bdf7a3b8aef2745e64d929b56af1df9 Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Thu, 7 Nov 2024 21:25:25 +0100 Subject: [PATCH 1/5] handle_double_colons --- src/formatter.rs | 9 +++++++++ src/lib.rs | 21 ++++++++++++++++++++- src/tokenizer.rs | 18 ++++++++++++++++-- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index 9f69329..5943840 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -80,6 +80,9 @@ pub(crate) fn format( TokenKind::Placeholder => { formatter.format_placeholder(token, &mut formatted_query); } + TokenKind::DoubleColon => { + formatter.format_double_colon(token, &mut formatted_query); + } _ => match token.value { "," => { formatter.format_comma(token, &mut formatted_query); @@ -150,6 +153,12 @@ impl<'a> Formatter<'a> { self.add_new_line(query); } + fn format_double_colon(&self, _token: &Token<'_>, query: &mut String) { + if query.ends_with(char::is_whitespace) { + query.pop(); + } + query.push_str("::"); + } fn format_block_comment(&self, token: &Token<'_>, query: &mut String) { self.add_new_line(query); query.push_str(&self.indent_comment(token.value)); diff --git a/src/lib.rs b/src/lib.rs index c6388c7..45ece30 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -669,7 +669,7 @@ mod tests { #[test] fn it_formats_postgres_specific_operators() { let strings = [ - ("column::int", "column :: int"), + ("column::int", "column::int"), ("v->2", "v -> 2"), ("v->>2", "v ->> 2"), ("foo ~~ 'hello'", "foo ~~ 'hello'"), @@ -1840,6 +1840,25 @@ SELECT left ~= right" ); + assert_eq!(format(input, &QueryParams::None, &options), expected); + } + #[test] + fn it_keept_() { + let input = "select text::text, num::integer, data::json frOM foo"; + let options = FormatOptions { + uppercase: Some(false), + ..FormatOptions::default() + }; + let expected = indoc!( + " +select + text::text, + num::integer, + data::json +from + foo" + ); + assert_eq!(format(input, &QueryParams::None, &options), expected); } } diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 5a5ed1b..4d9b4f9 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -46,6 +46,7 @@ pub(crate) struct Token<'a> { #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) enum TokenKind { + DoubleColon, Whitespace, String, Reserved, @@ -107,12 +108,25 @@ fn get_next_token<'a>( last_reserved_top_level_token, ) }) + .or_else(|_| get_double_colon_token(input)) .or_else(|_| get_operator_token(input)) .or_else(|_| get_placeholder_token(input, named_placeholders)) .or_else(|_| get_word_token(input)) .or_else(|_| get_any_other_char(input)) } - +fn get_double_colon_token(input: &str) -> IResult<&str, Token<'_>> { + // Use the tag combinator to match the "::" in the input + tag("::")(input).map(|(input, token)| { + ( + input, + Token { + kind: TokenKind::DoubleColon, + value: token, + key: None, + }, + ) + }) +} fn get_whitespace_token(input: &str) -> IResult<&str, Token<'_>> { take_while1(char::is_whitespace)(input).map(|(input, token)| { ( @@ -1073,7 +1087,6 @@ fn get_operator_token(input: &str) -> IResult<&str, Token<'_>> { tag(">"), tag("="), tag("|"), - tag(":"), tag("-"), tag("~"), tag("*"), @@ -1083,6 +1096,7 @@ fn get_operator_token(input: &str) -> IResult<&str, Token<'_>> { tag("?"), tag("#"), tag("/"), + tag(":"), )); map( From fbfe4e9407c564f6cd83469a0b31f1d9b949a865 Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Thu, 7 Nov 2024 21:29:01 +0100 Subject: [PATCH 2/5] extend test --- src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 45ece30..316c963 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1844,7 +1844,7 @@ SELECT } #[test] fn it_keept_() { - let input = "select text::text, num::integer, data::json frOM foo"; + let input = "select text::text, num::integer, data::json, (x - y)::integer frOM foo"; let options = FormatOptions { uppercase: Some(false), ..FormatOptions::default() @@ -1854,7 +1854,8 @@ SELECT select text::text, num::integer, - data::json + data::json, + (x - y)::integer from foo" ); From 84558bd2067214ef59d1f70533a0cc69bc7c052a Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Thu, 7 Nov 2024 21:30:39 +0100 Subject: [PATCH 3/5] remove comment --- src/tokenizer.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 4d9b4f9..0d2ef78 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -115,7 +115,6 @@ fn get_next_token<'a>( .or_else(|_| get_any_other_char(input)) } fn get_double_colon_token(input: &str) -> IResult<&str, Token<'_>> { - // Use the tag combinator to match the "::" in the input tag("::")(input).map(|(input, token)| { ( input, @@ -1087,6 +1086,7 @@ fn get_operator_token(input: &str) -> IResult<&str, Token<'_>> { tag(">"), tag("="), tag("|"), + tag(":"), tag("-"), tag("~"), tag("*"), @@ -1096,7 +1096,6 @@ fn get_operator_token(input: &str) -> IResult<&str, Token<'_>> { tag("?"), tag("#"), tag("/"), - tag(":"), )); map( From beddbc4cbd24a0b29c9ab682466cf7634e349c9a Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Thu, 7 Nov 2024 21:32:34 +0100 Subject: [PATCH 4/5] update test --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 316c963..33ce7d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1844,7 +1844,7 @@ SELECT } #[test] fn it_keept_() { - let input = "select text::text, num::integer, data::json, (x - y)::integer frOM foo"; + let input = "select text :: text, num::integer, data::json, (x - y)::integer frOM foo"; let options = FormatOptions { uppercase: Some(false), ..FormatOptions::default() From 42756f269ef0f01fb827dff21dc088eaf81a177b Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Thu, 7 Nov 2024 22:05:30 +0100 Subject: [PATCH 5/5] use trim_all_spaces_end --- src/formatter.rs | 4 +--- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/formatter.rs b/src/formatter.rs index 5943840..83bccc5 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -154,9 +154,7 @@ impl<'a> Formatter<'a> { } fn format_double_colon(&self, _token: &Token<'_>, query: &mut String) { - if query.ends_with(char::is_whitespace) { - query.pop(); - } + self.trim_all_spaces_end(query); query.push_str("::"); } fn format_block_comment(&self, token: &Token<'_>, query: &mut String) { diff --git a/src/lib.rs b/src/lib.rs index 33ce7d6..5d7d2da 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1843,7 +1843,7 @@ SELECT assert_eq!(format(input, &QueryParams::None, &options), expected); } #[test] - fn it_keept_() { + fn it_formats_double_colons() { let input = "select text :: text, num::integer, data::json, (x - y)::integer frOM foo"; let options = FormatOptions { uppercase: Some(false),