Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add brace_spacing option #628

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bbc2
Copy link

@bbc2 bbc2 commented Dec 4, 2022

Braces, used for table construction, are typically used with spaces inside them:

t = { "content-0" }

There can be another style, however, which consists in sticking the braces to the content:

t = {"content-0"}

This work adds a configuration parameter brace_spacing: BraceSpacing (Always, Never) to control the spacing inside table constructors and enable the use of the second style. This is similar to Prettier's bracket spacing option.

Which style is better is debatable, of course, and so is the configurability. 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

Closes #313

@bbc2
Copy link
Author

bbc2 commented Dec 4, 2022

To be honest, I have mixed feelings about adding options to formatters (maintainability, fragmentation of the community). Even if this is rejected, I hope it will shed some light on #620.

Regardless, I will definitely use this tool so thank you for it! Also, I found it very easy to modify.

@JohnnyMorganz
Copy link
Owner

JohnnyMorganz commented Dec 4, 2022

An interesting thing that prettier does, is that it doesn't have spaces around array braces, but does in object braces (to be fair, maybe because they are 2 different syntax constructs in JS/TS, but not in Lua).

I wonder if that would be an interesting middle ground?

Regardless, for all options I tend to not make them boolean but instead enums, so that they can be extended later (and also makes it slightly easier to understand what the option does)

No promises on whether this will be merged or not, but just some thoughts!

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: BraceSpacing`
(`Always`, `Never`) 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
@bbc2 bbc2 force-pushed the config-space-inside-braces branch from e62246a to 256e5e0 Compare December 4, 2022 12:20
@bbc2
Copy link
Author

bbc2 commented Dec 4, 2022

An interesting thing that prettier does, is that it doesn't have spaces around array braces, but does in object braces (to be fair, maybe because they are 2 different syntax constructs in JS/TS, but not in Lua).

I wonder if that would be an interesting middle ground?

I'm not sure because in Lua you can mix the two forms in the same literal:

table = {"x", "y", a = "z"}
-- or
table = { "x", "y", a = "z" }

Lua is pretty wild. :)

Regardless, for all options I tend to not make them boolean but instead enums, so that they can be extended later (and also makes it slightly easier to understand what the option does)

Done! I also added the --brace-spacing CLI option for the sake of completeness. Easy enough.

@michaeldougal
Copy link

Just came here to say that I've been pretty happy with StyLua so far except for this one missing feature. Please consider merging!

I'll note too that while Roblox (and I realize this is not just about Roblox, but they do constitute a significant group of Lua(u) developers) doesn't take an official stand on it in their style guide, most documentation examples follow the form of having no outer edge spacing, so that is likely to be the form that many developers on the platform learn and use.

As for fragmenting the community or causing divides by adding options, I wouldn't worry about that. StyLua is presented as an opinionated code formatter, and I don't see why we can't use it to implement our own opinionated formatting. Customization options seem to only be beneficial to me. We are also already given options for different parts like quote formatting, so this is entirely appropriate imo.

@tartopohm
Copy link

tartopohm commented Sep 19, 2023

Just came here to say that I've been pretty happy with StyLua so far except for this one missing feature. Please consider merging!

Agreed!

As for fragmenting the community or causing divides by adding options, I wouldn't worry about that.

Agreed as well. If anything, allowing for more configuration should have the opposite effect of fragmentation, by allowing people with different opinions to use the same tool, as long as the configuration is agreed upon at the project level.

@raiguard
Copy link

raiguard commented Aug 19, 2024

I will voice my support for this, it's the one thing in stylua that I don't like. We have very deeply nested table definitions and the extra spaces waste a lot of line length.

@premek
Copy link

premek commented Oct 26, 2024

just a note: editorconfig proposes:
spaces_around_brackets (none, inside, outside, both)

It's mentioned in the section:
Ideas for Domain-Specific Properties
The following properties are not intended to be implemented by EditorConfig.
This is simply a brainstorm of domain-specific properties that could be supported by some tools that rely on EditorConfig files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Request option] Don't add space within curly braces?
6 participants