1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-31 12:37:45 +00:00

add formatting for doc-comments

This commit is contained in:
hsjobeki 2023-09-14 08:53:55 +02:00 committed by Kevin Amado
parent 0f7f0afa42
commit 264e235466
3 changed files with 44 additions and 9 deletions

View file

@ -193,15 +193,27 @@ fn dedent_comment(pos: &crate::position::Position, text: &str) -> String {
if text.starts_with('#') { if text.starts_with('#') {
text.to_string() text.to_string()
} else { } else {
let mut lines: Vec<String> = text[2..text.len() - 2] let is_doc = text.starts_with("/**") && !text.starts_with("/**/");
let mut lines: Vec<String> = if is_doc {
text[3..text.len() - 2]
.lines() .lines()
.map(|line| line.to_string()) .map(|line| line.to_string())
.collect(); .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 all lines are whitespace just return a compact comment
if lines.iter().all(|line| line.trim().is_empty()) { if lines.iter().all(|line| line.trim().is_empty()) {
if is_doc {
return "/***/".to_string();
} else {
return "/**/".to_string(); return "/**/".to_string();
} }
}
// Make sure it starts with empty line // Make sure it starts with empty line
if lines.len() == 1 { if lines.len() == 1 {
@ -279,7 +291,10 @@ fn dedent_comment(pos: &crate::position::Position, text: &str) -> String {
} }
}) })
.collect(); .collect();
if is_doc {
format!("/**{}*/", lines.join("\n"))
} else {
format!("/*{}*/", lines.join("\n")) format!("/*{}*/", lines.join("\n"))
} }
}
} }

View file

@ -18,6 +18,15 @@
/*@*/ /*@*/
/**@*/
/**@
@
@*/
/**
*/
/** /**
@ @
**/ **/

View file

@ -14,8 +14,19 @@
@ @
*/ */
/* /**
* @
*/
/**
@
@
@
*/
/***/
/**
@ @
* *
*/ */