From e62246a1bdef34077d9508769a9fc544ef410fd5 Mon Sep 17 00:00:00 2001 From: Bertrand Bonnefoy-Claudet Date: Sat, 26 Nov 2022 12:58:03 +0100 Subject: [PATCH] Add `brace_spacing` option Braces, used for table construction, are typically used with spaces inside them: ```lua t = { "content-0" } ``` There can be another style, however, which consists in sticking the braces to the content: ```lua t = {"content-0"} ``` This work adds a configuration parameter `brace_spacing: bool` to control the spacing inside table constructors and enable the use of the second style. This is similar to [Prettier's bracket spacing option](https://prettier.io/docs/en/options.html#bracket-spacing). Which style is better is debatable, of course. In my quick research, I listed what the formatters I know do: - rustfmt (Rust): space - OCamlFormat (OCaml): space, configurable - Prettier (JavaScript, TypeScript): space, configurable - Black (Python): no space - Clang-Format (C, C++): no space, configurable --- src/formatters/table.rs | 15 +++++++++++---- src/lib.rs | 11 +++++++++++ tests/test_table_spaces.rs | 39 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 tests/test_table_spaces.rs diff --git a/src/formatters/table.rs b/src/formatters/table.rs index e65d7a59..f684757f 100644 --- a/src/formatters/table.rs +++ b/src/formatters/table.rs @@ -245,10 +245,17 @@ pub fn create_table_braces( ContainedSpan::new(start_brace_token, end_brace_token) } - TableType::SingleLine => ContainedSpan::new( - fmt_symbol!(ctx, start_brace, "{ ", shape), - fmt_symbol!(ctx, end_brace, " }", shape), - ), + TableType::SingleLine => { + let (start_, end_) = if ctx.config().brace_spacing { + ("{ ", " }") + } else { + ("{", "}") + }; + ContainedSpan::new( + fmt_symbol!(ctx, start_brace, start_, shape), + fmt_symbol!(ctx, end_brace, end_, shape), + ) + } TableType::Empty => { let start_brace = fmt_symbol!(ctx, start_brace, "{", shape); diff --git a/src/lib.rs b/src/lib.rs index 6c744e93..3636e469 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -168,6 +168,8 @@ pub struct Config { /// if set to [`CollapseSimpleStatement::None`] structures are never collapsed. /// if set to [`CollapseSimpleStatement::FunctionOnly`] then simple functions (i.e., functions with a single laststmt) can be collapsed collapse_simple_statement: CollapseSimpleStatement, + /// Whether we add spacing inside the curly brackets around the content of a table. + brace_spacing: bool, } #[cfg_attr(all(target_arch = "wasm32", feature = "wasm-bindgen"), wasm_bindgen)] @@ -275,6 +277,14 @@ impl Config { ..self } } + + /// Returns a new config with the given bracket space configuration + pub fn with_brace_spacing(self, brace_spacing: bool) -> Self { + Self { + brace_spacing, + ..self + } + } } impl Default for Config { @@ -288,6 +298,7 @@ impl Default for Config { no_call_parentheses: false, call_parentheses: CallParenType::default(), collapse_simple_statement: CollapseSimpleStatement::default(), + brace_spacing: true, } } } diff --git a/tests/test_table_spaces.rs b/tests/test_table_spaces.rs new file mode 100644 index 00000000..8d1885b9 --- /dev/null +++ b/tests/test_table_spaces.rs @@ -0,0 +1,39 @@ +use stylua_lib::{format_code, Config, OutputVerification}; + +fn format(brace_spacing: bool, input: &str) -> String { + format_code( + input, + Config::default().with_brace_spacing(brace_spacing), + None, + OutputVerification::None, + ) + .unwrap() +} + +#[test] +fn test_table_oneline_with_internal_spaces() { + insta::assert_snapshot!( + format(true, + r###" +local foo = { "content" } +"### + ), + @r###" + local foo = { "content" } + "### + ); +} + +#[test] +fn test_table_oneline_without_internal_spaces() { + insta::assert_snapshot!( + format(false, + r###" +local foo = { "content" } +"### + ), + @r###" + local foo = {"content"} + "### + ); +}