diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 8ac430e416b..98554dd4cdd 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -115,7 +115,7 @@ pub(crate) fn parent_module( let name = ast_module.name().unwrap(); Ok(vec![NavigationTarget { file_id, - name: name.text(), + name: name.text().clone(), range: name.syntax().range(), kind: MODULE, ptr: None, diff --git a/crates/ra_analysis/src/symbol_index.rs b/crates/ra_analysis/src/symbol_index.rs index ed179675643..8dd15b40e57 100644 --- a/crates/ra_analysis/src/symbol_index.rs +++ b/crates/ra_analysis/src/symbol_index.rs @@ -205,7 +205,7 @@ pub(crate) struct FileSymbol { fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, LocalSyntaxPtr)> { fn decl(node: &N) -> Option<(SmolStr, LocalSyntaxPtr)> { - let name = node.name()?.text(); + let name = node.name()?.text().clone(); let ptr = LocalSyntaxPtr::new(node.syntax()); Some((name, ptr)) } diff --git a/crates/ra_editor/src/assists/add_impl.rs b/crates/ra_editor/src/assists/add_impl.rs index 9353e2717b6..2eda7cae208 100644 --- a/crates/ra_editor/src/assists/add_impl.rs +++ b/crates/ra_editor/src/assists/add_impl.rs @@ -1,6 +1,6 @@ use join_to_string::join; use ra_syntax::{ - ast::{self, AstNode, NameOwner, TypeParamsOwner}, + ast::{self, AstNode, AstToken, NameOwner, TypeParamsOwner}, TextUnit, }; diff --git a/crates/ra_hir/src/name.rs b/crates/ra_hir/src/name.rs index dee1c9c5c51..3e6ce8b9557 100644 --- a/crates/ra_hir/src/name.rs +++ b/crates/ra_hir/src/name.rs @@ -76,13 +76,13 @@ pub(crate) trait AsName { impl AsName for ast::NameRef { fn as_name(&self) -> Name { - Name::new(self.text()) + Name::new(self.text().clone()) } } impl AsName for ast::Name { fn as_name(&self) -> Name { - Name::new(self.text()) + Name::new(self.text().clone()) } } diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 82675c0e44f..59a80376181 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -177,7 +177,7 @@ pub fn macro_symbols( if let Some(name) = trait_def.name() { let dst_range = name.syntax().range(); if let Some(src_range) = exp.map_range_back(dst_range) { - res.push((name.text(), src_range + off)) + res.push((name.text().clone(), src_range + off)) } } } diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 0e303ee9863..96879ae5a38 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -23,6 +23,12 @@ fn cast(syntax: &SyntaxNode) -> Option<&Self> 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 @@ pub fn as_call(&self) -> Option<(SmolStr, &TokenTree)> { } } -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 @@ pub fn is_doc_comment(&self) -> bool { } 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 @@ pub fn has_newlines(&self) -> bool { } 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() } } diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 5e96ab1421e..547e3c0033b 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -288,6 +288,7 @@ fn to_owned(&self) -> TreePtr { TreePtr::cast(self.syntax.to_owned()) } } +impl ast::AstToken for Byte {} impl Byte {} // ByteString @@ -312,6 +313,7 @@ fn to_owned(&self) -> TreePtr { TreePtr::cast(self.syntax.to_owned() } +impl ast::AstToken for ByteString {} impl ByteString {} // CallExpr @@ -397,6 +399,7 @@ fn to_owned(&self) -> TreePtr { TreePtr::cast(self.syntax.to_owned()) } } +impl ast::AstToken for Char {} impl Char {} // Comment @@ -421,6 +424,7 @@ fn to_owned(&self) -> TreePtr { TreePtr::cast(self.syntax.to_owned()) } } +impl ast::AstToken for Comment {} impl Comment {} // Condition @@ -1270,6 +1274,7 @@ fn to_owned(&self) -> TreePtr { TreePtr::cast(self.syntax.to_owned()) } +impl ast::AstToken for Lifetime {} impl Lifetime {} // LifetimeParam @@ -2766,6 +2771,7 @@ fn to_owned(&self) -> TreePtr { TreePtr::cast(self.syntax.to_owned()) } } +impl ast::AstToken for String {} impl String {} // StructDef @@ -3391,5 +3397,6 @@ fn to_owned(&self) -> TreePtr { TreePtr::cast(self.syntax.to_owned() } +impl ast::AstToken for Whitespace {} impl Whitespace {} diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index d7505ea06d3..bddd96a5ce5 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -424,10 +424,10 @@ Grammar( "PrefixExpr": (options: ["Expr"]), "RangeExpr": (), "BinExpr": (), - "String": (), - "Byte": (), - "ByteString": (), - "Char": (), + "String": ( traits: ["AstToken"] ), + "Byte": ( traits: ["AstToken"] ), + "ByteString": ( traits: ["AstToken"] ), + "Char": ( traits: ["AstToken"] ), "Literal": (), "Expr": ( @@ -505,7 +505,7 @@ Grammar( ), "TypeParam": ( traits: ["NameOwner"] ), "LifetimeParam": ( options: [ "Lifetime" ] ), - "Lifetime": (), + "Lifetime": ( traits: ["AstToken"] ), "WhereClause": (), "ExprStmt": ( options: [ ["expr", "Expr"] ] @@ -562,7 +562,7 @@ Grammar( "PathSegment": ( options: [ "NameRef" ] ), - "Comment": (), - "Whitespace": (), + "Comment": ( traits: ["AstToken"] ), + "Whitespace": ( traits: ["AstToken"] ), }, ) diff --git a/crates/ra_syntax/src/validation/byte.rs b/crates/ra_syntax/src/validation/byte.rs index 4deb302a740..9ab4b18a3d8 100644 --- a/crates/ra_syntax/src/validation/byte.rs +++ b/crates/ra_syntax/src/validation/byte.rs @@ -1,7 +1,7 @@ //! Validation of byte literals use crate::{ - ast::{self, AstNode}, + ast::{self, AstNode, AstToken}, string_lexing::{self, StringComponentKind}, TextRange, validation::char, diff --git a/crates/ra_syntax/src/validation/byte_string.rs b/crates/ra_syntax/src/validation/byte_string.rs index 670c43a09a3..cd41a0a68e0 100644 --- a/crates/ra_syntax/src/validation/byte_string.rs +++ b/crates/ra_syntax/src/validation/byte_string.rs @@ -1,5 +1,5 @@ use crate::{ - ast::{self, AstNode}, + ast::{self, AstNode, AstToken}, string_lexing::{self, StringComponentKind}, yellow::{ SyntaxError, diff --git a/crates/ra_syntax/src/validation/char.rs b/crates/ra_syntax/src/validation/char.rs index 9cbd43fba41..169c88f56e3 100644 --- a/crates/ra_syntax/src/validation/char.rs +++ b/crates/ra_syntax/src/validation/char.rs @@ -5,7 +5,7 @@ use arrayvec::ArrayString; use crate::{ - ast::{self, AstNode}, + ast::{self, AstNode, AstToken}, string_lexing::{self, StringComponentKind}, TextRange, yellow::{ diff --git a/crates/ra_syntax/src/validation/string.rs b/crates/ra_syntax/src/validation/string.rs index 7b2a68d1208..cb86b765f8d 100644 --- a/crates/ra_syntax/src/validation/string.rs +++ b/crates/ra_syntax/src/validation/string.rs @@ -1,5 +1,5 @@ use crate::{ - ast::{self, AstNode}, + ast::{self, AstNode, AstToken}, string_lexing, yellow::{ SyntaxError,