From f88e13f5393c75b02c3619ec432675c3316ee6e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Thu, 11 Oct 2018 16:25:35 +0200 Subject: Use Comment wrapper --- crates/ra_syntax/src/ast/generated.rs | 18 +++++++++++++++ crates/ra_syntax/src/ast/mod.rs | 43 +++++++++++++++++++++++++++++++++++ crates/ra_syntax/src/grammar.ron | 1 + 3 files changed, 62 insertions(+) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 2db6dff1b..b0b855eb4 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -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 { + 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> { diff --git a/crates/ra_syntax/src/ast/mod.rs b/crates/ra_syntax/src/ast/mod.rs index c1570b868..10dac72e5 100644 --- a/crates/ra_syntax/src/ast/mod.rs +++ b/crates/ra_syntax/src/ast/mod.rs @@ -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() diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 4b990fd8d..9da0c2c13 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -537,5 +537,6 @@ Grammar( "PathSegment": ( options: [ "NameRef" ] ), + "Comment": (), }, ) -- cgit v1.2.3