diff options
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/handlers/merge_imports.rs | 33 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/reorder_fields.rs | 32 |
2 files changed, 43 insertions, 22 deletions
diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index 0958f52f1..ef0ce0586 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use std::iter::successors; | 1 | use std::iter::successors; |
2 | 2 | ||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | algo::{neighbor, SyntaxRewriter}, | 4 | algo::{neighbor, skip_trivia_token, SyntaxRewriter}, |
5 | ast::{self, edit::AstNodeEdit, make}, | 5 | ast::{self, edit::AstNodeEdit, make}, |
6 | AstNode, Direction, InsertPosition, SyntaxElement, T, | 6 | AstNode, Direction, InsertPosition, SyntaxElement, T, |
7 | }; | 7 | }; |
@@ -72,9 +72,18 @@ fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTre | |||
72 | let lhs = old.split_prefix(&lhs_prefix); | 72 | let lhs = old.split_prefix(&lhs_prefix); |
73 | let rhs = new.split_prefix(&rhs_prefix); | 73 | let rhs = new.split_prefix(&rhs_prefix); |
74 | 74 | ||
75 | let should_insert_comma = lhs | ||
76 | .use_tree_list()? | ||
77 | .r_curly_token() | ||
78 | .and_then(|it| skip_trivia_token(it.prev_token()?, Direction::Prev)) | ||
79 | .map(|it| it.kind() != T![,]) | ||
80 | .unwrap_or(true); | ||
81 | |||
75 | let mut to_insert: Vec<SyntaxElement> = Vec::new(); | 82 | let mut to_insert: Vec<SyntaxElement> = Vec::new(); |
76 | to_insert.push(make::token(T![,]).into()); | 83 | if should_insert_comma { |
77 | to_insert.push(make::tokens::single_space().into()); | 84 | to_insert.push(make::token(T![,]).into()); |
85 | to_insert.push(make::tokens::single_space().into()); | ||
86 | } | ||
78 | to_insert.extend( | 87 | to_insert.extend( |
79 | rhs.use_tree_list()? | 88 | rhs.use_tree_list()? |
80 | .syntax() | 89 | .syntax() |
@@ -247,4 +256,22 @@ use { | |||
247 | ", | 256 | ", |
248 | ); | 257 | ); |
249 | } | 258 | } |
259 | |||
260 | #[test] | ||
261 | fn test_double_comma() { | ||
262 | check_assist( | ||
263 | merge_imports, | ||
264 | r" | ||
265 | use foo::bar::baz; | ||
266 | use foo::<|>{ | ||
267 | FooBar, | ||
268 | }; | ||
269 | ", | ||
270 | r" | ||
271 | use foo::{<|> | ||
272 | FooBar, | ||
273 | bar::baz}; | ||
274 | ", | ||
275 | ) | ||
276 | } | ||
250 | } | 277 | } |
diff --git a/crates/ra_assists/src/handlers/reorder_fields.rs b/crates/ra_assists/src/handlers/reorder_fields.rs index 692dd1315..5cbb98d73 100644 --- a/crates/ra_assists/src/handlers/reorder_fields.rs +++ b/crates/ra_assists/src/handlers/reorder_fields.rs | |||
@@ -1,20 +1,20 @@ | |||
1 | use std::collections::HashMap; | 1 | use std::collections::HashMap; |
2 | 2 | ||
3 | use itertools::Itertools; | ||
4 | |||
5 | use hir::{Adt, ModuleDef, PathResolution, Semantics, Struct}; | 3 | use hir::{Adt, ModuleDef, PathResolution, Semantics, Struct}; |
4 | use itertools::Itertools; | ||
6 | use ra_ide_db::RootDatabase; | 5 | use ra_ide_db::RootDatabase; |
7 | use ra_syntax::{ | 6 | use ra_syntax::{ |
8 | algo, ast, | 7 | algo, |
9 | ast::{Name, Path, RecordLit, RecordPat}, | 8 | ast::{self, Path, RecordLit, RecordPat}, |
10 | AstNode, SyntaxKind, SyntaxNode, | 9 | match_ast, AstNode, SyntaxKind, |
10 | SyntaxKind::*, | ||
11 | SyntaxNode, | ||
11 | }; | 12 | }; |
12 | 13 | ||
13 | use crate::{ | 14 | use crate::{ |
14 | assist_ctx::{Assist, AssistCtx}, | 15 | assist_ctx::{Assist, AssistCtx}, |
15 | AssistId, | 16 | AssistId, |
16 | }; | 17 | }; |
17 | use ra_syntax::ast::{Expr, NameRef}; | ||
18 | 18 | ||
19 | // Assist: reorder_fields | 19 | // Assist: reorder_fields |
20 | // | 20 | // |
@@ -59,7 +59,6 @@ fn reorder<R: AstNode>(ctx: AssistCtx) -> Option<Assist> { | |||
59 | } | 59 | } |
60 | 60 | ||
61 | fn get_fields_kind(node: &SyntaxNode) -> Vec<SyntaxKind> { | 61 | fn get_fields_kind(node: &SyntaxNode) -> Vec<SyntaxKind> { |
62 | use SyntaxKind::*; | ||
63 | match node.kind() { | 62 | match node.kind() { |
64 | RECORD_LIT => vec![RECORD_FIELD], | 63 | RECORD_LIT => vec![RECORD_FIELD], |
65 | RECORD_PAT => vec![RECORD_FIELD_PAT, BIND_PAT], | 64 | RECORD_PAT => vec![RECORD_FIELD_PAT, BIND_PAT], |
@@ -68,19 +67,14 @@ fn get_fields_kind(node: &SyntaxNode) -> Vec<SyntaxKind> { | |||
68 | } | 67 | } |
69 | 68 | ||
70 | fn get_field_name(node: &SyntaxNode) -> String { | 69 | fn get_field_name(node: &SyntaxNode) -> String { |
71 | use SyntaxKind::*; | 70 | let res = match_ast! { |
72 | match node.kind() { | 71 | match node { |
73 | RECORD_FIELD => { | 72 | ast::RecordField(field) => field.field_name().map(|it| it.to_string()), |
74 | if let Some(name) = node.children().find_map(NameRef::cast) { | 73 | ast::RecordFieldPat(field) => field.field_name().map(|it| it.to_string()), |
75 | return name.to_string(); | 74 | _ => None, |
76 | } | ||
77 | node.children().find_map(Expr::cast).map(|expr| expr.to_string()).unwrap_or_default() | ||
78 | } | ||
79 | BIND_PAT | RECORD_FIELD_PAT => { | ||
80 | node.children().find_map(Name::cast).map(|n| n.to_string()).unwrap_or_default() | ||
81 | } | 75 | } |
82 | _ => String::new(), | 76 | }; |
83 | } | 77 | res.unwrap_or_default() |
84 | } | 78 | } |
85 | 79 | ||
86 | fn get_fields(record: &SyntaxNode) -> Vec<SyntaxNode> { | 80 | fn get_fields(record: &SyntaxNode) -> Vec<SyntaxNode> { |