Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ignore keywords for uppercase=True #53

Merged
merged 4 commits into from
Sep 17, 2024
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
16 changes: 8 additions & 8 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn simple_query(c: &mut Criterion) {
format(
black_box(input),
black_box(&QueryParams::None),
black_box(FormatOptions::default()),
black_box(&FormatOptions::default()),
)
})
});
Expand All @@ -21,7 +21,7 @@ fn complex_query(c: &mut Criterion) {
format(
black_box(input),
black_box(&QueryParams::None),
black_box(FormatOptions::default()),
black_box(&FormatOptions::default()),
)
})
});
Expand All @@ -39,7 +39,7 @@ fn query_with_named_params(c: &mut Criterion) {
format(
black_box(input),
black_box(&QueryParams::Named(params.clone())),
black_box(FormatOptions::default()),
black_box(&FormatOptions::default()),
)
})
});
Expand All @@ -53,7 +53,7 @@ fn query_with_explicit_indexed_params(c: &mut Criterion) {
format(
black_box(input),
black_box(&QueryParams::Indexed(params.clone())),
black_box(FormatOptions::default()),
black_box(&FormatOptions::default()),
)
})
});
Expand All @@ -67,7 +67,7 @@ fn query_with_implicit_indexed_params(c: &mut Criterion) {
format(
black_box(input),
black_box(&QueryParams::Indexed(params.clone())),
black_box(FormatOptions::default()),
black_box(&FormatOptions::default()),
)
})
});
Expand Down Expand Up @@ -135,7 +135,7 @@ VALUES
format(
black_box(&input),
black_box(&QueryParams::None),
black_box(FormatOptions::default()),
black_box(&FormatOptions::default()),
)
})
});
Expand All @@ -149,7 +149,7 @@ fn issue_633_2(c: &mut Criterion) {
format(
black_box(input),
black_box(&QueryParams::Indexed(params.clone())),
black_box(FormatOptions::default()),
black_box(&FormatOptions::default()),
)
})
});
Expand All @@ -175,7 +175,7 @@ fn issue_633_3(c: &mut Criterion) {
format(
black_box(&input),
black_box(&QueryParams::None),
black_box(FormatOptions::default()),
black_box(&FormatOptions::default()),
)
})
});
Expand Down
39 changes: 32 additions & 7 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ pub(crate) fn check_fmt_off(s: &str) -> Option<bool> {
.map(|matched| matched.as_str().eq_ignore_ascii_case("off"))
}

pub(crate) fn format(tokens: &[Token<'_>], params: &QueryParams, options: FormatOptions) -> String {
pub(crate) fn format(
tokens: &[Token<'_>],
params: &QueryParams,
options: &FormatOptions,
) -> String {
let mut formatter = Formatter::new(tokens, params, options);
let mut formatted_query = String::new();
let mut is_fmt_enabled = true;
Expand Down Expand Up @@ -85,13 +89,13 @@ struct Formatter<'a> {
previous_reserved_word: Option<&'a Token<'a>>,
tokens: &'a [Token<'a>],
params: Params<'a>,
options: FormatOptions,
indentation: Indentation,
options: &'a FormatOptions<'a>,
indentation: Indentation<'a>,
inline_block: InlineBlock,
}

impl<'a> Formatter<'a> {
fn new(tokens: &'a [Token<'a>], params: &'a QueryParams, options: FormatOptions) -> Self {
fn new(tokens: &'a [Token<'a>], params: &'a QueryParams, options: &'a FormatOptions) -> Self {
Formatter {
index: 0,
previous_reserved_word: None,
Expand Down Expand Up @@ -182,7 +186,14 @@ impl<'a> Formatter<'a> {
{
self.trim_spaces_end(query);
}
if self.options.uppercase {
if self.options.uppercase
&& !self
.options
.ignore_case_convert
.as_ref()
.map(|values| values.contains(&token.value))
.unwrap_or(false)
{
query.push_str(&token.value.to_uppercase());
} else {
query.push_str(token.value);
Expand All @@ -199,7 +210,14 @@ impl<'a> Formatter<'a> {
// Closing parentheses decrease the block indent level
fn format_closing_parentheses(&mut self, token: &Token<'_>, query: &mut String) {
let mut token = token.clone();
let value = if self.options.uppercase {
let value = if self.options.uppercase
&& !self
.options
.ignore_case_convert
.as_ref()
.map(|values| values.contains(&token.value))
.unwrap_or(false)
{
token.value.to_uppercase()
} else {
token.value.to_string()
Expand Down Expand Up @@ -299,7 +317,14 @@ impl<'a> Formatter<'a> {
}

fn format_reserved_word<'t>(&self, token: &'t str) -> Cow<'t, str> {
if self.options.uppercase {
if self.options.uppercase
&& !self
.options
.ignore_case_convert
.as_ref()
.map(|values| values.contains(&token))
.unwrap_or(false)
{
Cow::Owned(token.to_uppercase())
} else {
Cow::Borrowed(token)
Expand Down
8 changes: 4 additions & 4 deletions src/indentation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{FormatOptions, Indent};

pub(crate) struct Indentation {
options: FormatOptions,
pub(crate) struct Indentation<'a> {
options: &'a FormatOptions<'a>,
indent_types: Vec<IndentType>,
}

Expand All @@ -11,8 +11,8 @@ enum IndentType {
BlockLevel,
}

impl Indentation {
pub fn new(options: FormatOptions) -> Self {
impl<'a> Indentation<'a> {
pub fn new(options: &'a FormatOptions) -> Self {
Indentation {
options,
indent_types: Vec::new(),
Expand Down
Loading
Loading