diff options
Diffstat (limited to 'crates/ra_ide/src/syntax_tree.rs')
-rw-r--r-- | crates/ra_ide/src/syntax_tree.rs | 277 |
1 files changed, 137 insertions, 140 deletions
diff --git a/crates/ra_ide/src/syntax_tree.rs b/crates/ra_ide/src/syntax_tree.rs index 5842ae2e8..bf97f8c56 100644 --- a/crates/ra_ide/src/syntax_tree.rs +++ b/crates/ra_ide/src/syntax_tree.rs | |||
@@ -5,7 +5,7 @@ use ra_ide_db::RootDatabase; | |||
5 | use ra_syntax::{ | 5 | use ra_syntax::{ |
6 | algo, AstNode, NodeOrToken, SourceFile, | 6 | algo, AstNode, NodeOrToken, SourceFile, |
7 | SyntaxKind::{RAW_STRING, STRING}, | 7 | SyntaxKind::{RAW_STRING, STRING}, |
8 | SyntaxToken, TextRange, TextUnit, | 8 | SyntaxToken, TextRange, TextSize, |
9 | }; | 9 | }; |
10 | 10 | ||
11 | pub use ra_db::FileId; | 11 | pub use ra_db::FileId; |
@@ -66,13 +66,10 @@ fn syntax_tree_for_token(node: &SyntaxToken, text_range: TextRange) -> Option<St | |||
66 | let len = len.min(node_len); | 66 | let len = len.min(node_len); |
67 | 67 | ||
68 | // Ensure our slice is inside the actual string | 68 | // Ensure our slice is inside the actual string |
69 | let end = if start + len < TextUnit::of_str(&text) { | 69 | let end = |
70 | start + len | 70 | if start + len < TextSize::of(&text) { start + len } else { TextSize::of(&text) - start }; |
71 | } else { | ||
72 | TextUnit::of_str(&text) - start | ||
73 | }; | ||
74 | 71 | ||
75 | let text = &text[TextRange::from_to(start, end)]; | 72 | let text = &text[TextRange::new(start, end)]; |
76 | 73 | ||
77 | // Remove possible extra string quotes from the start | 74 | // Remove possible extra string quotes from the start |
78 | // and the end of the string | 75 | // and the end of the string |
@@ -112,20 +109,20 @@ mod tests { | |||
112 | assert_eq_text!( | 109 | assert_eq_text!( |
113 | syn.trim(), | 110 | syn.trim(), |
114 | r#" | 111 | r#" |
115 | SOURCE_FILE@[0; 11) | 112 | SOURCE_FILE@0..11 |
116 | FN_DEF@[0; 11) | 113 | FN_DEF@0..11 |
117 | FN_KW@[0; 2) "fn" | 114 | FN_KW@0..2 "fn" |
118 | WHITESPACE@[2; 3) " " | 115 | WHITESPACE@2..3 " " |
119 | NAME@[3; 6) | 116 | NAME@3..6 |
120 | IDENT@[3; 6) "foo" | 117 | IDENT@3..6 "foo" |
121 | PARAM_LIST@[6; 8) | 118 | PARAM_LIST@6..8 |
122 | L_PAREN@[6; 7) "(" | 119 | L_PAREN@6..7 "(" |
123 | R_PAREN@[7; 8) ")" | 120 | R_PAREN@7..8 ")" |
124 | WHITESPACE@[8; 9) " " | 121 | WHITESPACE@8..9 " " |
125 | BLOCK_EXPR@[9; 11) | 122 | BLOCK_EXPR@9..11 |
126 | BLOCK@[9; 11) | 123 | BLOCK@9..11 |
127 | L_CURLY@[9; 10) "{" | 124 | L_CURLY@9..10 "{" |
128 | R_CURLY@[10; 11) "}" | 125 | R_CURLY@10..11 "}" |
129 | "# | 126 | "# |
130 | .trim() | 127 | .trim() |
131 | ); | 128 | ); |
@@ -145,37 +142,37 @@ fn test() { | |||
145 | assert_eq_text!( | 142 | assert_eq_text!( |
146 | syn.trim(), | 143 | syn.trim(), |
147 | r#" | 144 | r#" |
148 | SOURCE_FILE@[0; 60) | 145 | SOURCE_FILE@0..60 |
149 | FN_DEF@[0; 60) | 146 | FN_DEF@0..60 |
150 | FN_KW@[0; 2) "fn" | 147 | FN_KW@0..2 "fn" |
151 | WHITESPACE@[2; 3) " " | 148 | WHITESPACE@2..3 " " |
152 | NAME@[3; 7) | 149 | NAME@3..7 |
153 | IDENT@[3; 7) "test" | 150 | IDENT@3..7 "test" |
154 | PARAM_LIST@[7; 9) | 151 | PARAM_LIST@7..9 |
155 | L_PAREN@[7; 8) "(" | 152 | L_PAREN@7..8 "(" |
156 | R_PAREN@[8; 9) ")" | 153 | R_PAREN@8..9 ")" |
157 | WHITESPACE@[9; 10) " " | 154 | WHITESPACE@9..10 " " |
158 | BLOCK_EXPR@[10; 60) | 155 | BLOCK_EXPR@10..60 |
159 | BLOCK@[10; 60) | 156 | BLOCK@10..60 |
160 | L_CURLY@[10; 11) "{" | 157 | L_CURLY@10..11 "{" |
161 | WHITESPACE@[11; 16) "\n " | 158 | WHITESPACE@11..16 "\n " |
162 | EXPR_STMT@[16; 58) | 159 | EXPR_STMT@16..58 |
163 | MACRO_CALL@[16; 57) | 160 | MACRO_CALL@16..57 |
164 | PATH@[16; 22) | 161 | PATH@16..22 |
165 | PATH_SEGMENT@[16; 22) | 162 | PATH_SEGMENT@16..22 |
166 | NAME_REF@[16; 22) | 163 | NAME_REF@16..22 |
167 | IDENT@[16; 22) "assert" | 164 | IDENT@16..22 "assert" |
168 | BANG@[22; 23) "!" | 165 | BANG@22..23 "!" |
169 | TOKEN_TREE@[23; 57) | 166 | TOKEN_TREE@23..57 |
170 | L_PAREN@[23; 24) "(" | 167 | L_PAREN@23..24 "(" |
171 | STRING@[24; 52) "\"\n fn foo() {\n ..." | 168 | STRING@24..52 "\"\n fn foo() {\n ..." |
172 | COMMA@[52; 53) "," | 169 | COMMA@52..53 "," |
173 | WHITESPACE@[53; 54) " " | 170 | WHITESPACE@53..54 " " |
174 | STRING@[54; 56) "\"\"" | 171 | STRING@54..56 "\"\"" |
175 | R_PAREN@[56; 57) ")" | 172 | R_PAREN@56..57 ")" |
176 | SEMICOLON@[57; 58) ";" | 173 | SEMICOLON@57..58 ";" |
177 | WHITESPACE@[58; 59) "\n" | 174 | WHITESPACE@58..59 "\n" |
178 | R_CURLY@[59; 60) "}" | 175 | R_CURLY@59..60 "}" |
179 | "# | 176 | "# |
180 | .trim() | 177 | .trim() |
181 | ); | 178 | ); |
@@ -189,19 +186,19 @@ SOURCE_FILE@[0; 60) | |||
189 | assert_eq_text!( | 186 | assert_eq_text!( |
190 | syn.trim(), | 187 | syn.trim(), |
191 | r#" | 188 | r#" |
192 | FN_DEF@[0; 11) | 189 | FN_DEF@0..11 |
193 | FN_KW@[0; 2) "fn" | 190 | FN_KW@0..2 "fn" |
194 | WHITESPACE@[2; 3) " " | 191 | WHITESPACE@2..3 " " |
195 | NAME@[3; 6) | 192 | NAME@3..6 |
196 | IDENT@[3; 6) "foo" | 193 | IDENT@3..6 "foo" |
197 | PARAM_LIST@[6; 8) | 194 | PARAM_LIST@6..8 |
198 | L_PAREN@[6; 7) "(" | 195 | L_PAREN@6..7 "(" |
199 | R_PAREN@[7; 8) ")" | 196 | R_PAREN@7..8 ")" |
200 | WHITESPACE@[8; 9) " " | 197 | WHITESPACE@8..9 " " |
201 | BLOCK_EXPR@[9; 11) | 198 | BLOCK_EXPR@9..11 |
202 | BLOCK@[9; 11) | 199 | BLOCK@9..11 |
203 | L_CURLY@[9; 10) "{" | 200 | L_CURLY@9..10 "{" |
204 | R_CURLY@[10; 11) "}" | 201 | R_CURLY@10..11 "}" |
205 | "# | 202 | "# |
206 | .trim() | 203 | .trim() |
207 | ); | 204 | ); |
@@ -220,21 +217,21 @@ FN_DEF@[0; 11) | |||
220 | assert_eq_text!( | 217 | assert_eq_text!( |
221 | syn.trim(), | 218 | syn.trim(), |
222 | r#" | 219 | r#" |
223 | EXPR_STMT@[16; 58) | 220 | EXPR_STMT@16..58 |
224 | MACRO_CALL@[16; 57) | 221 | MACRO_CALL@16..57 |
225 | PATH@[16; 22) | 222 | PATH@16..22 |
226 | PATH_SEGMENT@[16; 22) | 223 | PATH_SEGMENT@16..22 |
227 | NAME_REF@[16; 22) | 224 | NAME_REF@16..22 |
228 | IDENT@[16; 22) "assert" | 225 | IDENT@16..22 "assert" |
229 | BANG@[22; 23) "!" | 226 | BANG@22..23 "!" |
230 | TOKEN_TREE@[23; 57) | 227 | TOKEN_TREE@23..57 |
231 | L_PAREN@[23; 24) "(" | 228 | L_PAREN@23..24 "(" |
232 | STRING@[24; 52) "\"\n fn foo() {\n ..." | 229 | STRING@24..52 "\"\n fn foo() {\n ..." |
233 | COMMA@[52; 53) "," | 230 | COMMA@52..53 "," |
234 | WHITESPACE@[53; 54) " " | 231 | WHITESPACE@53..54 " " |
235 | STRING@[54; 56) "\"\"" | 232 | STRING@54..56 "\"\"" |
236 | R_PAREN@[56; 57) ")" | 233 | R_PAREN@56..57 ")" |
237 | SEMICOLON@[57; 58) ";" | 234 | SEMICOLON@57..58 ";" |
238 | "# | 235 | "# |
239 | .trim() | 236 | .trim() |
240 | ); | 237 | ); |
@@ -257,21 +254,21 @@ fn bar() { | |||
257 | assert_eq_text!( | 254 | assert_eq_text!( |
258 | syn.trim(), | 255 | syn.trim(), |
259 | r#" | 256 | r#" |
260 | SOURCE_FILE@[0; 12) | 257 | SOURCE_FILE@0..12 |
261 | FN_DEF@[0; 12) | 258 | FN_DEF@0..12 |
262 | FN_KW@[0; 2) "fn" | 259 | FN_KW@0..2 "fn" |
263 | WHITESPACE@[2; 3) " " | 260 | WHITESPACE@2..3 " " |
264 | NAME@[3; 6) | 261 | NAME@3..6 |
265 | IDENT@[3; 6) "foo" | 262 | IDENT@3..6 "foo" |
266 | PARAM_LIST@[6; 8) | 263 | PARAM_LIST@6..8 |
267 | L_PAREN@[6; 7) "(" | 264 | L_PAREN@6..7 "(" |
268 | R_PAREN@[7; 8) ")" | 265 | R_PAREN@7..8 ")" |
269 | WHITESPACE@[8; 9) " " | 266 | WHITESPACE@8..9 " " |
270 | BLOCK_EXPR@[9; 12) | 267 | BLOCK_EXPR@9..12 |
271 | BLOCK@[9; 12) | 268 | BLOCK@9..12 |
272 | L_CURLY@[9; 10) "{" | 269 | L_CURLY@9..10 "{" |
273 | WHITESPACE@[10; 11) "\n" | 270 | WHITESPACE@10..11 "\n" |
274 | R_CURLY@[11; 12) "}" | 271 | R_CURLY@11..12 "}" |
275 | "# | 272 | "# |
276 | .trim() | 273 | .trim() |
277 | ); | 274 | ); |
@@ -292,21 +289,21 @@ fn bar() { | |||
292 | assert_eq_text!( | 289 | assert_eq_text!( |
293 | syn.trim(), | 290 | syn.trim(), |
294 | r#" | 291 | r#" |
295 | SOURCE_FILE@[0; 12) | 292 | SOURCE_FILE@0..12 |
296 | FN_DEF@[0; 12) | 293 | FN_DEF@0..12 |
297 | FN_KW@[0; 2) "fn" | 294 | FN_KW@0..2 "fn" |
298 | WHITESPACE@[2; 3) " " | 295 | WHITESPACE@2..3 " " |
299 | NAME@[3; 6) | 296 | NAME@3..6 |
300 | IDENT@[3; 6) "foo" | 297 | IDENT@3..6 "foo" |
301 | PARAM_LIST@[6; 8) | 298 | PARAM_LIST@6..8 |
302 | L_PAREN@[6; 7) "(" | 299 | L_PAREN@6..7 "(" |
303 | R_PAREN@[7; 8) ")" | 300 | R_PAREN@7..8 ")" |
304 | WHITESPACE@[8; 9) " " | 301 | WHITESPACE@8..9 " " |
305 | BLOCK_EXPR@[9; 12) | 302 | BLOCK_EXPR@9..12 |
306 | BLOCK@[9; 12) | 303 | BLOCK@9..12 |
307 | L_CURLY@[9; 10) "{" | 304 | L_CURLY@9..10 "{" |
308 | WHITESPACE@[10; 11) "\n" | 305 | WHITESPACE@10..11 "\n" |
309 | R_CURLY@[11; 12) "}" | 306 | R_CURLY@11..12 "}" |
310 | "# | 307 | "# |
311 | .trim() | 308 | .trim() |
312 | ); | 309 | ); |
@@ -326,36 +323,36 @@ fn bar() { | |||
326 | assert_eq_text!( | 323 | assert_eq_text!( |
327 | syn.trim(), | 324 | syn.trim(), |
328 | r#" | 325 | r#" |
329 | SOURCE_FILE@[0; 25) | 326 | SOURCE_FILE@0..25 |
330 | FN_DEF@[0; 12) | 327 | FN_DEF@0..12 |
331 | FN_KW@[0; 2) "fn" | 328 | FN_KW@0..2 "fn" |
332 | WHITESPACE@[2; 3) " " | 329 | WHITESPACE@2..3 " " |
333 | NAME@[3; 6) | 330 | NAME@3..6 |
334 | IDENT@[3; 6) "foo" | 331 | IDENT@3..6 "foo" |
335 | PARAM_LIST@[6; 8) | 332 | PARAM_LIST@6..8 |
336 | L_PAREN@[6; 7) "(" | 333 | L_PAREN@6..7 "(" |
337 | R_PAREN@[7; 8) ")" | 334 | R_PAREN@7..8 ")" |
338 | WHITESPACE@[8; 9) " " | 335 | WHITESPACE@8..9 " " |
339 | BLOCK_EXPR@[9; 12) | 336 | BLOCK_EXPR@9..12 |
340 | BLOCK@[9; 12) | 337 | BLOCK@9..12 |
341 | L_CURLY@[9; 10) "{" | 338 | L_CURLY@9..10 "{" |
342 | WHITESPACE@[10; 11) "\n" | 339 | WHITESPACE@10..11 "\n" |
343 | R_CURLY@[11; 12) "}" | 340 | R_CURLY@11..12 "}" |
344 | WHITESPACE@[12; 13) "\n" | 341 | WHITESPACE@12..13 "\n" |
345 | FN_DEF@[13; 25) | 342 | FN_DEF@13..25 |
346 | FN_KW@[13; 15) "fn" | 343 | FN_KW@13..15 "fn" |
347 | WHITESPACE@[15; 16) " " | 344 | WHITESPACE@15..16 " " |
348 | NAME@[16; 19) | 345 | NAME@16..19 |
349 | IDENT@[16; 19) "bar" | 346 | IDENT@16..19 "bar" |
350 | PARAM_LIST@[19; 21) | 347 | PARAM_LIST@19..21 |
351 | L_PAREN@[19; 20) "(" | 348 | L_PAREN@19..20 "(" |
352 | R_PAREN@[20; 21) ")" | 349 | R_PAREN@20..21 ")" |
353 | WHITESPACE@[21; 22) " " | 350 | WHITESPACE@21..22 " " |
354 | BLOCK_EXPR@[22; 25) | 351 | BLOCK_EXPR@22..25 |
355 | BLOCK@[22; 25) | 352 | BLOCK@22..25 |
356 | L_CURLY@[22; 23) "{" | 353 | L_CURLY@22..23 "{" |
357 | WHITESPACE@[23; 24) "\n" | 354 | WHITESPACE@23..24 "\n" |
358 | R_CURLY@[24; 25) "}" | 355 | R_CURLY@24..25 "}" |
359 | "# | 356 | "# |
360 | .trim() | 357 | .trim() |
361 | ); | 358 | ); |