aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/ast
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-05-06 23:19:52 +0100
committerGitHub <[email protected]>2021-05-06 23:19:52 +0100
commit0ee945e289c438e87d4701401059e8c407710e7b (patch)
tree6b16e28da049a1f627b26eda1a2595a6b3cd84cd /crates/syntax/src/ast
parent6fccb152b4646877e38dc29dce1b0cd826eb6908 (diff)
parent90a5dca0a2dbb858ee34eb7de06de8a1cad9e488 (diff)
Merge #8280
8280: Borrow text of immutable syntax node r=iDawer a=iDawer In https://github.com/rust-analyzer/rowan/pull/101 `rowan::SyntaxNode::green` returns `Cow<'_, GreenNodeData>`. It returns borrow of green node of immutable syntax tree node. Using this we can return borrowed text from `ast::Name::text`. ~~However now it allocates in case of mutable syntax trees.~~ (see next comment) The idea comes from https://github.com/rust-analyzer/rowan/pull/100#issuecomment-809330325 Co-authored-by: Dawer <[email protected]>
Diffstat (limited to 'crates/syntax/src/ast')
-rw-r--r--crates/syntax/src/ast/make.rs2
-rw-r--r--crates/syntax/src/ast/node_ext.rs25
2 files changed, 16 insertions, 11 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index 42da09606..4bcea28cc 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -572,7 +572,7 @@ fn ast_from_text<N: AstNode>(text: &str) -> N {
572} 572}
573 573
574fn unroot(n: SyntaxNode) -> SyntaxNode { 574fn unroot(n: SyntaxNode) -> SyntaxNode {
575 SyntaxNode::new_root(n.green()) 575 SyntaxNode::new_root(n.green().into())
576} 576}
577 577
578pub mod tokens { 578pub mod tokens {
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs
index 492fbc4a0..bef49238f 100644
--- a/crates/syntax/src/ast/node_ext.rs
+++ b/crates/syntax/src/ast/node_ext.rs
@@ -1,30 +1,31 @@
1//! Various extension methods to ast Nodes, which are hard to code-generate. 1//! Various extension methods to ast Nodes, which are hard to code-generate.
2//! Extensions for various expressions live in a sibling `expr_extensions` module. 2//! Extensions for various expressions live in a sibling `expr_extensions` module.
3 3
4use std::{fmt, iter::successors}; 4use std::{borrow::Cow, fmt, iter::successors};
5 5
6use itertools::Itertools; 6use itertools::Itertools;
7use parser::SyntaxKind; 7use parser::SyntaxKind;
8use rowan::{GreenNodeData, GreenTokenData};
8 9
9use crate::{ 10use crate::{
10 ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode}, 11 ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode},
11 SmolStr, SyntaxElement, SyntaxToken, TokenText, T, 12 NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
12}; 13};
13 14
14impl ast::Lifetime { 15impl ast::Lifetime {
15 pub fn text(&self) -> TokenText { 16 pub fn text(&self) -> TokenText<'_> {
16 text_of_first_token(self.syntax()) 17 text_of_first_token(self.syntax())
17 } 18 }
18} 19}
19 20
20impl ast::Name { 21impl ast::Name {
21 pub fn text(&self) -> TokenText { 22 pub fn text(&self) -> TokenText<'_> {
22 text_of_first_token(self.syntax()) 23 text_of_first_token(self.syntax())
23 } 24 }
24} 25}
25 26
26impl ast::NameRef { 27impl ast::NameRef {
27 pub fn text(&self) -> TokenText { 28 pub fn text(&self) -> TokenText<'_> {
28 text_of_first_token(self.syntax()) 29 text_of_first_token(self.syntax())
29 } 30 }
30 31
@@ -33,11 +34,15 @@ impl ast::NameRef {
33 } 34 }
34} 35}
35 36
36fn text_of_first_token(node: &SyntaxNode) -> TokenText { 37fn text_of_first_token(node: &SyntaxNode) -> TokenText<'_> {
37 let first_token = 38 fn first_token(green_ref: &GreenNodeData) -> &GreenTokenData {
38 node.green().children().next().and_then(|it| it.into_token()).unwrap().to_owned(); 39 green_ref.children().next().and_then(NodeOrToken::into_token).unwrap()
40 }
39 41
40 TokenText(first_token) 42 match node.green() {
43 Cow::Borrowed(green_ref) => TokenText::borrowed(first_token(green_ref).text()),
44 Cow::Owned(green) => TokenText::owned(first_token(&green).to_owned()),
45 }
41} 46}
42 47
43#[derive(Debug, PartialEq, Eq, Clone)] 48#[derive(Debug, PartialEq, Eq, Clone)]
@@ -412,7 +417,7 @@ impl fmt::Display for NameOrNameRef {
412} 417}
413 418
414impl NameOrNameRef { 419impl NameOrNameRef {
415 pub fn text(&self) -> TokenText { 420 pub fn text(&self) -> TokenText<'_> {
416 match self { 421 match self {
417 NameOrNameRef::Name(name) => name.text(), 422 NameOrNameRef::Name(name) => name.text(),
418 NameOrNameRef::NameRef(name_ref) => name_ref.text(), 423 NameOrNameRef::NameRef(name_ref) => name_ref.text(),