mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-18 09:30:31 +00:00
Better handling of block doc comments
This commit is contained in:
parent
0fbfab3b45
commit
ec824a92d0
8 changed files with 158 additions and 81 deletions
|
@ -33,23 +33,20 @@ impl ast::Comment {
|
|||
prefix
|
||||
}
|
||||
|
||||
/// Returns the textual content of a doc comment block as a single string.
|
||||
/// That is, strips leading `///` (+ optional 1 character of whitespace),
|
||||
/// trailing `*/`, trailing whitespace and then joins the lines.
|
||||
/// Returns the textual content of a doc comment node as a single string with prefix and suffix
|
||||
/// removed.
|
||||
pub fn doc_comment(&self) -> Option<&str> {
|
||||
let kind = self.kind();
|
||||
match kind {
|
||||
CommentKind { shape, doc: Some(_) } => {
|
||||
let prefix = kind.prefix();
|
||||
let text = &self.text()[prefix.len()..];
|
||||
let ws = text.chars().next().filter(|c| c.is_whitespace());
|
||||
let text = ws.map_or(text, |ws| &text[ws.len_utf8()..]);
|
||||
match shape {
|
||||
CommentShape::Block if text.ends_with("*/") => {
|
||||
Some(&text[..text.len() - "*/".len()])
|
||||
}
|
||||
_ => Some(text),
|
||||
}
|
||||
let text = if shape == CommentShape::Block {
|
||||
text.strip_suffix("*/").unwrap_or(text)
|
||||
} else {
|
||||
text
|
||||
};
|
||||
Some(text)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue