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 formatting of Module doc comments #5101

Merged
merged 1 commit into from
Sep 8, 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
8 changes: 4 additions & 4 deletions swayfmt/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,23 @@ impl Formatter {

self.with_comments_context(src)?;

let module = parse_file(&self.source_engine, Arc::from(src), path.clone())?.value;
module.format(&mut raw_formatted_code, self)?;
let annotated_module = parse_file(&self.source_engine, Arc::from(src), path.clone())?;
annotated_module.format(&mut raw_formatted_code, self)?;

let mut formatted_code = String::from(&raw_formatted_code);

// Write post-module comments
write_comments(
&mut formatted_code,
module.span().end()..src.len() + 1,
annotated_module.value.span().end()..src.len() + 1,
self,
)?;

// Add newline sequences
handle_newlines(
&self.source_engine,
Arc::from(src),
&module,
&annotated_module.value,
Arc::from(formatted_code.clone()),
path,
&mut formatted_code,
Expand Down
14 changes: 14 additions & 0 deletions swayfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ fn check(unformatted: &str, expected: &str) {
check_with_formatter(unformatted, expected, &mut formatter);
}

#[test]
fn module_doc_comments_persist() {
check(
r#"

//! this is a module level doc comment
library;
"#,
r#"//! this is a module level doc comment
library;
"#,
)
}

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