From 9cad61fe816c2f76bf0d9306f1b6651ba6e62f0c Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Wed, 7 Jun 2023 13:06:50 +0100 Subject: [PATCH 1/4] Add test case --- tests/inputs-ignore/multiline-ignore-table-field.lua | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/inputs-ignore/multiline-ignore-table-field.lua diff --git a/tests/inputs-ignore/multiline-ignore-table-field.lua b/tests/inputs-ignore/multiline-ignore-table-field.lua new file mode 100644 index 00000000..b70161a2 --- /dev/null +++ b/tests/inputs-ignore/multiline-ignore-table-field.lua @@ -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 +} From 085fce80ec51a1675948fa2128593c33fe755c04 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Wed, 7 Jun 2023 13:07:01 +0100 Subject: [PATCH 2/4] Support multiline ignores in table fields --- src/formatters/table.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/formatters/table.rs b/src/formatters/table.rs index 4535bfbc..d8104865 100644 --- a/src/formatters/table.rs +++ b/src/formatters/table.rs @@ -337,7 +337,7 @@ pub fn format_multiline_table( shape: Shape, ) -> (ContainedSpan, Punctuated) where - T: std::fmt::Display, + T: std::fmt::Display + Node, U: Fn(&Context, &T, TableType, Shape) -> (T, Vec), { let table_type = TableType::MultiLine; @@ -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 @@ -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)); From 327549c21e128edc786f2fa291a42870a39b7a22 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Wed, 7 Jun 2023 13:08:11 +0100 Subject: [PATCH 3/4] Add snapshot --- ...__ignores@multiline-ignore-table-field.lua.snap | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/snapshots/tests__ignores@multiline-ignore-table-field.lua.snap diff --git a/tests/snapshots/tests__ignores@multiline-ignore-table-field.lua.snap b/tests/snapshots/tests__ignores@multiline-ignore-table-field.lua.snap new file mode 100644 index 00000000..155724a2 --- /dev/null +++ b/tests/snapshots/tests__ignores@multiline-ignore-table-field.lua.snap @@ -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, +}) + From 4c56680b0a443ec61dfeecec699f651dd1f34932 Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Wed, 7 Jun 2023 13:08:15 +0100 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e08b67a3..eda0de1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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