diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-27 10:39:59 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-27 10:39:59 +0000 |
commit | dbf03b674e7e1a49d9b32ec5ed656df2aedd3ed3 (patch) | |
tree | 07aa7981298a4904e2d2349981ce063d57637844 /crates/ra_editor | |
parent | 1d6dcef5c584d0dffdf5386eec993e41daad0210 (diff) | |
parent | 8c2c1bf9eb1496adeaa647066c96d0e7d71a1a8a (diff) |
Merge #337
337: check edits in diagnostics r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_editor')
-rw-r--r-- | crates/ra_editor/src/lib.rs | 48 | ||||
-rw-r--r-- | crates/ra_editor/src/typing.rs | 16 |
2 files changed, 30 insertions, 34 deletions
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index dcf8be5a7..a8c68e79e 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs | |||
@@ -24,6 +24,7 @@ use ra_syntax::{ | |||
24 | SyntaxKind::{self, *}, | 24 | SyntaxKind::{self, *}, |
25 | SyntaxNodeRef, TextRange, TextUnit, | 25 | SyntaxNodeRef, TextRange, TextUnit, |
26 | }; | 26 | }; |
27 | use itertools::Itertools; | ||
27 | 28 | ||
28 | #[derive(Debug)] | 29 | #[derive(Debug)] |
29 | pub struct HighlightedRange { | 30 | pub struct HighlightedRange { |
@@ -126,10 +127,8 @@ fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec<Diagn | |||
126 | let mut diagnostics = Vec::new(); | 127 | let mut diagnostics = Vec::new(); |
127 | for node in file.syntax().descendants() { | 128 | for node in file.syntax().descendants() { |
128 | if let Some(use_tree_list) = ast::UseTreeList::cast(node) { | 129 | if let Some(use_tree_list) = ast::UseTreeList::cast(node) { |
129 | if use_tree_list.use_trees().count() == 1 { | 130 | if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() { |
130 | let range = use_tree_list.syntax().range(); | 131 | let range = use_tree_list.syntax().range(); |
131 | // use_tree_list always has one child, so we use unwrap directly here. | ||
132 | let single_use_tree: ast::UseTree = use_tree_list.use_trees().next().unwrap(); | ||
133 | let edit = text_edit_for_remove_unnecessary_braces_with_self_in_use_statement( | 132 | let edit = text_edit_for_remove_unnecessary_braces_with_self_in_use_statement( |
134 | single_use_tree, | 133 | single_use_tree, |
135 | ) | 134 | ) |
@@ -282,24 +281,31 @@ fn test_foo() {} | |||
282 | 281 | ||
283 | #[test] | 282 | #[test] |
284 | fn test_check_unnecessary_braces_in_use_statement() { | 283 | fn test_check_unnecessary_braces_in_use_statement() { |
285 | let file = SourceFileNode::parse( | 284 | fn check_not_applicable(code: &str) { |
286 | r#" | 285 | let file = SourceFileNode::parse(code); |
287 | use a; | 286 | let diagnostics = check_unnecessary_braces_in_use_statement(&file); |
288 | use {b}; | 287 | assert!(diagnostics.is_empty()); |
289 | use a::{c}; | 288 | } |
290 | use a::{self}; | 289 | |
291 | use a::{c, d::e}; | 290 | fn check_apply(before: &str, after: &str) { |
292 | use a::{c, d::{e}}; | 291 | let file = SourceFileNode::parse(before); |
293 | fn main() {} | 292 | let diagnostic = check_unnecessary_braces_in_use_statement(&file) |
294 | "#, | 293 | .pop() |
294 | .unwrap_or_else(|| panic!("no diagnostics for:\n{}\n", before)); | ||
295 | let fix = diagnostic.fix.unwrap(); | ||
296 | let actual = fix.edit.apply(&before); | ||
297 | assert_eq_text!(after, &actual); | ||
298 | } | ||
299 | |||
300 | check_not_applicable( | ||
301 | " | ||
302 | use a; | ||
303 | use a::{c, d::e}; | ||
304 | ", | ||
295 | ); | 305 | ); |
296 | let diagnostics = check_unnecessary_braces_in_use_statement(&file); | 306 | check_apply("use {b};", "use b;"); |
297 | assert_eq_dbg( | 307 | check_apply("use a::{c};", "use a::c;"); |
298 | r#"[Diagnostic { range: [12; 15), msg: "Unnecessary braces in use statement", severity: WeakWarning, fix: Some(LocalEdit { label: "Remove unnecessary braces", edit: TextEdit { atoms: [AtomTextEdit { delete: [12; 12), insert: "b" }, AtomTextEdit { delete: [12; 15), insert: "" }] }, cursor_position: None }) }, | 308 | check_apply("use a::{self};", "use a;"); |
299 | Diagnostic { range: [24; 27), msg: "Unnecessary braces in use statement", severity: WeakWarning, fix: Some(LocalEdit { label: "Remove unnecessary braces", edit: TextEdit { atoms: [AtomTextEdit { delete: [24; 24), insert: "c" }, AtomTextEdit { delete: [24; 27), insert: "" }] }, cursor_position: None }) }, | 309 | check_apply("use a::{c, d::{e}};", "use a::{c, d::e};"); |
300 | Diagnostic { range: [36; 42), msg: "Unnecessary braces in use statement", severity: WeakWarning, fix: Some(LocalEdit { label: "Remove unnecessary braces", edit: TextEdit { atoms: [AtomTextEdit { delete: [34; 42), insert: "" }] }, cursor_position: None }) }, | ||
301 | Diagnostic { range: [76; 79), msg: "Unnecessary braces in use statement", severity: WeakWarning, fix: Some(LocalEdit { label: "Remove unnecessary braces", edit: TextEdit { atoms: [AtomTextEdit { delete: [76; 76), insert: "e" }, AtomTextEdit { delete: [76; 79), insert: "" }] }, cursor_position: None }) }]"#, | ||
302 | &diagnostics, | ||
303 | ) | ||
304 | } | 310 | } |
305 | } | 311 | } |
diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index f0d8dc7bb..21d068a7b 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs | |||
@@ -8,9 +8,8 @@ use ra_syntax::{ | |||
8 | SyntaxKind::*, | 8 | SyntaxKind::*, |
9 | SyntaxNodeRef, TextRange, TextUnit, | 9 | SyntaxNodeRef, TextRange, TextUnit, |
10 | }; | 10 | }; |
11 | use ra_text_edit::text_utils::{ | 11 | use ra_text_edit::text_utils::contains_offset_nonstrict; |
12 | contains_offset_nonstrict | 12 | use itertools::Itertools; |
13 | }; | ||
14 | 13 | ||
15 | use crate::{find_node_at_offset, TextEditBuilder, LocalEdit}; | 14 | use crate::{find_node_at_offset, TextEditBuilder, LocalEdit}; |
16 | 15 | ||
@@ -246,7 +245,7 @@ fn single_expr(block: ast::Block) -> Option<ast::Expr> { | |||
246 | 245 | ||
247 | fn join_single_use_tree(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Option<()> { | 246 | fn join_single_use_tree(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Option<()> { |
248 | let use_tree_list = ast::UseTreeList::cast(node.parent()?)?; | 247 | let use_tree_list = ast::UseTreeList::cast(node.parent()?)?; |
249 | let tree = single_use_tree(use_tree_list)?; | 248 | let (tree,) = use_tree_list.use_trees().collect_tuple()?; |
250 | edit.replace( | 249 | edit.replace( |
251 | use_tree_list.syntax().range(), | 250 | use_tree_list.syntax().range(), |
252 | tree.syntax().text().to_string(), | 251 | tree.syntax().text().to_string(), |
@@ -254,15 +253,6 @@ fn join_single_use_tree(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Opti | |||
254 | Some(()) | 253 | Some(()) |
255 | } | 254 | } |
256 | 255 | ||
257 | fn single_use_tree(tree_list: ast::UseTreeList) -> Option<ast::UseTree> { | ||
258 | let sub_use_trees = tree_list.use_trees().count(); | ||
259 | if sub_use_trees != 1 { | ||
260 | return None; | ||
261 | } | ||
262 | |||
263 | tree_list.use_trees().next() | ||
264 | } | ||
265 | |||
266 | fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str { | 256 | fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str { |
267 | match left.kind() { | 257 | match left.kind() { |
268 | L_PAREN | L_BRACK => return "", | 258 | L_PAREN | L_BRACK => return "", |