Skip to content

Commit

Permalink
Add an option to keep the first arguments of top level words inline
Browse files Browse the repository at this point in the history
Towards addressing #66 a bit more.
  • Loading branch information
lu-zero committed Jan 20, 2025
1 parent bcc1be1 commit 9db0ef7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,12 @@ impl<'a> Formatter<'a> {
self.add_new_line(query);
self.indentation.increase_top_level(span_len);
query.push_str(&self.equalize_whitespace(&self.format_reserved_word(token.value)));
if self
.options
.max_inline_top_level
.map_or(true, |limit| limit < span_len)
if !(!["select", "from"].contains(&token.value.to_lowercase().as_str())

Check failure on line 213 in src/formatter.rs

View workflow job for this annotation

GitHub Actions / build-test-unix (latest-stable)

this boolean expression can be simplified

Check failure on line 213 in src/formatter.rs

View workflow job for this annotation

GitHub Actions / build-test-unix (latest-stable)

this boolean expression can be simplified
&& self.options.inline_first_top_level)
&& self
.options
.max_inline_top_level
.map_or(true, |limit| limit < span_len)
{
self.add_new_line(query);
} else {
Expand Down
33 changes: 33 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ pub struct FormatOptions<'a> {
///
/// Default: None
pub max_inline_top_level: Option<usize>,
/// Inline the first top level argument
///
/// Default: false
pub inline_first_top_level: bool,
}

impl<'a> Default for FormatOptions<'a> {
Expand All @@ -74,6 +78,7 @@ impl<'a> Default for FormatOptions<'a> {
max_inline_block: 50,
max_inline_arguments: None,
max_inline_top_level: None,
inline_first_top_level: false,
}
}
}
Expand Down Expand Up @@ -306,6 +311,34 @@ mod tests {
assert_eq!(format(input, &QueryParams::None, &options), expected);
}

#[test]
fn it_formats_select_with_complex_where_top_level_inline() {
let input = indoc!(
"
SELECT * FROM foo, bar, baz WHERE Column1 = 'testing'
AND ( (Column2 = Column3 OR Column4 >= NOW()) );
"
);
let options = FormatOptions {
inline_first_top_level: true,
max_inline_top_level: Some(10),
max_inline_arguments: Some(20),
max_inline_block: 50,
..Default::default()
};
let expected = indoc!(
"
SELECT *
FROM
foo, bar, baz
WHERE Column1 = 'testing'
AND ((Column2 = Column3
OR Column4 >= NOW()));"
);

assert_eq!(format(input, &QueryParams::None, &options), expected);
}

#[test]
fn it_formats_select_with_top_level_reserved_words() {
let input = indoc!(
Expand Down

0 comments on commit 9db0ef7

Please sign in to comment.