mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 06:41:48 +00:00
Remove dbg expression and newline as whole
This commit is contained in:
parent
2292ff64f1
commit
c7cd0aff4a
1 changed files with 28 additions and 6 deletions
|
@ -1,5 +1,5 @@
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode, AstToken},
|
||||||
match_ast, SyntaxElement, TextRange, TextSize, T,
|
match_ast, SyntaxElement, TextRange, TextSize, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -24,11 +24,25 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||||
let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?;
|
let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?;
|
||||||
let new_contents = adjusted_macro_contents(¯o_call)?;
|
let new_contents = adjusted_macro_contents(¯o_call)?;
|
||||||
|
|
||||||
let macro_text_range = macro_call.syntax().text_range();
|
let macro_text_range = if new_contents.is_empty() {
|
||||||
let macro_end = if macro_call.semicolon_token().is_some() {
|
let parent = macro_call.syntax().parent()?;
|
||||||
macro_text_range.end() - TextSize::of(';')
|
|
||||||
|
let start = parent
|
||||||
|
.prev_sibling_or_token()
|
||||||
|
.and_then(|el| {
|
||||||
|
Some(el.into_token().and_then(ast::Whitespace::cast)?.syntax().text_range().start())
|
||||||
|
})
|
||||||
|
.unwrap_or(parent.text_range().start());
|
||||||
|
let end = parent.text_range().end();
|
||||||
|
|
||||||
|
TextRange::new(start, end)
|
||||||
} else {
|
} else {
|
||||||
macro_text_range.end()
|
macro_call.syntax().text_range()
|
||||||
|
};
|
||||||
|
|
||||||
|
let macro_end = match macro_call.semicolon_token() {
|
||||||
|
Some(_) => macro_text_range.end() - TextSize::of(';'),
|
||||||
|
None => macro_text_range.end(),
|
||||||
};
|
};
|
||||||
|
|
||||||
acc.add(
|
acc.add(
|
||||||
|
@ -417,6 +431,14 @@ fn main() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_remove_empty_dbg() {
|
fn test_remove_empty_dbg() {
|
||||||
check_assist(remove_dbg, r#"$0dbg!()"#, r#""#);
|
check_assist(remove_dbg, r#"fn foo() { $0dbg!(); }"#, r#"fn foo() { }"#);
|
||||||
|
check_assist(
|
||||||
|
remove_dbg,
|
||||||
|
r#"fn foo() {
|
||||||
|
$0dbg!();
|
||||||
|
}"#,
|
||||||
|
r#"fn foo() {
|
||||||
|
}"#,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue