aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_editor/src/lib.rs52
-rw-r--r--crates/ra_editor/src/typing.rs2
-rw-r--r--crates/tools/src/lib.rs2
3 files changed, 43 insertions, 13 deletions
diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs
index f7f443419..dcf8be5a7 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<Diagn
129 if use_tree_list.use_trees().count() == 1 { 129 if use_tree_list.use_trees().count() == 1 {
130 let range = use_tree_list.syntax().range(); 130 let range = use_tree_list.syntax().range();
131 // use_tree_list always has one child, so we use unwrap directly here. 131 // use_tree_list always has one child, so we use unwrap directly here.
132 let to_replace = typing::single_use_tree(use_tree_list) 132 let single_use_tree: ast::UseTree = use_tree_list.use_trees().next().unwrap();
133 .unwrap() 133 let edit = text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
134 .syntax() 134 single_use_tree,
135 .text() 135 )
136 .to_string(); 136 .unwrap_or_else(|| {
137 let mut edit_builder = TextEditBuilder::new(); 137 let to_replace = single_use_tree.syntax().text().to_string();
138 edit_builder.delete(range); 138 let mut edit_builder = TextEditBuilder::new();
139 edit_builder.insert(range.start(), to_replace); 139 edit_builder.delete(range);
140 edit_builder.insert(range.start(), to_replace);
141 edit_builder.finish()
142 });
140 143
141 diagnostics.push(Diagnostic { 144 diagnostics.push(Diagnostic {
142 range: range, 145 range: range,
@@ -144,7 +147,7 @@ fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec<Diagn
144 severity: Severity::WeakWarning, 147 severity: Severity::WeakWarning,
145 fix: Some(LocalEdit { 148 fix: Some(LocalEdit {
146 label: "Remove unnecessary braces".to_string(), 149 label: "Remove unnecessary braces".to_string(),
147 edit: edit_builder.finish(), 150 edit: edit,
148 cursor_position: None, 151 cursor_position: None,
149 }), 152 }),
150 }) 153 })
@@ -155,6 +158,28 @@ fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec<Diagn
155 diagnostics 158 diagnostics
156} 159}
157 160
161fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
162 single_use_tree: ast::UseTree,
163) -> Option<TextEdit> {
164 let use_tree_list_node = single_use_tree.syntax().parent()?;
165 if single_use_tree
166 .path()?
167 .segment()?
168 .syntax()
169 .first_child()?
170 .kind()
171 == SyntaxKind::SELF_KW
172 {
173 let start = use_tree_list_node.prev_sibling()?.range().start();
174 let end = use_tree_list_node.range().end();
175 let range = TextRange::from_to(start, end);
176 let mut edit_builder = TextEditBuilder::new();
177 edit_builder.delete(range);
178 return Some(edit_builder.finish());
179 }
180 None
181}
182
158pub fn syntax_tree(file: &SourceFileNode) -> String { 183pub fn syntax_tree(file: &SourceFileNode) -> String {
159 ::ra_syntax::utils::dump_tree(file.syntax()) 184 ::ra_syntax::utils::dump_tree(file.syntax())
160} 185}
@@ -191,8 +216,9 @@ pub fn find_node_at_offset<'a, N: AstNode<'a>>(
191 216
192#[cfg(test)] 217#[cfg(test)]
193mod tests { 218mod tests {
219 use crate::test_utils::{add_cursor, assert_eq_dbg, assert_eq_text, extract_offset};
220
194 use super::*; 221 use super::*;
195 use crate::test_utils::{add_cursor, assert_eq_dbg, extract_offset, assert_eq_text};
196 222
197 #[test] 223 #[test]
198 fn test_highlighting() { 224 fn test_highlighting() {
@@ -261,6 +287,7 @@ fn test_foo() {}
261use a; 287use a;
262use {b}; 288use {b};
263use a::{c}; 289use a::{c};
290use a::{self};
264use a::{c, d::e}; 291use a::{c, d::e};
265use a::{c, d::{e}}; 292use a::{c, d::{e}};
266fn main() {} 293fn main() {}
@@ -268,7 +295,10 @@ fn main() {}
268 ); 295 );
269 let diagnostics = check_unnecessary_braces_in_use_statement(&file); 296 let diagnostics = check_unnecessary_braces_in_use_statement(&file);
270 assert_eq_dbg( 297 assert_eq_dbg(
271 "[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 }) }]", 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 }) },
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 }) },
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 }) }]"#,
272 &diagnostics, 302 &diagnostics,
273 ) 303 )
274 } 304 }
diff --git a/crates/ra_editor/src/typing.rs b/crates/ra_editor/src/typing.rs
index dcc8793af..f0d8dc7bb 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
254 Some(()) 254 Some(())
255} 255}
256 256
257pub(crate) fn single_use_tree(tree_list: ast::UseTreeList) -> Option<ast::UseTree> { 257fn single_use_tree(tree_list: ast::UseTreeList) -> Option<ast::UseTree> {
258 let sub_use_trees = tree_list.use_trees().count(); 258 let sub_use_trees = tree_list.use_trees().count();
259 if sub_use_trees != 1 { 259 if sub_use_trees != 1 {
260 return None; 260 return None;
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs
index 2795afe0b..6f96b8120 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)> {
29 let prefix = "// "; 29 let prefix = "// ";
30 let comment_blocks = s 30 let comment_blocks = s
31 .lines() 31 .lines()
32 .map(str::trim_left) 32 .map(str::trim_start)
33 .enumerate() 33 .enumerate()
34 .group_by(|(_idx, line)| line.starts_with(prefix)); 34 .group_by(|(_idx, line)| line.starts_with(prefix));
35 35