diff options
author | Dawer <[email protected]> | 2021-05-06 06:07:06 +0100 |
---|---|---|
committer | Dawer <[email protected]> | 2021-05-06 06:07:06 +0100 |
commit | d7e169fe55a15dd684e7a93dd111c66ed49ed949 (patch) | |
tree | 5ae79079f66e095caebf1f39f5fad1be9de197dc /crates/syntax/src/token_text.rs | |
parent | dc4fa504ea2723bd5083284cabf81b4c5806ed4b (diff) |
Borrow text from nodes of immutable syntax trees
Diffstat (limited to 'crates/syntax/src/token_text.rs')
-rw-r--r-- | crates/syntax/src/token_text.rs | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/crates/syntax/src/token_text.rs b/crates/syntax/src/token_text.rs index d2ed0a12a..e29f4eea4 100644 --- a/crates/syntax/src/token_text.rs +++ b/crates/syntax/src/token_text.rs | |||
@@ -2,75 +2,81 @@ | |||
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 | pub enum TokenText<'a> { |
6 | Borrowed(&'a str), | ||
7 | Owned(rowan::GreenToken), | ||
8 | } | ||
6 | 9 | ||
7 | impl TokenText { | 10 | impl TokenText<'_> { |
8 | pub fn as_str(&self) -> &str { | 11 | pub fn as_str(&self) -> &str { |
9 | self.0.text() | 12 | match self { |
13 | TokenText::Borrowed(it) => *it, | ||
14 | TokenText::Owned(green) => green.text(), | ||
15 | } | ||
10 | } | 16 | } |
11 | } | 17 | } |
12 | 18 | ||
13 | impl ops::Deref for TokenText { | 19 | impl ops::Deref for TokenText<'_> { |
14 | type Target = str; | 20 | type Target = str; |
15 | 21 | ||
16 | fn deref(&self) -> &str { | 22 | fn deref(&self) -> &str { |
17 | self.as_str() | 23 | self.as_str() |
18 | } | 24 | } |
19 | } | 25 | } |
20 | impl AsRef<str> for TokenText { | 26 | impl AsRef<str> for TokenText<'_> { |
21 | fn as_ref(&self) -> &str { | 27 | fn as_ref(&self) -> &str { |
22 | self.as_str() | 28 | self.as_str() |
23 | } | 29 | } |
24 | } | 30 | } |
25 | 31 | ||
26 | impl From<TokenText> for String { | 32 | impl From<TokenText<'_>> for String { |
27 | fn from(token_text: TokenText) -> Self { | 33 | fn from(token_text: TokenText) -> Self { |
28 | token_text.as_str().into() | 34 | token_text.as_str().into() |
29 | } | 35 | } |
30 | } | 36 | } |
31 | 37 | ||
32 | impl PartialEq<&'_ str> for TokenText { | 38 | impl PartialEq<&'_ str> for TokenText<'_> { |
33 | fn eq(&self, other: &&str) -> bool { | 39 | fn eq(&self, other: &&str) -> bool { |
34 | self.as_str() == *other | 40 | self.as_str() == *other |
35 | } | 41 | } |
36 | } | 42 | } |
37 | impl PartialEq<TokenText> for &'_ str { | 43 | impl PartialEq<TokenText<'_>> for &'_ str { |
38 | fn eq(&self, other: &TokenText) -> bool { | 44 | fn eq(&self, other: &TokenText) -> bool { |
39 | other == self | 45 | other == self |
40 | } | 46 | } |
41 | } | 47 | } |
42 | impl PartialEq<String> for TokenText { | 48 | impl PartialEq<String> for TokenText<'_> { |
43 | fn eq(&self, other: &String) -> bool { | 49 | fn eq(&self, other: &String) -> bool { |
44 | self.as_str() == other.as_str() | 50 | self.as_str() == other.as_str() |
45 | } | 51 | } |
46 | } | 52 | } |
47 | impl PartialEq<TokenText> for String { | 53 | impl PartialEq<TokenText<'_>> for String { |
48 | fn eq(&self, other: &TokenText) -> bool { | 54 | fn eq(&self, other: &TokenText) -> bool { |
49 | other == self | 55 | other == self |
50 | } | 56 | } |
51 | } | 57 | } |
52 | impl PartialEq for TokenText { | 58 | impl PartialEq for TokenText<'_> { |
53 | fn eq(&self, other: &TokenText) -> bool { | 59 | fn eq(&self, other: &TokenText) -> bool { |
54 | self.as_str() == other.as_str() | 60 | self.as_str() == other.as_str() |
55 | } | 61 | } |
56 | } | 62 | } |
57 | impl Eq for TokenText {} | 63 | impl Eq for TokenText<'_> {} |
58 | impl Ord for TokenText { | 64 | impl Ord for TokenText<'_> { |
59 | fn cmp(&self, other: &Self) -> Ordering { | 65 | fn cmp(&self, other: &Self) -> Ordering { |
60 | self.as_str().cmp(other.as_str()) | 66 | self.as_str().cmp(other.as_str()) |
61 | } | 67 | } |
62 | } | 68 | } |
63 | impl PartialOrd for TokenText { | 69 | impl PartialOrd for TokenText<'_> { |
64 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | 70 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
65 | Some(self.cmp(other)) | 71 | Some(self.cmp(other)) |
66 | } | 72 | } |
67 | } | 73 | } |
68 | impl fmt::Display for TokenText { | 74 | impl fmt::Display for TokenText<'_> { |
69 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 75 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
70 | fmt::Display::fmt(self.as_str(), f) | 76 | fmt::Display::fmt(self.as_str(), f) |
71 | } | 77 | } |
72 | } | 78 | } |
73 | impl fmt::Debug for TokenText { | 79 | impl fmt::Debug for TokenText<'_> { |
74 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | 80 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
75 | fmt::Debug::fmt(self.as_str(), f) | 81 | fmt::Debug::fmt(self.as_str(), f) |
76 | } | 82 | } |