Skip to content

Commit

Permalink
add formatting for doc-comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hsjobeki authored and kamadorueda committed Nov 5, 2024
1 parent 0f7f0af commit 264e235
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
29 changes: 22 additions & 7 deletions src/alejandra/src/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,26 @@ fn dedent_comment(pos: &crate::position::Position, text: &str) -> String {
if text.starts_with('#') {
text.to_string()
} else {
let mut lines: Vec<String> = text[2..text.len() - 2]
.lines()
.map(|line| line.to_string())
.collect();
let is_doc = text.starts_with("/**") && !text.starts_with("/**/");
let mut lines: Vec<String> = if is_doc {
text[3..text.len() - 2]
.lines()
.map(|line| line.to_string())
.collect()
} else {
text[2..text.len() - 2]
.lines()
.map(|line| line.to_string())
.collect()
};

// If all lines are whitespace just return a compact comment
if lines.iter().all(|line| line.trim().is_empty()) {
return "/**/".to_string();
if is_doc {
return "/***/".to_string();
} else {
return "/**/".to_string();
}
}

// Make sure it starts with empty line
Expand Down Expand Up @@ -279,7 +291,10 @@ fn dedent_comment(pos: &crate::position::Position, text: &str) -> String {
}
})
.collect();

format!("/*{}*/", lines.join("\n"))
if is_doc {
format!("/**{}*/", lines.join("\n"))
} else {
format!("/*{}*/", lines.join("\n"))
}
}
}
9 changes: 9 additions & 0 deletions src/alejandra/tests/cases/comment/in.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
*/

/*@*/

/**@*/

/**@
@
@*/

/**
*/

/**
@
Expand Down
15 changes: 13 additions & 2 deletions src/alejandra/tests/cases/comment/out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@
@
*/

/*
*
/**
@
*/

/**
@
@
@
*/

/***/

/**
@
*
*/
Expand Down

0 comments on commit 264e235

Please sign in to comment.