rust/crates/ra_assists/src/remove_dbg.rs

140 lines
3.8 KiB
Rust
Raw Normal View History

use hir::db::HirDatabase;
use ra_syntax::{
ast::{self, AstNode},
TextUnit,
2019-05-15 07:35:47 -05:00
T
};
2019-02-24 04:53:35 -06:00
use crate::{AssistCtx, Assist, AssistId};
pub(crate) fn remove_dbg(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
let macro_call = ctx.node_at_offset::<ast::MacroCall>()?;
if !is_valid_macrocall(macro_call, "dbg")? {
return None;
}
let macro_range = macro_call.syntax().range();
2019-02-11 10:18:27 -06:00
// If the cursor is inside the macro call, we'll try to maintain the cursor
// position by subtracting the length of dbg!( from the start of the file
// range, otherwise we'll default to using the start of the macro call
let cursor_pos = {
let file_range = ctx.frange.range;
let offset_start = file_range
.start()
.checked_sub(macro_range.start())
.unwrap_or_else(|| TextUnit::from(0));
2019-02-07 12:42:00 -06:00
let dbg_size = TextUnit::of_str("dbg!(");
if offset_start > dbg_size {
file_range.start() - dbg_size
} else {
macro_range.start()
}
};
let macro_content = {
let macro_args = macro_call.token_tree()?.syntax();
2019-02-07 12:42:00 -06:00
let range = macro_args.range();
let start = range.start() + TextUnit::of_char('(');
let end = range.end() - TextUnit::of_char(')');
macro_args.text().slice(start..end).to_string()
};
2019-02-24 04:53:35 -06:00
ctx.add_action(AssistId("remove_dbg"), "remove dbg!()", |edit| {
2019-02-08 17:34:05 -06:00
edit.target(macro_call.syntax().range());
edit.replace(macro_range, macro_content);
edit.set_cursor(cursor_pos);
});
ctx.build()
}
/// Verifies that the given macro_call actually matches the given name
/// and contains proper ending tokens
fn is_valid_macrocall(macro_call: &ast::MacroCall, macro_name: &str) -> Option<bool> {
let path = macro_call.path()?;
let name_ref = path.segment()?.name_ref()?;
2019-02-11 10:18:27 -06:00
// Make sure it is actually a dbg-macro call, dbg followed by !
2019-03-30 05:25:53 -05:00
let excl = path.syntax().next_sibling_or_token()?;
2019-05-15 07:35:47 -05:00
if name_ref.text() != macro_name || excl.kind() != T![!] {
return None;
}
let node = macro_call.token_tree()?.syntax();
2019-03-30 05:25:53 -05:00
let first_child = node.first_child_or_token()?;
let last_child = node.last_child_or_token()?;
match (first_child.kind(), last_child.kind()) {
2019-05-15 07:35:47 -05:00
(T!['('], T![')']) | (T!['['], T![']']) | (T!['{'], T!['}']) => Some(true),
_ => Some(false),
}
}
#[cfg(test)]
mod tests {
use super::*;
2019-02-08 17:34:05 -06:00
use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target};
#[test]
fn test_remove_dbg() {
check_assist(remove_dbg, "<|>dbg!(1 + 1)", "<|>1 + 1");
check_assist(remove_dbg, "dbg!<|>((1 + 1))", "<|>(1 + 1)");
check_assist(remove_dbg, "dbg!(1 <|>+ 1)", "1 <|>+ 1");
check_assist(remove_dbg, "let _ = <|>dbg!(1 + 1)", "let _ = <|>1 + 1");
check_assist(
remove_dbg,
"
fn foo(n: usize) {
if let Some(_) = dbg!(n.<|>checked_sub(4)) {
// ...
}
}
",
"
fn foo(n: usize) {
if let Some(_) = n.<|>checked_sub(4) {
// ...
}
}
",
);
}
#[test]
fn test_remove_dbg_with_brackets_and_braces() {
check_assist(remove_dbg, "dbg![<|>1 + 1]", "<|>1 + 1");
check_assist(remove_dbg, "dbg!{<|>1 + 1}", "<|>1 + 1");
}
#[test]
fn test_remove_dbg_not_applicable() {
check_assist_not_applicable(remove_dbg, "<|>vec![1, 2, 3]");
check_assist_not_applicable(remove_dbg, "<|>dbg(5, 6, 7)");
check_assist_not_applicable(remove_dbg, "<|>dbg!(5, 6, 7");
}
2019-02-08 17:34:05 -06:00
#[test]
fn remove_dbg_target() {
check_assist_target(
remove_dbg,
"
fn foo(n: usize) {
if let Some(_) = dbg!(n.<|>checked_sub(4)) {
// ...
}
}
",
"dbg!(n.checked_sub(4))",
);
}
}