Fix move_format_string_arg being tokentree unaware

This commit is contained in:
Lukas Wirth 2022-10-01 00:42:16 +02:00
parent 39eaf7864c
commit 3ad0334718

View File

@ -7,6 +7,7 @@
}, },
}; };
use itertools::Itertools; use itertools::Itertools;
use stdx::format_to;
use syntax::{ast, AstNode, AstToken, NodeOrToken, SyntaxKind::COMMA, TextRange}; use syntax::{ast, AstNode, AstToken, NodeOrToken, SyntaxKind::COMMA, TextRange};
// Assist: move_format_string_arg // Assist: move_format_string_arg
@ -78,20 +79,26 @@ pub(crate) fn move_format_string_arg(acc: &mut Assists, ctx: &AssistContext<'_>)
// Extract existing arguments in macro // Extract existing arguments in macro
let tokens = let tokens =
tt.token_trees_and_tokens().filter_map(NodeOrToken::into_token).collect_vec(); tt.token_trees_and_tokens().collect_vec();
let mut existing_args: Vec<String> = vec![]; let mut existing_args: Vec<String> = vec![];
let mut current_arg = String::new(); let mut current_arg = String::new();
if let [_opening_bracket, format_string, _args_start_comma, tokens @ .., end_bracket] = if let [_opening_bracket, NodeOrToken::Token(format_string), _args_start_comma, tokens @ .., NodeOrToken::Token(end_bracket)] =
tokens.as_slice() tokens.as_slice()
{ {
for t in tokens { for t in tokens {
if t.kind() == COMMA { match t {
existing_args.push(current_arg.trim().into()); NodeOrToken::Node(n) => {
current_arg.clear(); format_to!(current_arg, "{n}");
} else { },
current_arg.push_str(t.text()); NodeOrToken::Token(t) if t.kind() == COMMA=> {
existing_args.push(current_arg.trim().into());
current_arg.clear();
},
NodeOrToken::Token(t) => {
current_arg.push_str(t.text());
},
} }
} }
existing_args.push(current_arg.trim().into()); existing_args.push(current_arg.trim().into());
@ -261,6 +268,27 @@ fn main() {
fn main() { fn main() {
print!("{} {:b} {}"$0, 1, x + 1, Struct(1, 2)); print!("{} {:b} {}"$0, 1, x + 1, Struct(1, 2));
} }
"#,
),
);
}
#[test]
fn nested_tt() {
check_assist(
move_format_string_arg,
&add_macro_decl(
r#"
fn main() {
print!("My name is {} {x$0 + x}", stringify!(Paperino))
}
"#,
),
&add_macro_decl(
r#"
fn main() {
print!("My name is {} {}"$0, stringify!(Paperino), x + x)
}
"#, "#,
), ),
); );