From fa6e0b0d38d2a030b959be91232927b9c096272b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 12:23:10 +0300 Subject: itroduce trait for ast tokens --- crates/ra_syntax/src/ast.rs | 52 +++++++++------------------------------------ 1 file changed, 10 insertions(+), 42 deletions(-) (limited to 'crates/ra_syntax/src/ast.rs') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 0e303ee98..96879ae5a 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -23,6 +23,12 @@ pub trait AstNode: rowan::TransparentNewType> fn to_owned(&self) -> TreePtr; } +pub trait AstToken: AstNode { + fn text(&self) -> &SmolStr { + self.syntax().leaf_text().unwrap() + } +} + pub trait NameOwner: AstNode { fn name(&self) -> Option<&Name> { child_opt(self) @@ -155,41 +161,7 @@ impl Attr { } } -impl Lifetime { - pub fn text(&self) -> SmolStr { - self.syntax().leaf_text().unwrap().clone() - } -} - -impl Char { - pub fn text(&self) -> &SmolStr { - &self.syntax().leaf_text().unwrap() - } -} - -impl Byte { - pub fn text(&self) -> &SmolStr { - &self.syntax().leaf_text().unwrap() - } -} - -impl ByteString { - pub fn text(&self) -> &SmolStr { - &self.syntax().leaf_text().unwrap() - } -} - -impl String { - pub fn text(&self) -> &SmolStr { - &self.syntax().leaf_text().unwrap() - } -} - impl Comment { - pub fn text(&self) -> &SmolStr { - self.syntax().leaf_text().unwrap() - } - pub fn flavor(&self) -> CommentFlavor { let text = self.text(); if text.starts_with("///") { @@ -248,10 +220,6 @@ impl CommentFlavor { } impl Whitespace { - pub fn text(&self) -> &SmolStr { - &self.syntax().leaf_text().unwrap() - } - pub fn count_newlines_lazy(&self) -> impl Iterator { self.text().chars().filter(|&c| c == '\n').map(|_| &()) } @@ -262,16 +230,16 @@ impl Whitespace { } impl Name { - pub fn text(&self) -> SmolStr { + pub fn text(&self) -> &SmolStr { let ident = self.syntax().first_child().unwrap(); - ident.leaf_text().unwrap().clone() + ident.leaf_text().unwrap() } } impl NameRef { - pub fn text(&self) -> SmolStr { + pub fn text(&self) -> &SmolStr { let ident = self.syntax().first_child().unwrap(); - ident.leaf_text().unwrap().clone() + ident.leaf_text().unwrap() } } -- cgit v1.2.3