Skip to content

Commit

Permalink
Fixed formatting of Module doc comments (#5101)
Browse files Browse the repository at this point in the history
## Description
Closes #5058
  • Loading branch information
andrewvious authored Sep 8, 2023
1 parent 5f9f22b commit 1d0b9a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
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

0 comments on commit 1d0b9a5

Please sign in to comment.