aboutsummaryrefslogtreecommitdiff
path: root/crates/syntax/src/ast
diff options
context:
space:
mode:
authorDawer <[email protected]>2021-05-06 06:07:06 +0100
committerDawer <[email protected]>2021-05-06 06:07:06 +0100
commitd7e169fe55a15dd684e7a93dd111c66ed49ed949 (patch)
tree5ae79079f66e095caebf1f39f5fad1be9de197dc /crates/syntax/src/ast
parentdc4fa504ea2723bd5083284cabf81b4c5806ed4b (diff)
Borrow text from nodes of immutable syntax trees
Diffstat (limited to 'crates/syntax/src/ast')
-rw-r--r--crates/syntax/src/ast/node_ext.rs36
1 files changed, 27 insertions, 9 deletions
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs
index 492fbc4a0..8e6d7b092 100644
--- a/crates/syntax/src/ast/node_ext.rs
+++ b/crates/syntax/src/ast/node_ext.rs
@@ -1,10 +1,11 @@
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, NodeOrToken};
8 9
9use crate::{ 10use crate::{
10 ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode}, 11 ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode},
@@ -12,19 +13,19 @@ use crate::{
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,28 @@ impl ast::NameRef {
33 } 34 }
34} 35}
35 36
36fn text_of_first_token(node: &SyntaxNode) -> TokenText { 37fn _text_of_first_token(node: &SyntaxNode) -> Cow<'_, str> {
37 let first_token = 38 fn cow_map<F: FnOnce(&GreenNodeData) -> &str>(green: Cow<GreenNodeData>, f: F) -> Cow<str> {
38 node.green().children().next().and_then(|it| it.into_token()).unwrap().to_owned(); 39 match green {
40 Cow::Borrowed(green_ref) => Cow::Borrowed(f(green_ref)),
41 Cow::Owned(green) => Cow::Owned(f(&green).to_owned()),
42 }
43 }
44
45 cow_map(node.green(), |green_ref| {
46 green_ref.children().next().and_then(NodeOrToken::into_token).unwrap().text()
47 })
48}
39 49
40 TokenText(first_token) 50fn text_of_first_token(node: &SyntaxNode) -> TokenText<'_> {
51 fn first_token(green_ref: &GreenNodeData) -> &GreenTokenData {
52 green_ref.children().next().and_then(NodeOrToken::into_token).unwrap()
53 }
54
55 match node.green() {
56 Cow::Borrowed(green_ref) => TokenText::Borrowed(first_token(green_ref).text()),
57 Cow::Owned(green) => TokenText::Owned(first_token(&green).to_owned()),
58 }
41} 59}
42 60
43#[derive(Debug, PartialEq, Eq, Clone)] 61#[derive(Debug, PartialEq, Eq, Clone)]
@@ -412,7 +430,7 @@ impl fmt::Display for NameOrNameRef {
412} 430}
413 431
414impl NameOrNameRef { 432impl NameOrNameRef {
415 pub fn text(&self) -> TokenText { 433 pub fn text(&self) -> TokenText<'_> {
416 match self { 434 match self {
417 NameOrNameRef::Name(name) => name.text(), 435 NameOrNameRef::Name(name) => name.text(),
418 NameOrNameRef::NameRef(name_ref) => name_ref.text(), 436 NameOrNameRef::NameRef(name_ref) => name_ref.text(),