diff options
Diffstat (limited to 'crates/syntax/src/token_text.rs')
-rw-r--r-- | crates/syntax/src/token_text.rs | 50 |
1 files changed, 34 insertions, 16 deletions
diff --git a/crates/syntax/src/token_text.rs b/crates/syntax/src/token_text.rs index d2ed0a12a..f3e8b321a 100644 --- a/crates/syntax/src/token_text.rs +++ b/crates/syntax/src/token_text.rs | |||
@@ -2,75 +2,93 @@ | |||
2 | 2 | ||
3 | use std::{cmp::Ordering, fmt, ops}; | 3 | use std::{cmp::Ordering, fmt, ops}; |
4 | 4 | ||
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 | } | ||
6 | 22 | ||
7 | impl TokenText { | ||
8 | pub fn as_str(&self) -> &str { | 23 | 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 | } | ||
10 | } | 28 | } |
11 | } | 29 | } |
12 | 30 | ||
13 | impl ops::Deref for TokenText { | 31 | impl ops::Deref for TokenText<'_> { |
14 | type Target = str; | 32 | type Target = str; |
15 | 33 | ||
16 | fn deref(&self) -> &str { | 34 | fn deref(&self) -> &str { |
17 | self.as_str() | 35 | self.as_str() |
18 | } | 36 | } |
19 | } | 37 | } |
20 | impl AsRef<str> for TokenText { | 38 | impl AsRef<str> for TokenText<'_> { |
21 | fn as_ref(&self) -> &str { | 39 | fn as_ref(&self) -> &str { |
22 | self.as_str() | 40 | self.as_str() |
23 | } | 41 | } |
24 | } | 42 | } |
25 | 43 | ||
26 | impl From<TokenText> for String { | 44 | impl From<TokenText<'_>> for String { |
27 | fn from(token_text: TokenText) -> Self { | 45 | fn from(token_text: TokenText) -> Self { |
28 | token_text.as_str().into() | 46 | token_text.as_str().into() |
29 | } | 47 | } |
30 | } | 48 | } |
31 | 49 | ||
32 | impl PartialEq<&'_ str> for TokenText { | 50 | impl PartialEq<&'_ str> for TokenText<'_> { |
33 | fn eq(&self, other: &&str) -> bool { | 51 | fn eq(&self, other: &&str) -> bool { |
34 | self.as_str() == *other | 52 | self.as_str() == *other |
35 | } | 53 | } |
36 | } | 54 | } |
37 | impl PartialEq<TokenText> for &'_ str { | 55 | impl PartialEq<TokenText<'_>> for &'_ str { |
38 | fn eq(&self, other: &TokenText) -> bool { | 56 | fn eq(&self, other: &TokenText) -> bool { |
39 | other == self | 57 | other == self |
40 | } | 58 | } |
41 | } | 59 | } |
42 | impl PartialEq<String> for TokenText { | 60 | impl PartialEq<String> for TokenText<'_> { |
43 | fn eq(&self, other: &String) -> bool { | 61 | fn eq(&self, other: &String) -> bool { |
44 | self.as_str() == other.as_str() | 62 | self.as_str() == other.as_str() |
45 | } | 63 | } |
46 | } | 64 | } |
47 | impl PartialEq<TokenText> for String { | 65 | impl PartialEq<TokenText<'_>> for String { |
48 | fn eq(&self, other: &TokenText) -> bool { | 66 | fn eq(&self, other: &TokenText) -> bool { |
49 | other == self | 67 | other == self |
50 | } | 68 | } |
51 | } | 69 | } |
52 | impl PartialEq for TokenText { | 70 | impl PartialEq for TokenText<'_> { |
53 | fn eq(&self, other: &TokenText) -> bool { | 71 | fn eq(&self, other: &TokenText) -> bool { |
54 | self.as_str() == other.as_str() | 72 | self.as_str() == other.as_str() |
55 | } | 73 | } |
56 | } | 74 | } |
57 | impl Eq for TokenText {} | 75 | impl Eq for TokenText<'_> {} |
58 | impl Ord for TokenText { | 76 | impl Ord for TokenText<'_> { |
59 | fn cmp(&self, other: &Self) -> Ordering { | 77 | fn cmp(&self, other: &Self) -> Ordering { |
60 | self.as_str().cmp(other.as_str()) | 78 | self.as_str().cmp(other.as_str()) |
61 | } | 79 | } |
62 | } | 80 | } |
63 | impl PartialOrd for TokenText { | 81 | impl PartialOrd for TokenText<'_> { |
64 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | 82 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
65 | Some(self.cmp(other)) | 83 | Some(self.cmp(other)) |
66 | } | 84 | } |
67 | } | 85 | } |
68 | impl fmt::Display for TokenText { | 86 | impl fmt::Display for TokenText<'_> { |
69 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 87 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
70 | fmt::Display::fmt(self.as_str(), f) | 88 | fmt::Display::fmt(self.as_str(), f) |
71 | } | 89 | } |
72 | } | 90 | } |
73 | impl fmt::Debug for TokenText { | 91 | impl fmt::Debug for TokenText<'_> { |
74 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 92 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
75 | fmt::Debug::fmt(self.as_str(), f) | 93 | fmt::Debug::fmt(self.as_str(), f) |
76 | } | 94 | } |