diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-18 18:35:11 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-18 18:35:11 +0000 |
commit | b28d41186698a6e2aa3d679ee63dd48304ca4262 (patch) | |
tree | 7be0684f1c2660eb5da089d1067d13a343e8bcff /crates/ra_assists/src | |
parent | 12c952f8010afb583989a6e72f81ded4aa9110d2 (diff) | |
parent | 3f6dc20d3cf3fa101552a9067b98a1314260a679 (diff) |
Merge #3640
3640: Merge imports assist r=matklad a=matklad
Work towards #2220
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r-- | crates/ra_assists/src/doc_tests/generated.rs | 14 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/merge_imports.rs | 154 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/move_bounds.rs | 4 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/split_import.rs | 29 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 2 |
5 files changed, 178 insertions, 25 deletions
diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index 3f56dd508..aef6793e8 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs | |||
@@ -418,6 +418,20 @@ fn main() { | |||
418 | } | 418 | } |
419 | 419 | ||
420 | #[test] | 420 | #[test] |
421 | fn doctest_merge_imports() { | ||
422 | check( | ||
423 | "merge_imports", | ||
424 | r#####" | ||
425 | use std::<|>fmt::Formatter; | ||
426 | use std::io; | ||
427 | "#####, | ||
428 | r#####" | ||
429 | use std::{fmt::Formatter, io}; | ||
430 | "#####, | ||
431 | ) | ||
432 | } | ||
433 | |||
434 | #[test] | ||
421 | fn doctest_merge_match_arms() { | 435 | fn doctest_merge_match_arms() { |
422 | check( | 436 | check( |
423 | "merge_match_arms", | 437 | "merge_match_arms", |
diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs new file mode 100644 index 000000000..96b1ab86a --- /dev/null +++ b/crates/ra_assists/src/handlers/merge_imports.rs | |||
@@ -0,0 +1,154 @@ | |||
1 | use std::iter::successors; | ||
2 | |||
3 | use ast::{edit::AstNodeEdit, make}; | ||
4 | use ra_syntax::{ast, AstNode, AstToken, Direction, InsertPosition, SyntaxElement, T}; | ||
5 | |||
6 | use crate::{Assist, AssistCtx, AssistId}; | ||
7 | |||
8 | // Assist: merge_imports | ||
9 | // | ||
10 | // Merges two imports with a common prefix. | ||
11 | // | ||
12 | // ``` | ||
13 | // use std::<|>fmt::Formatter; | ||
14 | // use std::io; | ||
15 | // ``` | ||
16 | // -> | ||
17 | // ``` | ||
18 | // use std::{fmt::Formatter, io}; | ||
19 | // ``` | ||
20 | pub(crate) fn merge_imports(ctx: AssistCtx) -> Option<Assist> { | ||
21 | let tree: ast::UseTree = ctx.find_node_at_offset()?; | ||
22 | let use_item = tree.syntax().parent().and_then(ast::UseItem::cast)?; | ||
23 | let (merged, to_delete) = [Direction::Prev, Direction::Next] | ||
24 | .iter() | ||
25 | .copied() | ||
26 | .filter_map(|dir| next_use_item(&use_item, dir)) | ||
27 | .filter_map(|it| Some((it.clone(), it.use_tree()?))) | ||
28 | .find_map(|(use_item, use_tree)| { | ||
29 | Some((try_merge_trees(&tree, &use_tree)?, use_item.clone())) | ||
30 | })?; | ||
31 | let mut offset = ctx.frange.range.start(); | ||
32 | ctx.add_assist(AssistId("merge_imports"), "Merge imports", |edit| { | ||
33 | edit.replace_ast(tree, merged); | ||
34 | |||
35 | let mut range = to_delete.syntax().text_range(); | ||
36 | let next_ws = to_delete | ||
37 | .syntax() | ||
38 | .next_sibling_or_token() | ||
39 | .and_then(|it| it.into_token()) | ||
40 | .and_then(ast::Whitespace::cast); | ||
41 | if let Some(ws) = next_ws { | ||
42 | range = range.extend_to(&ws.syntax().text_range()) | ||
43 | } | ||
44 | edit.delete(range); | ||
45 | if range.end() <= offset { | ||
46 | offset -= range.len(); | ||
47 | } | ||
48 | edit.set_cursor(offset); | ||
49 | }) | ||
50 | } | ||
51 | |||
52 | fn next_use_item(this_use_item: &ast::UseItem, direction: Direction) -> Option<ast::UseItem> { | ||
53 | this_use_item.syntax().siblings(direction).skip(1).find_map(ast::UseItem::cast) | ||
54 | } | ||
55 | |||
56 | fn try_merge_trees(old: &ast::UseTree, new: &ast::UseTree) -> Option<ast::UseTree> { | ||
57 | let lhs_path = old.path()?; | ||
58 | let rhs_path = new.path()?; | ||
59 | |||
60 | let (lhs_prefix, rhs_prefix) = common_prefix(&lhs_path, &rhs_path)?; | ||
61 | |||
62 | let lhs = old.split_prefix(&lhs_prefix); | ||
63 | let rhs = new.split_prefix(&rhs_prefix); | ||
64 | |||
65 | let mut to_insert: Vec<SyntaxElement> = Vec::new(); | ||
66 | to_insert.push(make::token(T![,]).into()); | ||
67 | to_insert.push(make::tokens::single_space().into()); | ||
68 | to_insert.extend( | ||
69 | rhs.use_tree_list()? | ||
70 | .syntax() | ||
71 | .children_with_tokens() | ||
72 | .filter(|it| it.kind() != T!['{'] && it.kind() != T!['}']), | ||
73 | ); | ||
74 | let use_tree_list = lhs.use_tree_list()?; | ||
75 | let pos = InsertPosition::Before(use_tree_list.r_curly()?.into()); | ||
76 | let use_tree_list = use_tree_list.insert_children(pos, to_insert); | ||
77 | Some(lhs.with_use_tree_list(use_tree_list)) | ||
78 | } | ||
79 | |||
80 | fn common_prefix(lhs: &ast::Path, rhs: &ast::Path) -> Option<(ast::Path, ast::Path)> { | ||
81 | let mut res = None; | ||
82 | let mut lhs_curr = first_path(&lhs); | ||
83 | let mut rhs_curr = first_path(&rhs); | ||
84 | loop { | ||
85 | match (lhs_curr.segment(), rhs_curr.segment()) { | ||
86 | (Some(lhs), Some(rhs)) if lhs.syntax().text() == rhs.syntax().text() => (), | ||
87 | _ => break, | ||
88 | } | ||
89 | res = Some((lhs_curr.clone(), rhs_curr.clone())); | ||
90 | |||
91 | match (lhs_curr.parent_path(), rhs_curr.parent_path()) { | ||
92 | (Some(lhs), Some(rhs)) => { | ||
93 | lhs_curr = lhs; | ||
94 | rhs_curr = rhs; | ||
95 | } | ||
96 | _ => break, | ||
97 | } | ||
98 | } | ||
99 | |||
100 | res | ||
101 | } | ||
102 | |||
103 | fn first_path(path: &ast::Path) -> ast::Path { | ||
104 | successors(Some(path.clone()), |it| it.qualifier()).last().unwrap() | ||
105 | } | ||
106 | |||
107 | #[cfg(test)] | ||
108 | mod tests { | ||
109 | use crate::helpers::check_assist; | ||
110 | |||
111 | use super::*; | ||
112 | |||
113 | #[test] | ||
114 | fn test_merge_first() { | ||
115 | check_assist( | ||
116 | merge_imports, | ||
117 | r" | ||
118 | use std::fmt<|>::Debug; | ||
119 | use std::fmt::Display; | ||
120 | ", | ||
121 | r" | ||
122 | use std::fmt<|>::{Debug, Display}; | ||
123 | ", | ||
124 | ) | ||
125 | } | ||
126 | |||
127 | #[test] | ||
128 | fn test_merge_second() { | ||
129 | check_assist( | ||
130 | merge_imports, | ||
131 | r" | ||
132 | use std::fmt::Debug; | ||
133 | use std::fmt<|>::Display; | ||
134 | ", | ||
135 | r" | ||
136 | use std::fmt<|>::{Display, Debug}; | ||
137 | ", | ||
138 | ) | ||
139 | } | ||
140 | |||
141 | #[test] | ||
142 | #[ignore] | ||
143 | fn test_merge_nested() { | ||
144 | check_assist( | ||
145 | merge_imports, | ||
146 | r" | ||
147 | use std::{fmt<|>::Debug, fmt::Display}; | ||
148 | ", | ||
149 | r" | ||
150 | use std::{fmt::{Debug, Display}}; | ||
151 | ", | ||
152 | ) | ||
153 | } | ||
154 | } | ||
diff --git a/crates/ra_assists/src/handlers/move_bounds.rs b/crates/ra_assists/src/handlers/move_bounds.rs index 0b501f3e5..342a770ec 100644 --- a/crates/ra_assists/src/handlers/move_bounds.rs +++ b/crates/ra_assists/src/handlers/move_bounds.rs | |||
@@ -1,5 +1,5 @@ | |||
1 | use ra_syntax::{ | 1 | use ra_syntax::{ |
2 | ast::{self, edit, make, AstNode, NameOwner, TypeBoundsOwner}, | 2 | ast::{self, edit::AstNodeEdit, make, AstNode, NameOwner, TypeBoundsOwner}, |
3 | SyntaxElement, | 3 | SyntaxElement, |
4 | SyntaxKind::*, | 4 | SyntaxKind::*, |
5 | }; | 5 | }; |
@@ -54,7 +54,7 @@ pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx) -> Option<Assist> { | |||
54 | (type_param, without_bounds) | 54 | (type_param, without_bounds) |
55 | }); | 55 | }); |
56 | 56 | ||
57 | let new_type_param_list = edit::replace_descendants(&type_param_list, new_params); | 57 | let new_type_param_list = type_param_list.replace_descendants(new_params); |
58 | edit.replace_ast(type_param_list.clone(), new_type_param_list); | 58 | edit.replace_ast(type_param_list.clone(), new_type_param_list); |
59 | 59 | ||
60 | let where_clause = { | 60 | let where_clause = { |
diff --git a/crates/ra_assists/src/handlers/split_import.rs b/crates/ra_assists/src/handlers/split_import.rs index 292c39f59..d9244f22d 100644 --- a/crates/ra_assists/src/handlers/split_import.rs +++ b/crates/ra_assists/src/handlers/split_import.rs | |||
@@ -1,9 +1,6 @@ | |||
1 | use std::iter::{once, successors}; | 1 | use std::iter::successors; |
2 | 2 | ||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ast, AstNode, T}; |
4 | ast::{self, make}, | ||
5 | AstNode, T, | ||
6 | }; | ||
7 | 4 | ||
8 | use crate::{Assist, AssistCtx, AssistId}; | 5 | use crate::{Assist, AssistCtx, AssistId}; |
9 | 6 | ||
@@ -25,7 +22,10 @@ pub(crate) fn split_import(ctx: AssistCtx) -> Option<Assist> { | |||
25 | 22 | ||
26 | let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast)?; | 23 | let use_tree = top_path.syntax().ancestors().find_map(ast::UseTree::cast)?; |
27 | 24 | ||
28 | let new_tree = split_use_tree_prefix(&use_tree, &path)?; | 25 | let new_tree = use_tree.split_prefix(&path); |
26 | if new_tree == use_tree { | ||
27 | return None; | ||
28 | } | ||
29 | let cursor = ctx.frange.range.start(); | 29 | let cursor = ctx.frange.range.start(); |
30 | 30 | ||
31 | ctx.add_assist(AssistId("split_import"), "Split import", |edit| { | 31 | ctx.add_assist(AssistId("split_import"), "Split import", |edit| { |
@@ -35,23 +35,6 @@ pub(crate) fn split_import(ctx: AssistCtx) -> Option<Assist> { | |||
35 | }) | 35 | }) |
36 | } | 36 | } |
37 | 37 | ||
38 | fn split_use_tree_prefix(use_tree: &ast::UseTree, prefix: &ast::Path) -> Option<ast::UseTree> { | ||
39 | let suffix = split_path_prefix(&prefix)?; | ||
40 | let use_tree = make::use_tree(suffix.clone(), use_tree.use_tree_list(), use_tree.alias()); | ||
41 | let nested = make::use_tree_list(once(use_tree)); | ||
42 | let res = make::use_tree(prefix.clone(), Some(nested), None); | ||
43 | Some(res) | ||
44 | } | ||
45 | |||
46 | fn split_path_prefix(prefix: &ast::Path) -> Option<ast::Path> { | ||
47 | let parent = prefix.parent_path()?; | ||
48 | let mut res = make::path_unqualified(parent.segment()?); | ||
49 | for p in successors(parent.parent_path(), |it| it.parent_path()) { | ||
50 | res = make::path_qualified(res, p.segment()?); | ||
51 | } | ||
52 | Some(res) | ||
53 | } | ||
54 | |||
55 | #[cfg(test)] | 38 | #[cfg(test)] |
56 | mod tests { | 39 | mod tests { |
57 | use crate::helpers::{check_assist, check_assist_target}; | 40 | use crate::helpers::{check_assist, check_assist_target}; |
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 260ada716..b8704ea7d 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -110,6 +110,7 @@ mod handlers { | |||
110 | mod inline_local_variable; | 110 | mod inline_local_variable; |
111 | mod introduce_variable; | 111 | mod introduce_variable; |
112 | mod invert_if; | 112 | mod invert_if; |
113 | mod merge_imports; | ||
113 | mod merge_match_arms; | 114 | mod merge_match_arms; |
114 | mod move_bounds; | 115 | mod move_bounds; |
115 | mod move_guard; | 116 | mod move_guard; |
@@ -140,6 +141,7 @@ mod handlers { | |||
140 | inline_local_variable::inline_local_variable, | 141 | inline_local_variable::inline_local_variable, |
141 | introduce_variable::introduce_variable, | 142 | introduce_variable::introduce_variable, |
142 | invert_if::invert_if, | 143 | invert_if::invert_if, |
144 | merge_imports::merge_imports, | ||
143 | merge_match_arms::merge_match_arms, | 145 | merge_match_arms::merge_match_arms, |
144 | move_bounds::move_bounds_to_where_clause, | 146 | move_bounds::move_bounds_to_where_clause, |
145 | move_guard::move_arm_cond_to_match_guard, | 147 | move_guard::move_arm_cond_to_match_guard, |