Use Comment wrapper

This commit is contained in:
Adolfo Ochagavía 2018-10-11 16:25:35 +02:00
parent 27a86cb7df
commit f88e13f539
4 changed files with 78 additions and 36 deletions

View file

@ -227,6 +227,24 @@ impl<'a> AstNode<'a> for CastExpr<'a> {
impl<'a> CastExpr<'a> {}
// Comment
#[derive(Debug, Clone, Copy)]
pub struct Comment<'a> {
syntax: SyntaxNodeRef<'a>,
}
impl<'a> AstNode<'a> for Comment<'a> {
fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
match syntax.kind() {
COMMENT => Some(Comment { syntax }),
_ => None,
}
}
fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
}
impl<'a> Comment<'a> {}
// Condition
#[derive(Debug, Clone, Copy)]
pub struct Condition<'a> {

View file

@ -99,6 +99,49 @@ impl<'a> Lifetime<'a> {
}
}
impl<'a> Comment<'a> {
pub fn text(&self) -> SmolStr {
self.syntax().leaf_text().unwrap().clone()
}
pub fn flavor(&self) -> CommentFlavor {
let text = self.text();
if text.starts_with("///") {
CommentFlavor::Doc
} else if text.starts_with("//!") {
CommentFlavor::ModuleDoc
} else if text.starts_with("//") {
CommentFlavor::Line
} else {
CommentFlavor::Multiline
}
}
pub fn prefix(&self) -> &'static str {
self.flavor().prefix()
}
}
#[derive(Debug)]
pub enum CommentFlavor {
Line,
Doc,
ModuleDoc,
Multiline
}
impl CommentFlavor {
pub fn prefix(&self) -> &'static str {
use self::CommentFlavor::*;
match *self {
Line => "//",
Doc => "///",
ModuleDoc => "//!",
Multiline => "/*"
}
}
}
impl<'a> Name<'a> {
pub fn text(&self) -> SmolStr {
let ident = self.syntax().first_child()

View file

@ -537,5 +537,6 @@ Grammar(
"PathSegment": (
options: [ "NameRef" ]
),
"Comment": (),
},
)