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

Closed
wants to merge 13 commits into 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
38 changes: 38 additions & 0 deletions swayfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,44 @@ impl MyContract for Contract {
);
}

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

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

configurable { /* multiline */
// double slash
/// triple slash
SIGNER: EvmAddress = EvmAddress {
// double slash
// double slash
value: ZERO_B256, // end of line
},
}

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

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

configurable { /* multiline */
// double slash
/// triple slash
SIGNER: EvmAddress = EvmAddress {
// double slash
// double slash
value: ZERO_B256, // end of line
},
}

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

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