Skip to content

Commit 55bcdab

Browse files
fix: properly format markdown lists in @dev NatSpec tags (#11696)
* Update as_doc.rs * Update buf_writer.rs * Update crates/doc/src/writer/buf_writer.rs Co-authored-by: onbjerg <[email protected]> * Update buf_writer.rs * Update buf_writer.rs * Update buf_writer.rs * fmt --------- Co-authored-by: onbjerg <[email protected]>
1 parent f7563cf commit 55bcdab

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

crates/doc/src/writer/as_doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl AsDoc for CommentsRef<'_> {
5454
// Write dev tags
5555
let devs = self.include_tag(CommentTag::Dev);
5656
for d in devs.iter() {
57-
writer.write_italic(&d.value)?;
57+
writer.write_dev_content(&d.value)?;
5858
writer.writeln()?;
5959
}
6060

crates/doc/src/writer/buf_writer.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ impl BufWriter {
8989
writeln!(self.buf, "{}", Markdown::Italic(text))
9090
}
9191

92+
/// Writes dev content to the buffer, handling markdown lists properly.
93+
/// If the content contains markdown lists, it formats them correctly.
94+
/// Otherwise, it writes the content in italics.
95+
pub fn write_dev_content(&mut self, text: &str) -> fmt::Result {
96+
for line in text.lines() {
97+
let trimmed = line.trim();
98+
if let Some(content) = trimmed.strip_prefix("- ") {
99+
writeln!(self.buf, "- *{content}*")?;
100+
} else if !trimmed.is_empty() {
101+
writeln!(self.buf, "{}", Markdown::Italic(trimmed))?;
102+
} else {
103+
writeln!(self.buf)?;
104+
}
105+
}
106+
107+
Ok(())
108+
}
109+
92110
/// Writes bold text to the buffer formatted as [Markdown::Bold].
93111
pub fn write_bold(&mut self, text: &str) -> fmt::Result {
94112
writeln!(self.buf, "{}", Markdown::Bold(text))
@@ -296,3 +314,52 @@ impl BufWriter {
296314
self.buf
297315
}
298316
}
317+
318+
#[cfg(test)]
319+
mod tests {
320+
use super::*;
321+
322+
#[test]
323+
fn test_write_dev_content_with_list() {
324+
let mut writer = BufWriter::default();
325+
let content = "This function will revert if:\n- Any of the Coolers are not owned by the caller.\n- Any of the Coolers have not been created by the CoolerFactory.\n- A duplicate Cooler is provided.";
326+
327+
writer.write_dev_content(content).expect("Failed to write dev content with list");
328+
let result = writer.finish();
329+
330+
// Check that the first line is italicized
331+
assert!(result.contains("*This function will revert if:*"));
332+
// Check that list items are properly formatted
333+
assert!(result.contains("- *Any of the Coolers are not owned by the caller.*"));
334+
assert!(
335+
result.contains("- *Any of the Coolers have not been created by the CoolerFactory.*")
336+
);
337+
assert!(result.contains("- *A duplicate Cooler is provided.*"));
338+
}
339+
340+
#[test]
341+
fn test_write_dev_content_without_list() {
342+
let mut writer = BufWriter::default();
343+
let content = "This is a simple dev comment without any lists.";
344+
345+
writer.write_dev_content(content).expect("Failed to write dev content without list");
346+
let result = writer.finish();
347+
348+
// Check that the entire content is italicized
349+
assert!(result.contains("*This is a simple dev comment without any lists.*"));
350+
}
351+
352+
#[test]
353+
fn test_write_dev_content_empty_lines() {
354+
let mut writer = BufWriter::default();
355+
let content = "This function will revert if:\n\n- First item.\n\n- Second item.";
356+
357+
writer.write_dev_content(content).expect("Failed to write dev content with empty lines");
358+
let result = writer.finish();
359+
360+
// Check that empty lines are preserved
361+
assert!(result.contains("*This function will revert if:*"));
362+
assert!(result.contains("- *First item.*"));
363+
assert!(result.contains("- *Second item.*"));
364+
}
365+
}

0 commit comments

Comments
 (0)