mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-18 01:20:20 +00:00
Simplify ast::Comment api surface
This commit is contained in:
parent
2facd9517f
commit
3174e941db
1 changed files with 15 additions and 10 deletions
|
@ -18,20 +18,21 @@ impl ast::Comment {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prefix(&self) -> &'static str {
|
pub fn prefix(&self) -> &'static str {
|
||||||
let &(prefix, _kind) = CommentKind::with_prefix_from_text(self.text());
|
let &(prefix, _kind) = CommentKind::BY_PREFIX
|
||||||
|
.iter()
|
||||||
|
.find(|&(prefix, kind)| self.kind() == *kind && self.text().starts_with(prefix))
|
||||||
|
.unwrap();
|
||||||
prefix
|
prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn kind_and_prefix(&self) -> &(&'static str, CommentKind) {
|
|
||||||
CommentKind::with_prefix_from_text(self.text())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the textual content of a doc comment block as a single string.
|
/// Returns the textual content of a doc comment block as a single string.
|
||||||
/// That is, strips leading `///` (+ optional 1 character of whitespace),
|
/// That is, strips leading `///` (+ optional 1 character of whitespace),
|
||||||
/// trailing `*/`, trailing whitespace and then joins the lines.
|
/// trailing `*/`, trailing whitespace and then joins the lines.
|
||||||
pub fn doc_comment(&self) -> Option<&str> {
|
pub fn doc_comment(&self) -> Option<&str> {
|
||||||
match self.kind_and_prefix() {
|
let kind = self.kind();
|
||||||
(prefix, CommentKind { shape, doc: Some(_) }) => {
|
match kind {
|
||||||
|
CommentKind { shape, doc: Some(_) } => {
|
||||||
|
let prefix = kind.prefix();
|
||||||
let text = &self.text().as_str()[prefix.len()..];
|
let text = &self.text().as_str()[prefix.len()..];
|
||||||
let ws = text.chars().next().filter(|c| c.is_whitespace());
|
let ws = text.chars().next().filter(|c| c.is_whitespace());
|
||||||
let text = ws.map_or(text, |ws| &text[ws.len_utf8()..]);
|
let text = ws.map_or(text, |ws| &text[ws.len_utf8()..]);
|
||||||
|
@ -88,12 +89,16 @@ impl CommentKind {
|
||||||
];
|
];
|
||||||
|
|
||||||
pub(crate) fn from_text(text: &str) -> CommentKind {
|
pub(crate) fn from_text(text: &str) -> CommentKind {
|
||||||
let &(_prefix, kind) = Self::with_prefix_from_text(text);
|
let &(_prefix, kind) = CommentKind::BY_PREFIX
|
||||||
|
.iter()
|
||||||
|
.find(|&(prefix, _kind)| text.starts_with(prefix))
|
||||||
|
.unwrap();
|
||||||
kind
|
kind
|
||||||
}
|
}
|
||||||
|
|
||||||
fn with_prefix_from_text(text: &str) -> &(&'static str, CommentKind) {
|
fn prefix(&self) -> &'static str {
|
||||||
CommentKind::BY_PREFIX.iter().find(|&(prefix, _kind)| text.starts_with(prefix)).unwrap()
|
let &(prefix, _) = CommentKind::BY_PREFIX.iter().find(|(_, kind)| kind == self).unwrap();
|
||||||
|
prefix
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue