mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
fix use std::{self}
This commit is contained in:
parent
16996e9947
commit
cd60998b9b
3 changed files with 43 additions and 13 deletions
|
@ -129,14 +129,17 @@ fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec<Diagn
|
||||||
if use_tree_list.use_trees().count() == 1 {
|
if use_tree_list.use_trees().count() == 1 {
|
||||||
let range = use_tree_list.syntax().range();
|
let range = use_tree_list.syntax().range();
|
||||||
// use_tree_list always has one child, so we use unwrap directly here.
|
// use_tree_list always has one child, so we use unwrap directly here.
|
||||||
let to_replace = typing::single_use_tree(use_tree_list)
|
let single_use_tree: ast::UseTree = use_tree_list.use_trees().next().unwrap();
|
||||||
.unwrap()
|
let edit = text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
|
||||||
.syntax()
|
single_use_tree,
|
||||||
.text()
|
)
|
||||||
.to_string();
|
.unwrap_or_else(|| {
|
||||||
|
let to_replace = single_use_tree.syntax().text().to_string();
|
||||||
let mut edit_builder = TextEditBuilder::new();
|
let mut edit_builder = TextEditBuilder::new();
|
||||||
edit_builder.delete(range);
|
edit_builder.delete(range);
|
||||||
edit_builder.insert(range.start(), to_replace);
|
edit_builder.insert(range.start(), to_replace);
|
||||||
|
edit_builder.finish()
|
||||||
|
});
|
||||||
|
|
||||||
diagnostics.push(Diagnostic {
|
diagnostics.push(Diagnostic {
|
||||||
range: range,
|
range: range,
|
||||||
|
@ -144,7 +147,7 @@ fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec<Diagn
|
||||||
severity: Severity::WeakWarning,
|
severity: Severity::WeakWarning,
|
||||||
fix: Some(LocalEdit {
|
fix: Some(LocalEdit {
|
||||||
label: "Remove unnecessary braces".to_string(),
|
label: "Remove unnecessary braces".to_string(),
|
||||||
edit: edit_builder.finish(),
|
edit: edit,
|
||||||
cursor_position: None,
|
cursor_position: None,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
@ -155,6 +158,28 @@ fn check_unnecessary_braces_in_use_statement(file: &SourceFileNode) -> Vec<Diagn
|
||||||
diagnostics
|
diagnostics
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(
|
||||||
|
single_use_tree: ast::UseTree,
|
||||||
|
) -> Option<TextEdit> {
|
||||||
|
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 {
|
pub fn syntax_tree(file: &SourceFileNode) -> String {
|
||||||
::ra_syntax::utils::dump_tree(file.syntax())
|
::ra_syntax::utils::dump_tree(file.syntax())
|
||||||
}
|
}
|
||||||
|
@ -191,8 +216,9 @@ pub fn find_node_at_offset<'a, N: AstNode<'a>>(
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use crate::test_utils::{add_cursor, assert_eq_dbg, assert_eq_text, extract_offset};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::test_utils::{add_cursor, assert_eq_dbg, extract_offset, assert_eq_text};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_highlighting() {
|
fn test_highlighting() {
|
||||||
|
@ -261,6 +287,7 @@ fn test_foo() {}
|
||||||
use a;
|
use a;
|
||||||
use {b};
|
use {b};
|
||||||
use a::{c};
|
use a::{c};
|
||||||
|
use a::{self};
|
||||||
use a::{c, d::e};
|
use a::{c, d::e};
|
||||||
use a::{c, d::{e}};
|
use a::{c, d::{e}};
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
@ -268,7 +295,10 @@ fn main() {}
|
||||||
);
|
);
|
||||||
let diagnostics = check_unnecessary_braces_in_use_statement(&file);
|
let diagnostics = check_unnecessary_braces_in_use_statement(&file);
|
||||||
assert_eq_dbg(
|
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,
|
&diagnostics,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -254,7 +254,7 @@ fn join_single_use_tree(edit: &mut TextEditBuilder, node: SyntaxNodeRef) -> Opti
|
||||||
Some(())
|
Some(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn single_use_tree(tree_list: ast::UseTreeList) -> Option<ast::UseTree> {
|
fn single_use_tree(tree_list: ast::UseTreeList) -> Option<ast::UseTree> {
|
||||||
let sub_use_trees = tree_list.use_trees().count();
|
let sub_use_trees = tree_list.use_trees().count();
|
||||||
if sub_use_trees != 1 {
|
if sub_use_trees != 1 {
|
||||||
return None;
|
return None;
|
||||||
|
|
|
@ -29,7 +29,7 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> {
|
||||||
let prefix = "// ";
|
let prefix = "// ";
|
||||||
let comment_blocks = s
|
let comment_blocks = s
|
||||||
.lines()
|
.lines()
|
||||||
.map(str::trim_left)
|
.map(str::trim_start)
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.group_by(|(_idx, line)| line.starts_with(prefix));
|
.group_by(|(_idx, line)| line.starts_with(prefix));
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue