Skip to content

Commit

Permalink
handle_double_colons
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuku committed Nov 7, 2024
1 parent 3c00871 commit 7191d6c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
9 changes: 9 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,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));
Expand Down
21 changes: 20 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,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);
}
}
18 changes: 16 additions & 2 deletions 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,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)| {
(
Expand Down Expand Up @@ -1073,7 +1087,6 @@ fn get_operator_token(input: &str) -> IResult<&str, Token<'_>> {
tag(">"),
tag("="),
tag("|"),
tag(":"),
tag("-"),
tag("~"),
tag("*"),
Expand All @@ -1083,6 +1096,7 @@ fn get_operator_token(input: &str) -> IResult<&str, Token<'_>> {
tag("?"),
tag("#"),
tag("/"),
tag(":"),
));

map(
Expand Down

0 comments on commit 7191d6c

Please sign in to comment.