Skip to content

Commit

Permalink
handle_double_colons (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuku authored Nov 7, 2024
1 parent aa5b145 commit fe382a4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -150,6 +153,10 @@ impl<'a> Formatter<'a> {
self.add_new_line(query);
}

fn format_double_colon(&self, _token: &Token<'_>, query: &mut String) {
self.trim_all_spaces_end(query);
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));
Expand Down
22 changes: 21 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'"),
Expand Down Expand Up @@ -1840,6 +1840,26 @@ SELECT
left ~= right"
);

assert_eq!(format(input, &QueryParams::None, &options), expected);
}
#[test]
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),
..FormatOptions::default()
};
let expected = indoc!(
"
select
text::text,
num::integer,
data::json,
(x - y)::integer
from
foo"
);

assert_eq!(format(input, &QueryParams::None, &options), expected);
}
}
15 changes: 14 additions & 1 deletion src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub(crate) struct Token<'a> {

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum TokenKind {
DoubleColon,
Whitespace,
String,
Reserved,
Expand Down Expand Up @@ -107,12 +108,24 @@ 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<'_>> {
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)| {
(
Expand Down

0 comments on commit fe382a4

Please sign in to comment.