diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index f7f4434196..dcf8be5a7c 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -129,14 +129,17 @@ fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec Vec Vec Option { + let use_tree_list_node = single_use_tree.syntax().parent()?; + if single_use_tree + .path()? + .segment()? + .syntax() + .first_child()? + .kind() + == SyntaxKind::SELF_KW + { + let start = use_tree_list_node.prev_sibling()?.range().start(); + let end = use_tree_list_node.range().end(); + let range = TextRange::from_to(start, end); + let mut edit_builder = TextEditBuilder::new(); + edit_builder.delete(range); + return Some(edit_builder.finish()); + } + None +} + pub fn syntax_tree(file: &SourceFileNode) -> String { ::ra_syntax::utils::dump_tree(file.syntax()) } @@ -191,8 +216,9 @@ pub fn find_node_at_offset<'a, N: AstNode<'a>>( #[cfg(test)] mod tests { + use crate::test_utils::{add_cursor, assert_eq_dbg, assert_eq_text, extract_offset}; + use super::*; - use crate::test_utils::{add_cursor, assert_eq_dbg, extract_offset, assert_eq_text}; #[test] fn test_highlighting() { @@ -261,6 +287,7 @@ fn test_foo() {} use a; use {b}; use a::{c}; +use a::{self}; use a::{c, d::e}; use a::{c, d::{e}}; fn main() {} @@ -268,7 +295,10 @@ fn main() {} ); let diagnostics = check_unnecessary_braces_in_use_statement(&file); assert_eq_dbg( - "[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 }) }, 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 }) }, Diagnostic { range: [61; 64), msg: \"Unnecessary braces in use statement\", severity: WeakWarning, fix: Some(LocalEdit { label: \"Remove unnecessary braces\", edit: TextEdit { atoms: [AtomTextEdit { delete: [61; 61), insert: \"e\" }, AtomTextEdit { delete: [61; 64), insert: \"\" }] }, cursor_position: None }) }]", + 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 }) }, + 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 }) }, + 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 }) }, + 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 }) }]"#, &diagnostics, ) } diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs index dcc8793afc..f0d8dc7bbd 100644 --- a/crates/ra_editor/src/typing.rs +++ b/crates/ra_editor/src/typing.rs @@ -254,7 +254,7 @@ fn join_single_use_tree(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Opti Some(()) } -pub(crate) fn single_use_tree(tree_list: ast::UseTreeList) -> Option { +fn single_use_tree(tree_list: ast::UseTreeList) -> Option { let sub_use_trees = tree_list.use_trees().count(); if sub_use_trees != 1 { return None; diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index 2795afe0b6..6f96b81205 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs @@ -29,7 +29,7 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> { let prefix = "// "; let comment_blocks = s .lines() - .map(str::trim_left) + .map(str::trim_start) .enumerate() .group_by(|(_idx, line)| line.starts_with(prefix));