Skip to content

Commit 9c9376c

Browse files
bors[bot]matklad
andauthored
Merge #8209
8209: Add TokenText r=lnicola a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 77a447d + 1bbac90 commit 9c9376c

File tree

3 files changed

+89
-7
lines changed

3 files changed

+89
-7
lines changed

crates/syntax/src/ast/node_ext.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ use parser::SyntaxKind;
88

99
use crate::{
1010
ast::{self, support, AstNode, AstToken, AttrsOwner, NameOwner, SyntaxNode},
11-
SmolStr, SyntaxElement, SyntaxToken, T,
11+
SmolStr, SyntaxElement, SyntaxToken, TokenText, T,
1212
};
1313

1414
impl ast::Lifetime {
15-
pub fn text(&self) -> SmolStr {
15+
pub fn text(&self) -> TokenText {
1616
text_of_first_token(self.syntax())
1717
}
1818
}
1919

2020
impl ast::Name {
21-
pub fn text(&self) -> SmolStr {
21+
pub fn text(&self) -> TokenText {
2222
text_of_first_token(self.syntax())
2323
}
2424
}
2525

2626
impl ast::NameRef {
27-
pub fn text(&self) -> SmolStr {
27+
pub fn text(&self) -> TokenText {
2828
text_of_first_token(self.syntax())
2929
}
3030

@@ -33,8 +33,11 @@ impl ast::NameRef {
3333
}
3434
}
3535

36-
fn text_of_first_token(node: &SyntaxNode) -> SmolStr {
37-
node.green().children().next().and_then(|it| it.into_token()).unwrap().text().into()
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();
39+
40+
TokenText(first_token)
3841
}
3942

4043
pub enum Macro {
@@ -376,7 +379,7 @@ impl fmt::Display for NameOrNameRef {
376379
}
377380

378381
impl NameOrNameRef {
379-
pub fn text(&self) -> SmolStr {
382+
pub fn text(&self) -> TokenText {
380383
match self {
381384
NameOrNameRef::Name(name) => name.text(),
382385
NameOrNameRef::NameRef(name_ref) => name_ref.text(),

crates/syntax/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mod syntax_error;
2929
mod parsing;
3030
mod validation;
3131
mod ptr;
32+
mod token_text;
3233
#[cfg(test)]
3334
mod tests;
3435

@@ -55,6 +56,7 @@ pub use crate::{
5556
SyntaxElement, SyntaxElementChildren, SyntaxNode, SyntaxNodeChildren, SyntaxToken,
5657
SyntaxTreeBuilder,
5758
},
59+
token_text::TokenText,
5860
};
5961
pub use parser::{SyntaxKind, T};
6062
pub use rowan::{

crates/syntax/src/token_text.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//! Yet another version of owned string, backed by a syntax tree token.
2+
3+
use std::{cmp::Ordering, fmt, ops};
4+
5+
pub struct TokenText(pub(crate) rowan::GreenToken);
6+
7+
impl TokenText {
8+
pub fn as_str(&self) -> &str {
9+
self.0.text()
10+
}
11+
}
12+
13+
impl ops::Deref for TokenText {
14+
type Target = str;
15+
16+
fn deref(&self) -> &str {
17+
self.as_str()
18+
}
19+
}
20+
impl AsRef<str> for TokenText {
21+
fn as_ref(&self) -> &str {
22+
self.as_str()
23+
}
24+
}
25+
26+
impl From<TokenText> for String {
27+
fn from(token_text: TokenText) -> Self {
28+
token_text.as_str().into()
29+
}
30+
}
31+
32+
impl PartialEq<&'_ str> for TokenText {
33+
fn eq(&self, other: &&str) -> bool {
34+
self.as_str() == *other
35+
}
36+
}
37+
impl PartialEq<TokenText> for &'_ str {
38+
fn eq(&self, other: &TokenText) -> bool {
39+
other == self
40+
}
41+
}
42+
impl PartialEq<String> for TokenText {
43+
fn eq(&self, other: &String) -> bool {
44+
self.as_str() == other.as_str()
45+
}
46+
}
47+
impl PartialEq<TokenText> for String {
48+
fn eq(&self, other: &TokenText) -> bool {
49+
other == self
50+
}
51+
}
52+
impl PartialEq for TokenText {
53+
fn eq(&self, other: &TokenText) -> bool {
54+
self.as_str() == other.as_str()
55+
}
56+
}
57+
impl Eq for TokenText {}
58+
impl Ord for TokenText {
59+
fn cmp(&self, other: &Self) -> Ordering {
60+
self.as_str().cmp(other.as_str())
61+
}
62+
}
63+
impl PartialOrd for TokenText {
64+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
65+
Some(self.cmp(other))
66+
}
67+
}
68+
impl fmt::Display for TokenText {
69+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70+
fmt::Display::fmt(self.as_str(), f)
71+
}
72+
}
73+
impl fmt::Debug for TokenText {
74+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75+
fmt::Debug::fmt(self.as_str(), f)
76+
}
77+
}

0 commit comments

Comments
 (0)