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

Support multiline ignores in table fields #707

Merged
merged 4 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Multiline ignores (`-- stylua: ignore start` / `-- stylua: ignore end`) will now work within table fields:

```lua
require("foo").bar {
-- stylua: ignore start
baz =0, -- < not formatted
foo = 2, -- < not formatted
-- stylua: ignore end
bar = 1234 -- formatted
}
```

### Fixed

- Fixed missing option `--sort-requires` to enable sort requires on the command line
Expand Down
19 changes: 14 additions & 5 deletions src/formatters/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ pub fn format_multiline_table<T, U>(
shape: Shape,
) -> (ContainedSpan, Punctuated<T>)
where
T: std::fmt::Display,
T: std::fmt::Display + Node,
U: Fn(&Context, &T, TableType, Shape) -> (T, Vec<Token>),
{
let table_type = TableType::MultiLine;
Expand All @@ -349,14 +349,18 @@ where
let current_fields = fields.pairs();
let mut fields = Punctuated::new();

let mut ctx = *ctx;

for pair in current_fields {
let (field, punctuation) = (pair.value(), pair.punctuation());

ctx = ctx.check_toggle_formatting(field);

// Reset the shape onto a new line, as we are a new field
shape = shape.reset().add_width(1); // Add 1 to include the trailing comma at the end

// Format the field
let (formatted_field, mut trailing_trivia) = formatter(ctx, field, table_type, shape);
let (formatted_field, mut trailing_trivia) = formatter(&ctx, field, table_type, shape);

// If trivia is just whitespace, ignore it completely
if trailing_trivia
Expand All @@ -370,17 +374,22 @@ where
.iter()
.filter(|x| !trivia_util::trivia_is_whitespace(x))
.flat_map(|x| {
trivia_to_vec(format_token(ctx, x, FormatTokenType::TrailingTrivia, shape))
trivia_to_vec(format_token(
&ctx,
x,
FormatTokenType::TrailingTrivia,
shape,
))
})
.collect();
}

// Continue adding a comma and a new line for multiline tables
// Add newline trivia to the end of the symbol
trailing_trivia.push(create_newline_trivia(ctx));
trailing_trivia.push(create_newline_trivia(&ctx));

let symbol = match punctuation {
Some(punctuation) => fmt_symbol!(ctx, punctuation, ",", shape),
Some(punctuation) => fmt_symbol!(&ctx, punctuation, ",", shape),
None => TokenReference::symbol(",").unwrap(),
}
.update_trailing_trivia(FormatTriviaType::Append(trailing_trivia));
Expand Down
9 changes: 9 additions & 0 deletions tests/inputs-ignore/multiline-ignore-table-field.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- https://github.com/JohnnyMorganz/StyLua/issues/705

require("foo").bar {
-- stylua: ignore start
baz =0,
foo = 2,
-- stylua: ignore end
bar = 1234
}
14 changes: 14 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: tests/tests.rs
expression: format(&contents)
---
-- https://github.com/JohnnyMorganz/StyLua/issues/705

require("foo").bar({
-- stylua: ignore start
baz =0,
foo = 2,
-- stylua: ignore end
bar = 1234,
})