diff options
author | IceSentry <[email protected]> | 2020-04-13 02:29:14 +0100 |
---|---|---|
committer | IceSentry <[email protected]> | 2020-04-13 02:29:14 +0100 |
commit | abdf725c558f860b47883a6843bfd2a5a7c633bd (patch) | |
tree | ccdaed7162ae2e36208aa5cac532f90f8c87d844 | |
parent | c388130f5ffbcbe7d3131213a24d12d02f769b87 (diff) |
Fix double comma when merge imports on second line
This fixes the a bug when merging imports from the second line when it already has a comma it would previously insert a comma.
There's probably a better way to check for a COMMA.
This also ends up with a weird indentation, but rust-fmt can easily deal with it so I'm not sure how to resolve that.
Closes #3832
-rw-r--r-- | crates/ra_assists/src/handlers/merge_imports.rs | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index 0958f52f1..ab1fb2a0b 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs | |||
@@ -3,7 +3,8 @@ use std::iter::successors; | |||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | algo::{neighbor, SyntaxRewriter}, | 4 | algo::{neighbor, SyntaxRewriter}, |
5 | ast::{self, edit::AstNodeEdit, make}, | 5 | ast::{self, edit::AstNodeEdit, make}, |
6 | AstNode, Direction, InsertPosition, SyntaxElement, T, | 6 | AstNode, Direction, InsertPosition, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxToken, Token, |
7 | T, | ||
7 | }; | 8 | }; |
8 | 9 | ||
9 | use crate::{Assist, AssistCtx, AssistId}; | 10 | use crate::{Assist, AssistCtx, AssistId}; |
@@ -72,9 +73,24 @@ fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTre | |||
72 | let lhs = old.split_prefix(&lhs_prefix); | 73 | let lhs = old.split_prefix(&lhs_prefix); |
73 | let rhs = new.split_prefix(&rhs_prefix); | 74 | let rhs = new.split_prefix(&rhs_prefix); |
74 | 75 | ||
76 | let mut should_insert_comma = true; | ||
77 | |||
78 | lhs.syntax() | ||
79 | .last_child() | ||
80 | .and_then(|child| child.children().last()) | ||
81 | .and_then(|last| last.next_sibling_or_token()) | ||
82 | .map(|next_sibling| { | ||
83 | // FIXME: there's probably a better way to check if it's a COMMA | ||
84 | if let NodeOrToken::Token(maybe_comma) = next_sibling { | ||
85 | should_insert_comma = maybe_comma.to_string() != ","; | ||
86 | } | ||
87 | }); | ||
88 | |||
75 | let mut to_insert: Vec<SyntaxElement> = Vec::new(); | 89 | let mut to_insert: Vec<SyntaxElement> = Vec::new(); |
76 | to_insert.push(make::token(T![,]).into()); | 90 | if should_insert_comma { |
77 | to_insert.push(make::tokens::single_space().into()); | 91 | to_insert.push(make::token(T![,]).into()); |
92 | to_insert.push(make::tokens::single_space().into()); | ||
93 | } | ||
78 | to_insert.extend( | 94 | to_insert.extend( |
79 | rhs.use_tree_list()? | 95 | rhs.use_tree_list()? |
80 | .syntax() | 96 | .syntax() |
@@ -247,4 +263,22 @@ use { | |||
247 | ", | 263 | ", |
248 | ); | 264 | ); |
249 | } | 265 | } |
266 | |||
267 | #[test] | ||
268 | fn test_double_comma() { | ||
269 | check_assist( | ||
270 | merge_imports, | ||
271 | r" | ||
272 | use hyper::service::make_service_fn; | ||
273 | use hyper::<|>{ | ||
274 | StatusCode, | ||
275 | }; | ||
276 | ", | ||
277 | r" | ||
278 | use hyper::{<|> | ||
279 | StatusCode, | ||
280 | service::make_service_fn}; | ||
281 | ", | ||
282 | ) | ||
283 | } | ||
250 | } | 284 | } |