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

Fixed SwayFmt removing comments in configurable blocks #5297

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
11 changes: 11 additions & 0 deletions swayfmt/src/items/item_configurable/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
comments::rewrite_with_comments,
config::user_def::FieldAlignment,
formatter::{
shape::{ExprKind, LineStyle},
Expand Down Expand Up @@ -27,6 +28,8 @@ impl Format for ItemConfigurable {
.shape
.with_code_line_from(LineStyle::Multiline, ExprKind::default()),
|formatter| -> Result<(), FormatterError> {
// Required for comment formatting
let start_len = formatted_code.len();
// Add configurable token
write!(
formatted_code,
Expand Down Expand Up @@ -119,6 +122,14 @@ impl Format for ItemConfigurable {
// Handle closing brace
Self::close_curly_brace(formatted_code, formatter)?;

rewrite_with_comments::<ItemConfigurable>(
formatter,
self.span(),
self.leaf_spans(),
formatted_code,
start_len,
)?;

Ok(())
},
)?;
Expand Down
34 changes: 34 additions & 0 deletions swayfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,40 @@ impl MyContract for Contract {
);
}

#[test]
fn configurable_comments() {
check(
r#"
script;

use std::{constants::ZERO_B256, vm::evm::evm_address::EvmAddress};

configurable { /* inline comment */
// double slash comment
/// triple slash comment
SIGNER: EvmAddress = EvmAddress {
value: ZERO_B256,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be also useful to test adding comments inside of structs and enums in configurable blocks, and at the end of the line.

Suggested change
value: ZERO_B256,
// comment here
// multiline!
value: ZERO_B256, // end of line too

},
}

fn main() {}"#,
r#"script;

use std::{constants::ZERO_B256, vm::evm::evm_address::EvmAddress};

configurable { /* inline comment */
// double slash comment
/// triple slash comment
SIGNER: EvmAddress = EvmAddress {
value: ZERO_B256,
},
}

fn main() {}
"#,
);
}

#[test]
fn empty_fn() {
check(
Expand Down