Skip to content

Commit

Permalink
Add brace_spacing option
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bbc2 committed Dec 4, 2022
1 parent ffbef7e commit e62246a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/formatters/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand All @@ -288,6 +298,7 @@ impl Default for Config {
no_call_parentheses: false,
call_parentheses: CallParenType::default(),
collapse_simple_statement: CollapseSimpleStatement::default(),
brace_spacing: true,
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions tests/test_table_spaces.rs
Original file line number Diff line number Diff line change
@@ -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"}
"###
);
}

0 comments on commit e62246a

Please sign in to comment.