fix: don't count dedent on the empty comment lines (#1690)

* fix: don't count dedent on the empty comment lines

* fix: more tests

* test: update snapshot
This commit is contained in:
Myriad-Dreamin 2025-04-30 21:47:07 +08:00 committed by GitHub
parent 3103f3933c
commit 170dd7b948
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 88 additions and 14 deletions

View file

@ -1,5 +1,7 @@
//! Convenient utilities to match comment in code.
use itertools::Itertools;
use crate::prelude::*;
/// Extract the module-level documentation from a source.
@ -190,10 +192,18 @@ impl DocCommentMatcher {
});
let comments = comments.collect::<Vec<_>>();
let dedent = comments.iter().fold(usize::MAX, |acc, content| {
let indent = content.chars().take_while(|ch| ch.is_whitespace()).count();
acc.min(indent)
});
let dedent = comments
.iter()
.flat_map(|line| {
let mut chars = line.chars();
let cnt = chars
.by_ref()
.peeking_take_while(|c| c.is_whitespace())
.count();
chars.next().map(|_| cnt)
})
.min()
.unwrap_or(0);
let size_hint = comments.iter().map(|comment| comment.len()).sum::<usize>();
let mut comments = comments
@ -213,3 +223,72 @@ impl DocCommentMatcher {
res
}
}
#[cfg(test)]
mod tests {
use super::*;
fn test(it: &str) -> String {
find_module_level_docs(&Source::detached(it)).unwrap()
}
#[test]
fn simple() {
assert_eq!(
test(
r#"/// foo
/// bar
#let main() = printf("hello World")"#
),
"foo\nbar"
);
}
#[test]
fn dedent() {
assert_eq!(
test(
r#"/// a
/// b
/// c
#let main() = printf("hello World")"#
),
"a\nb\nc"
);
assert_eq!(
test(
r#"///a
/// b
/// c
#let main() = printf("hello World")"#
),
"a\n b\n c"
);
}
#[test]
fn issue_1687_postive() {
assert_eq!(
test(
r#"/// Description.
///
/// Note.
#let main() = printf("hello World")"#
),
"Description.\n\nNote."
);
}
#[test]
fn issue_1687_negative() {
assert_eq!(
test(
r#"/// Description.
///
/// Note.
#let main() = printf("hello World")"#
),
"Description.\n\nNote."
);
}
}