diff options
Diffstat (limited to 'crates/ra_ide_api/src/join_lines.rs')
-rw-r--r-- | crates/ra_ide_api/src/join_lines.rs | 581 |
1 files changed, 581 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/join_lines.rs b/crates/ra_ide_api/src/join_lines.rs new file mode 100644 index 000000000..8fb3eaa06 --- /dev/null +++ b/crates/ra_ide_api/src/join_lines.rs | |||
@@ -0,0 +1,581 @@ | |||
1 | use itertools::Itertools; | ||
2 | use ra_syntax::{ | ||
3 | SourceFile, TextRange, TextUnit, AstNode, SyntaxNode, | ||
4 | SyntaxKind::{self, WHITESPACE, COMMA, R_CURLY, R_PAREN, R_BRACK}, | ||
5 | algo::{find_covering_node, non_trivia_sibling}, | ||
6 | ast, | ||
7 | Direction, | ||
8 | }; | ||
9 | use ra_fmt::{ | ||
10 | compute_ws, extract_trivial_expression | ||
11 | }; | ||
12 | use ra_text_edit::{TextEdit, TextEditBuilder}; | ||
13 | |||
14 | pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit { | ||
15 | let range = if range.is_empty() { | ||
16 | let syntax = file.syntax(); | ||
17 | let text = syntax.text().slice(range.start()..); | ||
18 | let pos = match text.find('\n') { | ||
19 | None => return TextEditBuilder::default().finish(), | ||
20 | Some(pos) => pos, | ||
21 | }; | ||
22 | TextRange::offset_len(range.start() + pos, TextUnit::of_char('\n')) | ||
23 | } else { | ||
24 | range | ||
25 | }; | ||
26 | |||
27 | let node = find_covering_node(file.syntax(), range); | ||
28 | let mut edit = TextEditBuilder::default(); | ||
29 | for node in node.descendants() { | ||
30 | let text = match node.leaf_text() { | ||
31 | Some(text) => text, | ||
32 | None => continue, | ||
33 | }; | ||
34 | let range = match range.intersection(&node.range()) { | ||
35 | Some(range) => range, | ||
36 | None => continue, | ||
37 | } - node.range().start(); | ||
38 | for (pos, _) in text[range].bytes().enumerate().filter(|&(_, b)| b == b'\n') { | ||
39 | let pos: TextUnit = (pos as u32).into(); | ||
40 | let off = node.range().start() + range.start() + pos; | ||
41 | if !edit.invalidates_offset(off) { | ||
42 | remove_newline(&mut edit, node, text.as_str(), off); | ||
43 | } | ||
44 | } | ||
45 | } | ||
46 | |||
47 | edit.finish() | ||
48 | } | ||
49 | |||
50 | fn remove_newline( | ||
51 | edit: &mut TextEditBuilder, | ||
52 | node: &SyntaxNode, | ||
53 | node_text: &str, | ||
54 | offset: TextUnit, | ||
55 | ) { | ||
56 | if node.kind() != WHITESPACE || node_text.bytes().filter(|&b| b == b'\n').count() != 1 { | ||
57 | // The node is either the first or the last in the file | ||
58 | let suff = &node_text[TextRange::from_to( | ||
59 | offset - node.range().start() + TextUnit::of_char('\n'), | ||
60 | TextUnit::of_str(node_text), | ||
61 | )]; | ||
62 | let spaces = suff.bytes().take_while(|&b| b == b' ').count(); | ||
63 | |||
64 | edit.replace(TextRange::offset_len(offset, ((spaces + 1) as u32).into()), " ".to_string()); | ||
65 | return; | ||
66 | } | ||
67 | |||
68 | // Special case that turns something like: | ||
69 | // | ||
70 | // ``` | ||
71 | // my_function({<|> | ||
72 | // <some-expr> | ||
73 | // }) | ||
74 | // ``` | ||
75 | // | ||
76 | // into `my_function(<some-expr>)` | ||
77 | if join_single_expr_block(edit, node).is_some() { | ||
78 | return; | ||
79 | } | ||
80 | // ditto for | ||
81 | // | ||
82 | // ``` | ||
83 | // use foo::{<|> | ||
84 | // bar | ||
85 | // }; | ||
86 | // ``` | ||
87 | if join_single_use_tree(edit, node).is_some() { | ||
88 | return; | ||
89 | } | ||
90 | |||
91 | // The node is between two other nodes | ||
92 | let prev = node.prev_sibling().unwrap(); | ||
93 | let next = node.next_sibling().unwrap(); | ||
94 | if is_trailing_comma(prev.kind(), next.kind()) { | ||
95 | // Removes: trailing comma, newline (incl. surrounding whitespace) | ||
96 | edit.delete(TextRange::from_to(prev.range().start(), node.range().end())); | ||
97 | } else if prev.kind() == COMMA && next.kind() == R_CURLY { | ||
98 | // Removes: comma, newline (incl. surrounding whitespace) | ||
99 | let space = if let Some(left) = prev.prev_sibling() { compute_ws(left, next) } else { " " }; | ||
100 | edit.replace( | ||
101 | TextRange::from_to(prev.range().start(), node.range().end()), | ||
102 | space.to_string(), | ||
103 | ); | ||
104 | } else if let (Some(_), Some(next)) = (ast::Comment::cast(prev), ast::Comment::cast(next)) { | ||
105 | // Removes: newline (incl. surrounding whitespace), start of the next comment | ||
106 | edit.delete(TextRange::from_to( | ||
107 | node.range().start(), | ||
108 | next.syntax().range().start() + TextUnit::of_str(next.prefix()), | ||
109 | )); | ||
110 | } else { | ||
111 | // Remove newline but add a computed amount of whitespace characters | ||
112 | edit.replace(node.range(), compute_ws(prev, next).to_string()); | ||
113 | } | ||
114 | } | ||
115 | |||
116 | fn has_comma_after(node: &SyntaxNode) -> bool { | ||
117 | match non_trivia_sibling(node, Direction::Next) { | ||
118 | Some(n) => n.kind() == COMMA, | ||
119 | _ => false, | ||
120 | } | ||
121 | } | ||
122 | |||
123 | fn join_single_expr_block(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> { | ||
124 | let block = ast::Block::cast(node.parent()?)?; | ||
125 | let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?; | ||
126 | let expr = extract_trivial_expression(block)?; | ||
127 | |||
128 | let block_range = block_expr.syntax().range(); | ||
129 | let mut buf = expr.syntax().text().to_string(); | ||
130 | |||
131 | // Match block needs to have a comma after the block | ||
132 | if let Some(match_arm) = block_expr.syntax().parent().and_then(ast::MatchArm::cast) { | ||
133 | if !has_comma_after(match_arm.syntax()) { | ||
134 | buf.push(','); | ||
135 | } | ||
136 | } | ||
137 | |||
138 | edit.replace(block_range, buf); | ||
139 | |||
140 | Some(()) | ||
141 | } | ||
142 | |||
143 | fn join_single_use_tree(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> { | ||
144 | let use_tree_list = ast::UseTreeList::cast(node.parent()?)?; | ||
145 | let (tree,) = use_tree_list.use_trees().collect_tuple()?; | ||
146 | edit.replace(use_tree_list.syntax().range(), tree.syntax().text().to_string()); | ||
147 | Some(()) | ||
148 | } | ||
149 | |||
150 | fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool { | ||
151 | match (left, right) { | ||
152 | (COMMA, R_PAREN) | (COMMA, R_BRACK) => true, | ||
153 | _ => false, | ||
154 | } | ||
155 | } | ||
156 | |||
157 | #[cfg(test)] | ||
158 | mod tests { | ||
159 | use crate::test_utils::{assert_eq_text, check_action, extract_range}; | ||
160 | |||
161 | use super::*; | ||
162 | |||
163 | fn check_join_lines(before: &str, after: &str) { | ||
164 | check_action(before, after, |file, offset| { | ||
165 | let range = TextRange::offset_len(offset, 0.into()); | ||
166 | let res = join_lines(file, range); | ||
167 | Some(res) | ||
168 | }) | ||
169 | } | ||
170 | |||
171 | #[test] | ||
172 | fn test_join_lines_comma() { | ||
173 | check_join_lines( | ||
174 | r" | ||
175 | fn foo() { | ||
176 | <|>foo(1, | ||
177 | ) | ||
178 | } | ||
179 | ", | ||
180 | r" | ||
181 | fn foo() { | ||
182 | <|>foo(1) | ||
183 | } | ||
184 | ", | ||
185 | ); | ||
186 | } | ||
187 | |||
188 | #[test] | ||
189 | fn test_join_lines_lambda_block() { | ||
190 | check_join_lines( | ||
191 | r" | ||
192 | pub fn reparse(&self, edit: &AtomTextEdit) -> File { | ||
193 | <|>self.incremental_reparse(edit).unwrap_or_else(|| { | ||
194 | self.full_reparse(edit) | ||
195 | }) | ||
196 | } | ||
197 | ", | ||
198 | r" | ||
199 | pub fn reparse(&self, edit: &AtomTextEdit) -> File { | ||
200 | <|>self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) | ||
201 | } | ||
202 | ", | ||
203 | ); | ||
204 | } | ||
205 | |||
206 | #[test] | ||
207 | fn test_join_lines_block() { | ||
208 | check_join_lines( | ||
209 | r" | ||
210 | fn foo() { | ||
211 | foo(<|>{ | ||
212 | 92 | ||
213 | }) | ||
214 | }", | ||
215 | r" | ||
216 | fn foo() { | ||
217 | foo(<|>92) | ||
218 | }", | ||
219 | ); | ||
220 | } | ||
221 | |||
222 | #[test] | ||
223 | fn join_lines_adds_comma_for_block_in_match_arm() { | ||
224 | check_join_lines( | ||
225 | r" | ||
226 | fn foo(e: Result<U, V>) { | ||
227 | match e { | ||
228 | Ok(u) => <|>{ | ||
229 | u.foo() | ||
230 | } | ||
231 | Err(v) => v, | ||
232 | } | ||
233 | }", | ||
234 | r" | ||
235 | fn foo(e: Result<U, V>) { | ||
236 | match e { | ||
237 | Ok(u) => <|>u.foo(), | ||
238 | Err(v) => v, | ||
239 | } | ||
240 | }", | ||
241 | ); | ||
242 | } | ||
243 | |||
244 | #[test] | ||
245 | fn join_lines_keeps_comma_for_block_in_match_arm() { | ||
246 | // We already have a comma | ||
247 | check_join_lines( | ||
248 | r" | ||
249 | fn foo(e: Result<U, V>) { | ||
250 | match e { | ||
251 | Ok(u) => <|>{ | ||
252 | u.foo() | ||
253 | }, | ||
254 | Err(v) => v, | ||
255 | } | ||
256 | }", | ||
257 | r" | ||
258 | fn foo(e: Result<U, V>) { | ||
259 | match e { | ||
260 | Ok(u) => <|>u.foo(), | ||
261 | Err(v) => v, | ||
262 | } | ||
263 | }", | ||
264 | ); | ||
265 | |||
266 | // comma with whitespace between brace and , | ||
267 | check_join_lines( | ||
268 | r" | ||
269 | fn foo(e: Result<U, V>) { | ||
270 | match e { | ||
271 | Ok(u) => <|>{ | ||
272 | u.foo() | ||
273 | } , | ||
274 | Err(v) => v, | ||
275 | } | ||
276 | }", | ||
277 | r" | ||
278 | fn foo(e: Result<U, V>) { | ||
279 | match e { | ||
280 | Ok(u) => <|>u.foo() , | ||
281 | Err(v) => v, | ||
282 | } | ||
283 | }", | ||
284 | ); | ||
285 | |||
286 | // comma with newline between brace and , | ||
287 | check_join_lines( | ||
288 | r" | ||
289 | fn foo(e: Result<U, V>) { | ||
290 | match e { | ||
291 | Ok(u) => <|>{ | ||
292 | u.foo() | ||
293 | } | ||
294 | , | ||
295 | Err(v) => v, | ||
296 | } | ||
297 | }", | ||
298 | r" | ||
299 | fn foo(e: Result<U, V>) { | ||
300 | match e { | ||
301 | Ok(u) => <|>u.foo() | ||
302 | , | ||
303 | Err(v) => v, | ||
304 | } | ||
305 | }", | ||
306 | ); | ||
307 | } | ||
308 | |||
309 | #[test] | ||
310 | fn join_lines_keeps_comma_with_single_arg_tuple() { | ||
311 | // A single arg tuple | ||
312 | check_join_lines( | ||
313 | r" | ||
314 | fn foo() { | ||
315 | let x = (<|>{ | ||
316 | 4 | ||
317 | },); | ||
318 | }", | ||
319 | r" | ||
320 | fn foo() { | ||
321 | let x = (<|>4,); | ||
322 | }", | ||
323 | ); | ||
324 | |||
325 | // single arg tuple with whitespace between brace and comma | ||
326 | check_join_lines( | ||
327 | r" | ||
328 | fn foo() { | ||
329 | let x = (<|>{ | ||
330 | 4 | ||
331 | } ,); | ||
332 | }", | ||
333 | r" | ||
334 | fn foo() { | ||
335 | let x = (<|>4 ,); | ||
336 | }", | ||
337 | ); | ||
338 | |||
339 | // single arg tuple with newline between brace and comma | ||
340 | check_join_lines( | ||
341 | r" | ||
342 | fn foo() { | ||
343 | let x = (<|>{ | ||
344 | 4 | ||
345 | } | ||
346 | ,); | ||
347 | }", | ||
348 | r" | ||
349 | fn foo() { | ||
350 | let x = (<|>4 | ||
351 | ,); | ||
352 | }", | ||
353 | ); | ||
354 | } | ||
355 | |||
356 | #[test] | ||
357 | fn test_join_lines_use_items_left() { | ||
358 | // No space after the '{' | ||
359 | check_join_lines( | ||
360 | r" | ||
361 | <|>use ra_syntax::{ | ||
362 | TextUnit, TextRange, | ||
363 | };", | ||
364 | r" | ||
365 | <|>use ra_syntax::{TextUnit, TextRange, | ||
366 | };", | ||
367 | ); | ||
368 | } | ||
369 | |||
370 | #[test] | ||
371 | fn test_join_lines_use_items_right() { | ||
372 | // No space after the '}' | ||
373 | check_join_lines( | ||
374 | r" | ||
375 | use ra_syntax::{ | ||
376 | <|> TextUnit, TextRange | ||
377 | };", | ||
378 | r" | ||
379 | use ra_syntax::{ | ||
380 | <|> TextUnit, TextRange};", | ||
381 | ); | ||
382 | } | ||
383 | |||
384 | #[test] | ||
385 | fn test_join_lines_use_items_right_comma() { | ||
386 | // No space after the '}' | ||
387 | check_join_lines( | ||
388 | r" | ||
389 | use ra_syntax::{ | ||
390 | <|> TextUnit, TextRange, | ||
391 | };", | ||
392 | r" | ||
393 | use ra_syntax::{ | ||
394 | <|> TextUnit, TextRange};", | ||
395 | ); | ||
396 | } | ||
397 | |||
398 | #[test] | ||
399 | fn test_join_lines_use_tree() { | ||
400 | check_join_lines( | ||
401 | r" | ||
402 | use ra_syntax::{ | ||
403 | algo::<|>{ | ||
404 | find_leaf_at_offset, | ||
405 | }, | ||
406 | ast, | ||
407 | };", | ||
408 | r" | ||
409 | use ra_syntax::{ | ||
410 | algo::<|>find_leaf_at_offset, | ||
411 | ast, | ||
412 | };", | ||
413 | ); | ||
414 | } | ||
415 | |||
416 | #[test] | ||
417 | fn test_join_lines_normal_comments() { | ||
418 | check_join_lines( | ||
419 | r" | ||
420 | fn foo() { | ||
421 | // Hello<|> | ||
422 | // world! | ||
423 | } | ||
424 | ", | ||
425 | r" | ||
426 | fn foo() { | ||
427 | // Hello<|> world! | ||
428 | } | ||
429 | ", | ||
430 | ); | ||
431 | } | ||
432 | |||
433 | #[test] | ||
434 | fn test_join_lines_doc_comments() { | ||
435 | check_join_lines( | ||
436 | r" | ||
437 | fn foo() { | ||
438 | /// Hello<|> | ||
439 | /// world! | ||
440 | } | ||
441 | ", | ||
442 | r" | ||
443 | fn foo() { | ||
444 | /// Hello<|> world! | ||
445 | } | ||
446 | ", | ||
447 | ); | ||
448 | } | ||
449 | |||
450 | #[test] | ||
451 | fn test_join_lines_mod_comments() { | ||
452 | check_join_lines( | ||
453 | r" | ||
454 | fn foo() { | ||
455 | //! Hello<|> | ||
456 | //! world! | ||
457 | } | ||
458 | ", | ||
459 | r" | ||
460 | fn foo() { | ||
461 | //! Hello<|> world! | ||
462 | } | ||
463 | ", | ||
464 | ); | ||
465 | } | ||
466 | |||
467 | #[test] | ||
468 | fn test_join_lines_multiline_comments_1() { | ||
469 | check_join_lines( | ||
470 | r" | ||
471 | fn foo() { | ||
472 | // Hello<|> | ||
473 | /* world! */ | ||
474 | } | ||
475 | ", | ||
476 | r" | ||
477 | fn foo() { | ||
478 | // Hello<|> world! */ | ||
479 | } | ||
480 | ", | ||
481 | ); | ||
482 | } | ||
483 | |||
484 | #[test] | ||
485 | fn test_join_lines_multiline_comments_2() { | ||
486 | check_join_lines( | ||
487 | r" | ||
488 | fn foo() { | ||
489 | // The<|> | ||
490 | /* quick | ||
491 | brown | ||
492 | fox! */ | ||
493 | } | ||
494 | ", | ||
495 | r" | ||
496 | fn foo() { | ||
497 | // The<|> quick | ||
498 | brown | ||
499 | fox! */ | ||
500 | } | ||
501 | ", | ||
502 | ); | ||
503 | } | ||
504 | |||
505 | fn check_join_lines_sel(before: &str, after: &str) { | ||
506 | let (sel, before) = extract_range(before); | ||
507 | let file = SourceFile::parse(&before); | ||
508 | let result = join_lines(&file, sel); | ||
509 | let actual = result.apply(&before); | ||
510 | assert_eq_text!(after, &actual); | ||
511 | } | ||
512 | |||
513 | #[test] | ||
514 | fn test_join_lines_selection_fn_args() { | ||
515 | check_join_lines_sel( | ||
516 | r" | ||
517 | fn foo() { | ||
518 | <|>foo(1, | ||
519 | 2, | ||
520 | 3, | ||
521 | <|>) | ||
522 | } | ||
523 | ", | ||
524 | r" | ||
525 | fn foo() { | ||
526 | foo(1, 2, 3) | ||
527 | } | ||
528 | ", | ||
529 | ); | ||
530 | } | ||
531 | |||
532 | #[test] | ||
533 | fn test_join_lines_selection_struct() { | ||
534 | check_join_lines_sel( | ||
535 | r" | ||
536 | struct Foo <|>{ | ||
537 | f: u32, | ||
538 | }<|> | ||
539 | ", | ||
540 | r" | ||
541 | struct Foo { f: u32 } | ||
542 | ", | ||
543 | ); | ||
544 | } | ||
545 | |||
546 | #[test] | ||
547 | fn test_join_lines_selection_dot_chain() { | ||
548 | check_join_lines_sel( | ||
549 | r" | ||
550 | fn foo() { | ||
551 | join(<|>type_params.type_params() | ||
552 | .filter_map(|it| it.name()) | ||
553 | .map(|it| it.text())<|>) | ||
554 | }", | ||
555 | r" | ||
556 | fn foo() { | ||
557 | join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text())) | ||
558 | }", | ||
559 | ); | ||
560 | } | ||
561 | |||
562 | #[test] | ||
563 | fn test_join_lines_selection_lambda_block_body() { | ||
564 | check_join_lines_sel( | ||
565 | r" | ||
566 | pub fn handle_find_matching_brace() { | ||
567 | params.offsets | ||
568 | .map(|offset| <|>{ | ||
569 | world.analysis().matching_brace(&file, offset).unwrap_or(offset) | ||
570 | }<|>) | ||
571 | .collect(); | ||
572 | }", | ||
573 | r" | ||
574 | pub fn handle_find_matching_brace() { | ||
575 | params.offsets | ||
576 | .map(|offset| world.analysis().matching_brace(&file, offset).unwrap_or(offset)) | ||
577 | .collect(); | ||
578 | }", | ||
579 | ); | ||
580 | } | ||
581 | } | ||