aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_editor/src/lib.rs')
-rw-r--r--crates/ra_editor/src/lib.rs48
1 files changed, 27 insertions, 21 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};
27use itertools::Itertools;
27 28
28#[derive(Debug)] 29#[derive(Debug)]
29pub struct HighlightedRange { 30pub 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);
287use a; 286 let diagnostics = check_unnecessary_braces_in_use_statement(&file);
288use {b}; 287 assert!(diagnostics.is_empty());
289use a::{c}; 288 }
290use a::{self}; 289
291use a::{c, d::e}; 290 fn check_apply(before: &str, after: &str) {
292use a::{c, d::{e}}; 291 let file = SourceFileNode::parse(before);
293fn 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}