|
1 | 1 | use syntax::{
|
2 | 2 | algo::non_trivia_sibling,
|
3 | 3 | ast::{self, syntax_factory::SyntaxFactory},
|
4 |
| - syntax_editor::{Element, SyntaxMapping}, |
5 |
| - AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxToken, T, |
| 4 | + syntax_editor::SyntaxMapping, |
| 5 | + AstNode, Direction, NodeOrToken, SyntaxKind, SyntaxToken, T, |
6 | 6 | };
|
7 | 7 |
|
8 | 8 | use crate::{AssistContext, AssistId, AssistKind, Assists};
|
@@ -39,37 +39,24 @@ pub(crate) fn flip_comma(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
|
39 | 39 | return None;
|
40 | 40 | }
|
41 | 41 |
|
42 |
| - let prev = match prev { |
43 |
| - SyntaxElement::Node(node) => node.syntax_element(), |
44 |
| - _ => prev, |
45 |
| - }; |
46 |
| - let next = match next { |
47 |
| - SyntaxElement::Node(node) => node.syntax_element(), |
48 |
| - _ => next, |
49 |
| - }; |
50 |
| - |
51 |
| - acc.add( |
52 |
| - AssistId("flip_comma", AssistKind::RefactorRewrite), |
53 |
| - "Flip comma", |
54 |
| - comma.text_range(), |
55 |
| - |builder| { |
56 |
| - let parent = comma.parent().unwrap(); |
57 |
| - let mut editor = builder.make_editor(&parent); |
58 |
| - |
59 |
| - if let Some(parent) = ast::TokenTree::cast(parent) { |
60 |
| - // An attribute. It often contains a path followed by a |
61 |
| - // token tree (e.g. `align(2)`), so we have to be smarter. |
62 |
| - let (new_tree, mapping) = flip_tree(parent.clone(), comma); |
63 |
| - editor.replace(parent.syntax(), new_tree.syntax()); |
64 |
| - editor.add_mappings(mapping); |
65 |
| - } else { |
66 |
| - editor.replace(prev.clone(), next.clone()); |
67 |
| - editor.replace(next.clone(), prev.clone()); |
68 |
| - } |
69 |
| - |
70 |
| - builder.add_file_edits(ctx.file_id(), editor); |
71 |
| - }, |
72 |
| - ) |
| 42 | + let target = comma.text_range(); |
| 43 | + acc.add(AssistId("flip_comma", AssistKind::RefactorRewrite), "Flip comma", target, |builder| { |
| 44 | + let parent = comma.parent().unwrap(); |
| 45 | + let mut editor = builder.make_editor(&parent); |
| 46 | + |
| 47 | + if let Some(parent) = ast::TokenTree::cast(parent) { |
| 48 | + // An attribute. It often contains a path followed by a |
| 49 | + // token tree (e.g. `align(2)`), so we have to be smarter. |
| 50 | + let (new_tree, mapping) = flip_tree(parent.clone(), comma); |
| 51 | + editor.replace(parent.syntax(), new_tree.syntax()); |
| 52 | + editor.add_mappings(mapping); |
| 53 | + } else { |
| 54 | + editor.replace(prev.clone(), next.clone()); |
| 55 | + editor.replace(next.clone(), prev.clone()); |
| 56 | + } |
| 57 | + |
| 58 | + builder.add_file_edits(ctx.file_id(), editor); |
| 59 | + }) |
73 | 60 | }
|
74 | 61 |
|
75 | 62 | fn flip_tree(tree: ast::TokenTree, comma: SyntaxToken) -> (ast::TokenTree, SyntaxMapping) {
|
|
0 commit comments