diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/syntax_node.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/syntax_text.rs | 25 |
3 files changed, 15 insertions, 14 deletions
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 2dca4e3e8..604abe5c6 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs | |||
@@ -238,7 +238,7 @@ fn api_walkthrough() { | |||
238 | 238 | ||
239 | // You can get node's text as a `SyntaxText` object, which will traverse the | 239 | // You can get node's text as a `SyntaxText` object, which will traverse the |
240 | // tree collecting token's text: | 240 | // tree collecting token's text: |
241 | let text: SyntaxText<'_> = expr_syntax.text(); | 241 | let text: SyntaxText = expr_syntax.text(); |
242 | assert_eq!(text.to_string(), "1 + 1"); | 242 | assert_eq!(text.to_string(), "1 + 1"); |
243 | 243 | ||
244 | // There's a bunch of traversal methods on `SyntaxNode`: | 244 | // There's a bunch of traversal methods on `SyntaxNode`: |
diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index 8fe9e5b4e..51bae04de 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs | |||
@@ -68,7 +68,7 @@ impl SyntaxNode { | |||
68 | } | 68 | } |
69 | 69 | ||
70 | pub fn text(&self) -> SyntaxText { | 70 | pub fn text(&self) -> SyntaxText { |
71 | SyntaxText::new(self) | 71 | SyntaxText::new(self.clone()) |
72 | } | 72 | } |
73 | 73 | ||
74 | pub fn parent(&self) -> Option<SyntaxNode> { | 74 | pub fn parent(&self) -> Option<SyntaxNode> { |
diff --git a/crates/ra_syntax/src/syntax_text.rs b/crates/ra_syntax/src/syntax_text.rs index d8adf782b..2ad98809b 100644 --- a/crates/ra_syntax/src/syntax_text.rs +++ b/crates/ra_syntax/src/syntax_text.rs | |||
@@ -6,14 +6,15 @@ use std::{ | |||
6 | use crate::{SmolStr, SyntaxElement, SyntaxNode, TextRange, TextUnit}; | 6 | use crate::{SmolStr, SyntaxElement, SyntaxNode, TextRange, TextUnit}; |
7 | 7 | ||
8 | #[derive(Clone)] | 8 | #[derive(Clone)] |
9 | pub struct SyntaxText<'a> { | 9 | pub struct SyntaxText { |
10 | node: &'a SyntaxNode, | 10 | node: SyntaxNode, |
11 | range: TextRange, | 11 | range: TextRange, |
12 | } | 12 | } |
13 | 13 | ||
14 | impl<'a> SyntaxText<'a> { | 14 | impl SyntaxText { |
15 | pub(crate) fn new(node: &'a SyntaxNode) -> SyntaxText<'a> { | 15 | pub(crate) fn new(node: SyntaxNode) -> SyntaxText { |
16 | SyntaxText { node, range: node.range() } | 16 | let range = node.range(); |
17 | SyntaxText { node, range } | ||
17 | } | 18 | } |
18 | 19 | ||
19 | pub fn try_fold_chunks<T, F, E>(&self, init: T, mut f: F) -> Result<T, E> | 20 | pub fn try_fold_chunks<T, F, E>(&self, init: T, mut f: F) -> Result<T, E> |
@@ -95,7 +96,7 @@ impl<'a> SyntaxText<'a> { | |||
95 | self.range.is_empty() | 96 | self.range.is_empty() |
96 | } | 97 | } |
97 | 98 | ||
98 | pub fn slice(&self, range: impl ops::RangeBounds<TextUnit>) -> SyntaxText<'a> { | 99 | pub fn slice(&self, range: impl ops::RangeBounds<TextUnit>) -> SyntaxText { |
99 | let start = match range.start_bound() { | 100 | let start = match range.start_bound() { |
100 | Bound::Included(&b) => b, | 101 | Bound::Included(&b) => b, |
101 | Bound::Excluded(_) => panic!("utf-aware slicing can't work this way"), | 102 | Bound::Excluded(_) => panic!("utf-aware slicing can't work this way"), |
@@ -123,7 +124,7 @@ impl<'a> SyntaxText<'a> { | |||
123 | self.range, | 124 | self.range, |
124 | range, | 125 | range, |
125 | ); | 126 | ); |
126 | SyntaxText { node: self.node, range } | 127 | SyntaxText { node: self.node.clone(), range } |
127 | } | 128 | } |
128 | 129 | ||
129 | pub fn char_at(&self, offset: impl Into<TextUnit>) -> Option<char> { | 130 | pub fn char_at(&self, offset: impl Into<TextUnit>) -> Option<char> { |
@@ -149,25 +150,25 @@ fn found<T>(res: Result<(), T>) -> Option<T> { | |||
149 | } | 150 | } |
150 | } | 151 | } |
151 | 152 | ||
152 | impl<'a> fmt::Debug for SyntaxText<'a> { | 153 | impl fmt::Debug for SyntaxText { |
153 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 154 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
154 | fmt::Debug::fmt(&self.to_string(), f) | 155 | fmt::Debug::fmt(&self.to_string(), f) |
155 | } | 156 | } |
156 | } | 157 | } |
157 | 158 | ||
158 | impl<'a> fmt::Display for SyntaxText<'a> { | 159 | impl fmt::Display for SyntaxText { |
159 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 160 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
160 | fmt::Display::fmt(&self.to_string(), f) | 161 | fmt::Display::fmt(&self.to_string(), f) |
161 | } | 162 | } |
162 | } | 163 | } |
163 | 164 | ||
164 | impl From<SyntaxText<'_>> for String { | 165 | impl From<SyntaxText> for String { |
165 | fn from(text: SyntaxText) -> String { | 166 | fn from(text: SyntaxText) -> String { |
166 | text.to_string() | 167 | text.to_string() |
167 | } | 168 | } |
168 | } | 169 | } |
169 | 170 | ||
170 | impl PartialEq<str> for SyntaxText<'_> { | 171 | impl PartialEq<str> for SyntaxText { |
171 | fn eq(&self, mut rhs: &str) -> bool { | 172 | fn eq(&self, mut rhs: &str) -> bool { |
172 | self.try_for_each_chunk(|chunk| { | 173 | self.try_for_each_chunk(|chunk| { |
173 | if !rhs.starts_with(chunk) { | 174 | if !rhs.starts_with(chunk) { |
@@ -180,7 +181,7 @@ impl PartialEq<str> for SyntaxText<'_> { | |||
180 | } | 181 | } |
181 | } | 182 | } |
182 | 183 | ||
183 | impl PartialEq<&'_ str> for SyntaxText<'_> { | 184 | impl PartialEq<&'_ str> for SyntaxText { |
184 | fn eq(&self, rhs: &&str) -> bool { | 185 | fn eq(&self, rhs: &&str) -> bool { |
185 | self == *rhs | 186 | self == *rhs |
186 | } | 187 | } |