diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-10 15:32:34 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-10 15:32:34 +0000 |
commit | acd90bc33b6111b47af68d51ec668bf027afb31d (patch) | |
tree | 42e0151888d94e8f9e33d52e44771252d9e151d5 /crates/ra_ide_api_light/src/typing.rs | |
parent | 3990f93921537c4ab4db3a89b76ecfd20504d93f (diff) | |
parent | a0c978cd0ce95446f6d3e6a13047474670d9ee55 (diff) |
Merge #482
482: fix code duplication r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api_light/src/typing.rs')
-rw-r--r-- | crates/ra_ide_api_light/src/typing.rs | 490 |
1 files changed, 5 insertions, 485 deletions
diff --git a/crates/ra_ide_api_light/src/typing.rs b/crates/ra_ide_api_light/src/typing.rs index d8177f245..5726209cc 100644 --- a/crates/ra_ide_api_light/src/typing.rs +++ b/crates/ra_ide_api_light/src/typing.rs | |||
@@ -1,62 +1,12 @@ | |||
1 | use std::mem; | ||
2 | |||
3 | use itertools::Itertools; | ||
4 | use ra_syntax::{ | 1 | use ra_syntax::{ |
5 | algo::{find_node_at_offset, find_covering_node, find_leaf_at_offset, LeafAtOffset}, | 2 | algo::{find_node_at_offset, find_leaf_at_offset, LeafAtOffset}, |
6 | ast, | 3 | ast, |
7 | AstNode, Direction, SourceFile, SyntaxKind, | 4 | AstNode, Direction, SourceFile, SyntaxKind::*, |
8 | SyntaxKind::*, | 5 | SyntaxNode, TextUnit, |
9 | SyntaxNode, TextRange, TextUnit, | ||
10 | }; | 6 | }; |
11 | 7 | ||
12 | use crate::{LocalEdit, TextEditBuilder}; | 8 | use crate::{LocalEdit, TextEditBuilder}; |
13 | 9 | ||
14 | pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit { | ||
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 => { | ||
20 | return LocalEdit { | ||
21 | label: "join lines".to_string(), | ||
22 | edit: TextEditBuilder::default().finish(), | ||
23 | cursor_position: None, | ||
24 | }; | ||
25 | } | ||
26 | Some(pos) => pos, | ||
27 | }; | ||
28 | TextRange::offset_len(range.start() + pos, TextUnit::of_char('\n')) | ||
29 | } else { | ||
30 | range | ||
31 | }; | ||
32 | |||
33 | let node = find_covering_node(file.syntax(), range); | ||
34 | let mut edit = TextEditBuilder::default(); | ||
35 | for node in node.descendants() { | ||
36 | let text = match node.leaf_text() { | ||
37 | Some(text) => text, | ||
38 | None => continue, | ||
39 | }; | ||
40 | let range = match range.intersection(&node.range()) { | ||
41 | Some(range) => range, | ||
42 | None => continue, | ||
43 | } - node.range().start(); | ||
44 | for (pos, _) in text[range].bytes().enumerate().filter(|&(_, b)| b == b'\n') { | ||
45 | let pos: TextUnit = (pos as u32).into(); | ||
46 | let off = node.range().start() + range.start() + pos; | ||
47 | if !edit.invalidates_offset(off) { | ||
48 | remove_newline(&mut edit, node, text.as_str(), off); | ||
49 | } | ||
50 | } | ||
51 | } | ||
52 | |||
53 | LocalEdit { | ||
54 | label: "join lines".to_string(), | ||
55 | edit: edit.finish(), | ||
56 | cursor_position: None, | ||
57 | } | ||
58 | } | ||
59 | |||
60 | pub fn on_enter(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> { | 10 | pub fn on_enter(file: &SourceFile, offset: TextUnit) -> Option<LocalEdit> { |
61 | let comment = find_leaf_at_offset(file.syntax(), offset) | 11 | let comment = find_leaf_at_offset(file.syntax(), offset) |
62 | .left_biased() | 12 | .left_biased() |
@@ -184,441 +134,11 @@ fn last_line_indent_in_whitespace(ws: &str) -> &str { | |||
184 | ws.split('\n').last().unwrap_or("") | 134 | ws.split('\n').last().unwrap_or("") |
185 | } | 135 | } |
186 | 136 | ||
187 | fn remove_newline( | ||
188 | edit: &mut TextEditBuilder, | ||
189 | node: &SyntaxNode, | ||
190 | node_text: &str, | ||
191 | offset: TextUnit, | ||
192 | ) { | ||
193 | if node.kind() != WHITESPACE || node_text.bytes().filter(|&b| b == b'\n').count() != 1 { | ||
194 | // The node is either the first or the last in the file | ||
195 | let suff = &node_text[TextRange::from_to( | ||
196 | offset - node.range().start() + TextUnit::of_char('\n'), | ||
197 | TextUnit::of_str(node_text), | ||
198 | )]; | ||
199 | let spaces = suff.bytes().take_while(|&b| b == b' ').count(); | ||
200 | |||
201 | edit.replace( | ||
202 | TextRange::offset_len(offset, ((spaces + 1) as u32).into()), | ||
203 | " ".to_string(), | ||
204 | ); | ||
205 | return; | ||
206 | } | ||
207 | |||
208 | // Special case that turns something like: | ||
209 | // | ||
210 | // ``` | ||
211 | // my_function({<|> | ||
212 | // <some-expr> | ||
213 | // }) | ||
214 | // ``` | ||
215 | // | ||
216 | // into `my_function(<some-expr>)` | ||
217 | if join_single_expr_block(edit, node).is_some() { | ||
218 | return; | ||
219 | } | ||
220 | // ditto for | ||
221 | // | ||
222 | // ``` | ||
223 | // use foo::{<|> | ||
224 | // bar | ||
225 | // }; | ||
226 | // ``` | ||
227 | if join_single_use_tree(edit, node).is_some() { | ||
228 | return; | ||
229 | } | ||
230 | |||
231 | // The node is between two other nodes | ||
232 | let prev = node.prev_sibling().unwrap(); | ||
233 | let next = node.next_sibling().unwrap(); | ||
234 | if is_trailing_comma(prev.kind(), next.kind()) { | ||
235 | // Removes: trailing comma, newline (incl. surrounding whitespace) | ||
236 | edit.delete(TextRange::from_to(prev.range().start(), node.range().end())); | ||
237 | } else if prev.kind() == COMMA && next.kind() == R_CURLY { | ||
238 | // Removes: comma, newline (incl. surrounding whitespace) | ||
239 | let space = if let Some(left) = prev.prev_sibling() { | ||
240 | compute_ws(left, next) | ||
241 | } else { | ||
242 | " " | ||
243 | }; | ||
244 | edit.replace( | ||
245 | TextRange::from_to(prev.range().start(), node.range().end()), | ||
246 | space.to_string(), | ||
247 | ); | ||
248 | } else if let (Some(_), Some(next)) = (ast::Comment::cast(prev), ast::Comment::cast(next)) { | ||
249 | // Removes: newline (incl. surrounding whitespace), start of the next comment | ||
250 | edit.delete(TextRange::from_to( | ||
251 | node.range().start(), | ||
252 | next.syntax().range().start() + TextUnit::of_str(next.prefix()), | ||
253 | )); | ||
254 | } else { | ||
255 | // Remove newline but add a computed amount of whitespace characters | ||
256 | edit.replace(node.range(), compute_ws(prev, next).to_string()); | ||
257 | } | ||
258 | } | ||
259 | |||
260 | fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool { | ||
261 | match (left, right) { | ||
262 | (COMMA, R_PAREN) | (COMMA, R_BRACK) => true, | ||
263 | _ => false, | ||
264 | } | ||
265 | } | ||
266 | |||
267 | fn join_single_expr_block(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> { | ||
268 | let block = ast::Block::cast(node.parent()?)?; | ||
269 | let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?; | ||
270 | let expr = single_expr(block)?; | ||
271 | edit.replace( | ||
272 | block_expr.syntax().range(), | ||
273 | expr.syntax().text().to_string(), | ||
274 | ); | ||
275 | Some(()) | ||
276 | } | ||
277 | |||
278 | fn single_expr(block: &ast::Block) -> Option<&ast::Expr> { | ||
279 | let mut res = None; | ||
280 | for child in block.syntax().children() { | ||
281 | if let Some(expr) = ast::Expr::cast(child) { | ||
282 | if expr.syntax().text().contains('\n') { | ||
283 | return None; | ||
284 | } | ||
285 | if mem::replace(&mut res, Some(expr)).is_some() { | ||
286 | return None; | ||
287 | } | ||
288 | } else { | ||
289 | match child.kind() { | ||
290 | WHITESPACE | L_CURLY | R_CURLY => (), | ||
291 | _ => return None, | ||
292 | } | ||
293 | } | ||
294 | } | ||
295 | res | ||
296 | } | ||
297 | |||
298 | fn join_single_use_tree(edit: &mut TextEditBuilder, node: &SyntaxNode) -> Option<()> { | ||
299 | let use_tree_list = ast::UseTreeList::cast(node.parent()?)?; | ||
300 | let (tree,) = use_tree_list.use_trees().collect_tuple()?; | ||
301 | edit.replace( | ||
302 | use_tree_list.syntax().range(), | ||
303 | tree.syntax().text().to_string(), | ||
304 | ); | ||
305 | Some(()) | ||
306 | } | ||
307 | |||
308 | fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str { | ||
309 | match left.kind() { | ||
310 | L_PAREN | L_BRACK => return "", | ||
311 | L_CURLY => { | ||
312 | if let USE_TREE = right.kind() { | ||
313 | return ""; | ||
314 | } | ||
315 | } | ||
316 | _ => (), | ||
317 | } | ||
318 | match right.kind() { | ||
319 | R_PAREN | R_BRACK => return "", | ||
320 | R_CURLY => { | ||
321 | if let USE_TREE = left.kind() { | ||
322 | return ""; | ||
323 | } | ||
324 | } | ||
325 | DOT => return "", | ||
326 | _ => (), | ||
327 | } | ||
328 | " " | ||
329 | } | ||
330 | |||
331 | #[cfg(test)] | 137 | #[cfg(test)] |
332 | mod tests { | 138 | mod tests { |
333 | use super::*; | 139 | use crate::test_utils::{add_cursor, assert_eq_text, extract_offset}; |
334 | use crate::test_utils::{ | ||
335 | add_cursor, assert_eq_text, check_action, extract_offset, extract_range, | ||
336 | }; | ||
337 | |||
338 | fn check_join_lines(before: &str, after: &str) { | ||
339 | check_action(before, after, |file, offset| { | ||
340 | let range = TextRange::offset_len(offset, 0.into()); | ||
341 | let res = join_lines(file, range); | ||
342 | Some(res) | ||
343 | }) | ||
344 | } | ||
345 | 140 | ||
346 | #[test] | 141 | use super::*; |
347 | fn test_join_lines_comma() { | ||
348 | check_join_lines( | ||
349 | r" | ||
350 | fn foo() { | ||
351 | <|>foo(1, | ||
352 | ) | ||
353 | } | ||
354 | ", | ||
355 | r" | ||
356 | fn foo() { | ||
357 | <|>foo(1) | ||
358 | } | ||
359 | ", | ||
360 | ); | ||
361 | } | ||
362 | |||
363 | #[test] | ||
364 | fn test_join_lines_lambda_block() { | ||
365 | check_join_lines( | ||
366 | r" | ||
367 | pub fn reparse(&self, edit: &AtomTextEdit) -> File { | ||
368 | <|>self.incremental_reparse(edit).unwrap_or_else(|| { | ||
369 | self.full_reparse(edit) | ||
370 | }) | ||
371 | } | ||
372 | ", | ||
373 | r" | ||
374 | pub fn reparse(&self, edit: &AtomTextEdit) -> File { | ||
375 | <|>self.incremental_reparse(edit).unwrap_or_else(|| self.full_reparse(edit)) | ||
376 | } | ||
377 | ", | ||
378 | ); | ||
379 | } | ||
380 | |||
381 | #[test] | ||
382 | fn test_join_lines_block() { | ||
383 | check_join_lines( | ||
384 | r" | ||
385 | fn foo() { | ||
386 | foo(<|>{ | ||
387 | 92 | ||
388 | }) | ||
389 | }", | ||
390 | r" | ||
391 | fn foo() { | ||
392 | foo(<|>92) | ||
393 | }", | ||
394 | ); | ||
395 | } | ||
396 | |||
397 | #[test] | ||
398 | fn test_join_lines_use_items_left() { | ||
399 | // No space after the '{' | ||
400 | check_join_lines( | ||
401 | r" | ||
402 | <|>use ra_syntax::{ | ||
403 | TextUnit, TextRange, | ||
404 | };", | ||
405 | r" | ||
406 | <|>use ra_syntax::{TextUnit, TextRange, | ||
407 | };", | ||
408 | ); | ||
409 | } | ||
410 | |||
411 | #[test] | ||
412 | fn test_join_lines_use_items_right() { | ||
413 | // No space after the '}' | ||
414 | check_join_lines( | ||
415 | r" | ||
416 | use ra_syntax::{ | ||
417 | <|> TextUnit, TextRange | ||
418 | };", | ||
419 | r" | ||
420 | use ra_syntax::{ | ||
421 | <|> TextUnit, TextRange};", | ||
422 | ); | ||
423 | } | ||
424 | |||
425 | #[test] | ||
426 | fn test_join_lines_use_items_right_comma() { | ||
427 | // No space after the '}' | ||
428 | check_join_lines( | ||
429 | r" | ||
430 | use ra_syntax::{ | ||
431 | <|> TextUnit, TextRange, | ||
432 | };", | ||
433 | r" | ||
434 | use ra_syntax::{ | ||
435 | <|> TextUnit, TextRange};", | ||
436 | ); | ||
437 | } | ||
438 | |||
439 | #[test] | ||
440 | fn test_join_lines_use_tree() { | ||
441 | check_join_lines( | ||
442 | r" | ||
443 | use ra_syntax::{ | ||
444 | algo::<|>{ | ||
445 | find_leaf_at_offset, | ||
446 | }, | ||
447 | ast, | ||
448 | };", | ||
449 | r" | ||
450 | use ra_syntax::{ | ||
451 | algo::<|>find_leaf_at_offset, | ||
452 | ast, | ||
453 | };", | ||
454 | ); | ||
455 | } | ||
456 | |||
457 | #[test] | ||
458 | fn test_join_lines_normal_comments() { | ||
459 | check_join_lines( | ||
460 | r" | ||
461 | fn foo() { | ||
462 | // Hello<|> | ||
463 | // world! | ||
464 | } | ||
465 | ", | ||
466 | r" | ||
467 | fn foo() { | ||
468 | // Hello<|> world! | ||
469 | } | ||
470 | ", | ||
471 | ); | ||
472 | } | ||
473 | |||
474 | #[test] | ||
475 | fn test_join_lines_doc_comments() { | ||
476 | check_join_lines( | ||
477 | r" | ||
478 | fn foo() { | ||
479 | /// Hello<|> | ||
480 | /// world! | ||
481 | } | ||
482 | ", | ||
483 | r" | ||
484 | fn foo() { | ||
485 | /// Hello<|> world! | ||
486 | } | ||
487 | ", | ||
488 | ); | ||
489 | } | ||
490 | |||
491 | #[test] | ||
492 | fn test_join_lines_mod_comments() { | ||
493 | check_join_lines( | ||
494 | r" | ||
495 | fn foo() { | ||
496 | //! Hello<|> | ||
497 | //! world! | ||
498 | } | ||
499 | ", | ||
500 | r" | ||
501 | fn foo() { | ||
502 | //! Hello<|> world! | ||
503 | } | ||
504 | ", | ||
505 | ); | ||
506 | } | ||
507 | |||
508 | #[test] | ||
509 | fn test_join_lines_multiline_comments_1() { | ||
510 | check_join_lines( | ||
511 | r" | ||
512 | fn foo() { | ||
513 | // Hello<|> | ||
514 | /* world! */ | ||
515 | } | ||
516 | ", | ||
517 | r" | ||
518 | fn foo() { | ||
519 | // Hello<|> world! */ | ||
520 | } | ||
521 | ", | ||
522 | ); | ||
523 | } | ||
524 | |||
525 | #[test] | ||
526 | fn test_join_lines_multiline_comments_2() { | ||
527 | check_join_lines( | ||
528 | r" | ||
529 | fn foo() { | ||
530 | // The<|> | ||
531 | /* quick | ||
532 | brown | ||
533 | fox! */ | ||
534 | } | ||
535 | ", | ||
536 | r" | ||
537 | fn foo() { | ||
538 | // The<|> quick | ||
539 | brown | ||
540 | fox! */ | ||
541 | } | ||
542 | ", | ||
543 | ); | ||
544 | } | ||
545 | |||
546 | fn check_join_lines_sel(before: &str, after: &str) { | ||
547 | let (sel, before) = extract_range(before); | ||
548 | let file = SourceFile::parse(&before); | ||
549 | let result = join_lines(&file, sel); | ||
550 | let actual = result.edit.apply(&before); | ||
551 | assert_eq_text!(after, &actual); | ||
552 | } | ||
553 | |||
554 | #[test] | ||
555 | fn test_join_lines_selection_fn_args() { | ||
556 | check_join_lines_sel( | ||
557 | r" | ||
558 | fn foo() { | ||
559 | <|>foo(1, | ||
560 | 2, | ||
561 | 3, | ||
562 | <|>) | ||
563 | } | ||
564 | ", | ||
565 | r" | ||
566 | fn foo() { | ||
567 | foo(1, 2, 3) | ||
568 | } | ||
569 | ", | ||
570 | ); | ||
571 | } | ||
572 | |||
573 | #[test] | ||
574 | fn test_join_lines_selection_struct() { | ||
575 | check_join_lines_sel( | ||
576 | r" | ||
577 | struct Foo <|>{ | ||
578 | f: u32, | ||
579 | }<|> | ||
580 | ", | ||
581 | r" | ||
582 | struct Foo { f: u32 } | ||
583 | ", | ||
584 | ); | ||
585 | } | ||
586 | |||
587 | #[test] | ||
588 | fn test_join_lines_selection_dot_chain() { | ||
589 | check_join_lines_sel( | ||
590 | r" | ||
591 | fn foo() { | ||
592 | join(<|>type_params.type_params() | ||
593 | .filter_map(|it| it.name()) | ||
594 | .map(|it| it.text())<|>) | ||
595 | }", | ||
596 | r" | ||
597 | fn foo() { | ||
598 | join(type_params.type_params().filter_map(|it| it.name()).map(|it| it.text())) | ||
599 | }", | ||
600 | ); | ||
601 | } | ||
602 | |||
603 | #[test] | ||
604 | fn test_join_lines_selection_lambda_block_body() { | ||
605 | check_join_lines_sel( | ||
606 | r" | ||
607 | pub fn handle_find_matching_brace() { | ||
608 | params.offsets | ||
609 | .map(|offset| <|>{ | ||
610 | world.analysis().matching_brace(&file, offset).unwrap_or(offset) | ||
611 | }<|>) | ||
612 | .collect(); | ||
613 | }", | ||
614 | r" | ||
615 | pub fn handle_find_matching_brace() { | ||
616 | params.offsets | ||
617 | .map(|offset| world.analysis().matching_brace(&file, offset).unwrap_or(offset)) | ||
618 | .collect(); | ||
619 | }", | ||
620 | ); | ||
621 | } | ||
622 | 142 | ||
623 | #[test] | 143 | #[test] |
624 | fn test_on_eq_typed() { | 144 | fn test_on_eq_typed() { |