From bc5bfbc3a925f1fae27b554a1b897d17240fc0b1 Mon Sep 17 00:00:00 2001 From: hsjobeki Date: Thu, 14 Sep 2023 08:53:55 +0200 Subject: [PATCH] add formatting for doc-comments --- src/alejandra/src/children.rs | 29 +++++++++++++++++------ src/alejandra/tests/cases/comment/in.nix | 9 +++++++ src/alejandra/tests/cases/comment/out.nix | 15 ++++++++++-- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/alejandra/src/children.rs b/src/alejandra/src/children.rs index fcf26fff..7263e077 100644 --- a/src/alejandra/src/children.rs +++ b/src/alejandra/src/children.rs @@ -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 = 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 = 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 @@ -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")) + } } } diff --git a/src/alejandra/tests/cases/comment/in.nix b/src/alejandra/tests/cases/comment/in.nix index 7ed19e54..8f1a3a40 100644 --- a/src/alejandra/tests/cases/comment/in.nix +++ b/src/alejandra/tests/cases/comment/in.nix @@ -17,6 +17,15 @@ */ /*@*/ + + /**@*/ + + /**@ + @ + @*/ + + /** + */ /** @ diff --git a/src/alejandra/tests/cases/comment/out.nix b/src/alejandra/tests/cases/comment/out.nix index 4004ae52..53583527 100644 --- a/src/alejandra/tests/cases/comment/out.nix +++ b/src/alejandra/tests/cases/comment/out.nix @@ -14,8 +14,19 @@ @ */ - /* - * + /** + @ + */ + + /** + @ + @ + @ + */ + + /***/ + + /** @ * */