Skip to content

Commit 0ee945e

Browse files
bors[bot]iDawer
andauthored
Merge #8280
8280: Borrow text of immutable syntax node r=iDawer a=iDawer In rust-analyzer/rowan#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 rust-analyzer/rowan#100 (comment) Co-authored-by: Dawer <[email protected]>
2 parents 6fccb15 + 90a5dca commit 0ee945e

File tree

7 files changed

+55
-32
lines changed

7 files changed

+55
-32
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir_expand/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
283283
};
284284
let loc = db.lookup_intern_macro(id);
285285
let arg = loc.kind.arg(db)?;
286-
Some(arg.green())
286+
Some(arg.green().into())
287287
}
288288

289289
fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<TokenExpander>> {

crates/syntax/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ doctest = false
1313
[dependencies]
1414
cov-mark = { version = "1.1", features = ["thread-local"] }
1515
itertools = "0.10.0"
16-
rowan = "=0.13.0-pre.3"
16+
rowan = "=0.13.0-pre.5"
1717
rustc_lexer = { version = "716.0.0", package = "rustc-ap-rustc_lexer" }
1818
rustc-hash = "1.1.0"
1919
arrayvec = "0.7"

crates/syntax/src/algo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ impl SyntaxRewriter<'_> {
555555

556556
fn element_to_green(element: SyntaxElement) -> NodeOrToken<rowan::GreenNode, rowan::GreenToken> {
557557
match element {
558-
NodeOrToken::Node(it) => NodeOrToken::Node(it.green()),
558+
NodeOrToken::Node(it) => NodeOrToken::Node(it.green().into_owned()),
559559
NodeOrToken::Token(it) => NodeOrToken::Token(it.green().to_owned()),
560560
}
561561
}

crates/syntax/src/ast/make.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ fn ast_from_text<N: AstNode>(text: &str) -> N {
572572
}
573573

574574
fn unroot(n: SyntaxNode) -> SyntaxNode {
575-
SyntaxNode::new_root(n.green())
575+
SyntaxNode::new_root(n.green().into())
576576
}
577577

578578
pub mod tokens {

crates/syntax/src/ast/node_ext.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
//! Various extension methods to ast Nodes, which are hard to code-generate.
22
//! Extensions for various expressions live in a sibling `expr_extensions` module.
33
4-
use std::{fmt, iter::successors};
4+
use std::{borrow::Cow, fmt, iter::successors};
55

66
use itertools::Itertools;
77
use parser::SyntaxKind;
8+
use rowan::{GreenNodeData, GreenTokenData};
89

910
use crate::{
1011
ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode},
11-
SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
12+
NodeOrToken, SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
1213
};
1314

1415
impl ast::Lifetime {
15-
pub fn text(&self) -> TokenText {
16+
pub fn text(&self) -> TokenText<'_> {
1617
text_of_first_token(self.syntax())
1718
}
1819
}
1920

2021
impl ast::Name {
21-
pub fn text(&self) -> TokenText {
22+
pub fn text(&self) -> TokenText<'_> {
2223
text_of_first_token(self.syntax())
2324
}
2425
}
2526

2627
impl ast::NameRef {
27-
pub fn text(&self) -> TokenText {
28+
pub fn text(&self) -> TokenText<'_> {
2829
text_of_first_token(self.syntax())
2930
}
3031

@@ -33,11 +34,15 @@ impl ast::NameRef {
3334
}
3435
}
3536

36-
fn text_of_first_token(node: &SyntaxNode) -> TokenText {
37-
let first_token =
38-
node.green().children().next().and_then(|it| it.into_token()).unwrap().to_owned();
37+
fn text_of_first_token(node: &SyntaxNode) -> TokenText<'_> {
38+
fn first_token(green_ref: &GreenNodeData) -> &GreenTokenData {
39+
green_ref.children().next().and_then(NodeOrToken::into_token).unwrap()
40+
}
3941

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+
}
4146
}
4247

4348
#[derive(Debug, PartialEq, Eq, Clone)]
@@ -412,7 +417,7 @@ impl fmt::Display for NameOrNameRef {
412417
}
413418

414419
impl NameOrNameRef {
415-
pub fn text(&self) -> TokenText {
420+
pub fn text(&self) -> TokenText<'_> {
416421
match self {
417422
NameOrNameRef::Name(name) => name.text(),
418423
NameOrNameRef::NameRef(name_ref) => name_ref.text(),

crates/syntax/src/token_text.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,93 @@
22
33
use std::{cmp::Ordering, fmt, ops};
44

5-
pub struct TokenText(pub(crate) rowan::GreenToken);
5+
use rowan::GreenToken;
6+
7+
pub struct TokenText<'a>(pub(crate) Repr<'a>);
8+
9+
pub(crate) enum Repr<'a> {
10+
Borrowed(&'a str),
11+
Owned(GreenToken),
12+
}
13+
14+
impl<'a> TokenText<'a> {
15+
pub(crate) fn borrowed(text: &'a str) -> Self {
16+
TokenText(Repr::Borrowed(text))
17+
}
18+
19+
pub(crate) fn owned(green: GreenToken) -> Self {
20+
TokenText(Repr::Owned(green))
21+
}
622

7-
impl TokenText {
823
pub fn as_str(&self) -> &str {
9-
self.0.text()
24+
match self.0 {
25+
Repr::Borrowed(it) => it,
26+
Repr::Owned(ref green) => green.text(),
27+
}
1028
}
1129
}
1230

13-
impl ops::Deref for TokenText {
31+
impl ops::Deref for TokenText<'_> {
1432
type Target = str;
1533

1634
fn deref(&self) -> &str {
1735
self.as_str()
1836
}
1937
}
20-
impl AsRef<str> for TokenText {
38+
impl AsRef<str> for TokenText<'_> {
2139
fn as_ref(&self) -> &str {
2240
self.as_str()
2341
}
2442
}
2543

26-
impl From<TokenText> for String {
44+
impl From<TokenText<'_>> for String {
2745
fn from(token_text: TokenText) -> Self {
2846
token_text.as_str().into()
2947
}
3048
}
3149

32-
impl PartialEq<&'_ str> for TokenText {
50+
impl PartialEq<&'_ str> for TokenText<'_> {
3351
fn eq(&self, other: &&str) -> bool {
3452
self.as_str() == *other
3553
}
3654
}
37-
impl PartialEq<TokenText> for &'_ str {
55+
impl PartialEq<TokenText<'_>> for &'_ str {
3856
fn eq(&self, other: &TokenText) -> bool {
3957
other == self
4058
}
4159
}
42-
impl PartialEq<String> for TokenText {
60+
impl PartialEq<String> for TokenText<'_> {
4361
fn eq(&self, other: &String) -> bool {
4462
self.as_str() == other.as_str()
4563
}
4664
}
47-
impl PartialEq<TokenText> for String {
65+
impl PartialEq<TokenText<'_>> for String {
4866
fn eq(&self, other: &TokenText) -> bool {
4967
other == self
5068
}
5169
}
52-
impl PartialEq for TokenText {
70+
impl PartialEq for TokenText<'_> {
5371
fn eq(&self, other: &TokenText) -> bool {
5472
self.as_str() == other.as_str()
5573
}
5674
}
57-
impl Eq for TokenText {}
58-
impl Ord for TokenText {
75+
impl Eq for TokenText<'_> {}
76+
impl Ord for TokenText<'_> {
5977
fn cmp(&self, other: &Self) -> Ordering {
6078
self.as_str().cmp(other.as_str())
6179
}
6280
}
63-
impl PartialOrd for TokenText {
81+
impl PartialOrd for TokenText<'_> {
6482
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
6583
Some(self.cmp(other))
6684
}
6785
}
68-
impl fmt::Display for TokenText {
86+
impl fmt::Display for TokenText<'_> {
6987
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7088
fmt::Display::fmt(self.as_str(), f)
7189
}
7290
}
73-
impl fmt::Debug for TokenText {
91+
impl fmt::Debug for TokenText<'_> {
7492
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7593
fmt::Debug::fmt(self.as_str(), f)
7694
}

0 commit comments

Comments
 (0)