Skip to content

Commit bc41595

Browse files
committed
Add an option to keep the first arguments of top level words inline
Towards addressing shssoichiro#66 a bit more.
1 parent 954cdf7 commit bc41595

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/formatter.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,12 @@ impl<'a> Formatter<'a> {
210210
self.add_new_line(query);
211211
self.indentation.increase_top_level(span_len);
212212
query.push_str(&self.equalize_whitespace(&self.format_reserved_word(token.value)));
213-
if self
214-
.options
215-
.max_inline_top_level
216-
.map_or(true, |limit| limit < span_len)
213+
if !(!["select", "from"].contains(&token.value.to_lowercase().as_str())
214+
&& self.options.inline_first_top_level)
215+
&& self
216+
.options
217+
.max_inline_top_level
218+
.map_or(true, |limit| limit < span_len)
217219
{
218220
self.add_new_line(query);
219221
} else {

src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ pub struct FormatOptions<'a> {
6161
///
6262
/// Default: None
6363
pub max_inline_top_level: Option<usize>,
64+
/// Inline the first top level argument
65+
///
66+
/// Default: false
67+
pub inline_first_top_level: bool,
6468
}
6569

6670
impl<'a> Default for FormatOptions<'a> {
@@ -74,6 +78,7 @@ impl<'a> Default for FormatOptions<'a> {
7478
max_inline_block: 50,
7579
max_inline_arguments: None,
7680
max_inline_top_level: None,
81+
inline_first_top_level: false,
7782
}
7883
}
7984
}
@@ -306,6 +311,34 @@ mod tests {
306311
assert_eq!(format(input, &QueryParams::None, &options), expected);
307312
}
308313

314+
#[test]
315+
fn it_formats_select_with_complex_where_top_level_inline() {
316+
let input = indoc!(
317+
"
318+
SELECT * FROM foo, bar, baz WHERE Column1 = 'testing'
319+
AND ( (Column2 = Column3 OR Column4 >= NOW()) );
320+
"
321+
);
322+
let options = FormatOptions {
323+
inline_first_top_level: true,
324+
max_inline_top_level: Some(10),
325+
max_inline_arguments: Some(20),
326+
max_inline_block: 50,
327+
..Default::default()
328+
};
329+
let expected = indoc!(
330+
"
331+
SELECT *
332+
FROM
333+
foo, bar, baz
334+
WHERE Column1 = 'testing'
335+
AND ((Column2 = Column3
336+
OR Column4 >= NOW()));"
337+
);
338+
339+
assert_eq!(format(input, &QueryParams::None, &options), expected);
340+
}
341+
309342
#[test]
310343
fn it_formats_select_with_top_level_reserved_words() {
311344
let input = indoc!(

0 commit comments

Comments
 (0)