From 1bbac9053decfe235949acf33f9bc574b0ad1575 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 26 Mar 2021 21:33:45 +0300 Subject: Add TokenText --- crates/syntax/src/ast/node_ext.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'crates/syntax/src/ast') diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 6d7db5fb2..2772d7364 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -8,23 +8,23 @@ use parser::SyntaxKind; use crate::{ ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode}, - SmolStr, SyntaxElement, SyntaxToken, T, + SmolStr, SyntaxElement, SyntaxToken, TokenText, T, }; impl ast::Lifetime { - pub fn text(&self) -> SmolStr { + pub fn text(&self) -> TokenText { text_of_first_token(self.syntax()) } } impl ast::Name { - pub fn text(&self) -> SmolStr { + pub fn text(&self) -> TokenText { text_of_first_token(self.syntax()) } } impl ast::NameRef { - pub fn text(&self) -> SmolStr { + pub fn text(&self) -> TokenText { text_of_first_token(self.syntax()) } @@ -33,8 +33,11 @@ impl ast::NameRef { } } -fn text_of_first_token(node: &SyntaxNode) -> SmolStr { - node.green().children().next().and_then(|it| it.into_token()).unwrap().text().into() +fn text_of_first_token(node: &SyntaxNode) -> TokenText { + let first_token = + node.green().children().next().and_then(|it| it.into_token()).unwrap().to_owned(); + + TokenText(first_token) } pub enum Macro { @@ -376,7 +379,7 @@ impl fmt::Display for NameOrNameRef { } impl NameOrNameRef { - pub fn text(&self) -> SmolStr { + pub fn text(&self) -> TokenText { match self { NameOrNameRef::Name(name) => name.text(), NameOrNameRef::NameRef(name_ref) => name_ref.text(), -- cgit v1.2.3