Skip to content

Commit e62246a

Browse files
committed
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
1 parent ffbef7e commit e62246a

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

src/formatters/table.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,17 @@ pub fn create_table_braces(
245245
ContainedSpan::new(start_brace_token, end_brace_token)
246246
}
247247

248-
TableType::SingleLine => ContainedSpan::new(
249-
fmt_symbol!(ctx, start_brace, "{ ", shape),
250-
fmt_symbol!(ctx, end_brace, " }", shape),
251-
),
248+
TableType::SingleLine => {
249+
let (start_, end_) = if ctx.config().brace_spacing {
250+
("{ ", " }")
251+
} else {
252+
("{", "}")
253+
};
254+
ContainedSpan::new(
255+
fmt_symbol!(ctx, start_brace, start_, shape),
256+
fmt_symbol!(ctx, end_brace, end_, shape),
257+
)
258+
}
252259

253260
TableType::Empty => {
254261
let start_brace = fmt_symbol!(ctx, start_brace, "{", shape);

src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ pub struct Config {
168168
/// if set to [`CollapseSimpleStatement::None`] structures are never collapsed.
169169
/// if set to [`CollapseSimpleStatement::FunctionOnly`] then simple functions (i.e., functions with a single laststmt) can be collapsed
170170
collapse_simple_statement: CollapseSimpleStatement,
171+
/// Whether we add spacing inside the curly brackets around the content of a table.
172+
brace_spacing: bool,
171173
}
172174

173175
#[cfg_attr(all(target_arch = "wasm32", feature = "wasm-bindgen"), wasm_bindgen)]
@@ -275,6 +277,14 @@ impl Config {
275277
..self
276278
}
277279
}
280+
281+
/// Returns a new config with the given bracket space configuration
282+
pub fn with_brace_spacing(self, brace_spacing: bool) -> Self {
283+
Self {
284+
brace_spacing,
285+
..self
286+
}
287+
}
278288
}
279289

280290
impl Default for Config {
@@ -288,6 +298,7 @@ impl Default for Config {
288298
no_call_parentheses: false,
289299
call_parentheses: CallParenType::default(),
290300
collapse_simple_statement: CollapseSimpleStatement::default(),
301+
brace_spacing: true,
291302
}
292303
}
293304
}

tests/test_table_spaces.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use stylua_lib::{format_code, Config, OutputVerification};
2+
3+
fn format(brace_spacing: bool, input: &str) -> String {
4+
format_code(
5+
input,
6+
Config::default().with_brace_spacing(brace_spacing),
7+
None,
8+
OutputVerification::None,
9+
)
10+
.unwrap()
11+
}
12+
13+
#[test]
14+
fn test_table_oneline_with_internal_spaces() {
15+
insta::assert_snapshot!(
16+
format(true,
17+
r###"
18+
local foo = { "content" }
19+
"###
20+
),
21+
@r###"
22+
local foo = { "content" }
23+
"###
24+
);
25+
}
26+
27+
#[test]
28+
fn test_table_oneline_without_internal_spaces() {
29+
insta::assert_snapshot!(
30+
format(false,
31+
r###"
32+
local foo = { "content" }
33+
"###
34+
),
35+
@r###"
36+
local foo = {"content"}
37+
"###
38+
);
39+
}

0 commit comments

Comments
 (0)