fix: rename to extract_expressions_from_format_string

This commit is contained in:
unvalley 2022-11-26 23:00:03 +09:00 committed by Lukas Wirth
parent 25717af4aa
commit 9290399ec4
3 changed files with 45 additions and 42 deletions

View File

@ -10,7 +10,7 @@
use stdx::format_to;
use syntax::{ast, AstNode, AstToken, NodeOrToken, SyntaxKind::COMMA, TextRange};
// Assist: move_format_string_arg
// Assist: extract_expressions_from_format_string
//
// Move an expression out of a format string.
//
@ -40,7 +40,10 @@
// }
// ```
pub(crate) fn move_format_string_arg(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
pub(crate) fn extract_expressions_from_format_string(
acc: &mut Assists,
ctx: &AssistContext<'_>,
) -> Option<()> {
let fmt_string = ctx.find_token_at_offset::<ast::String>()?;
let tt = fmt_string.syntax().parent().and_then(ast::TokenTree::cast)?;
@ -58,7 +61,7 @@ pub(crate) fn move_format_string_arg(acc: &mut Assists, ctx: &AssistContext<'_>)
acc.add(
AssistId(
"move_format_string_arg",
"extract_expressions_from_format_string",
// if there aren't any expressions, then make the assist a RefactorExtract
if extracted_args.iter().filter(|f| matches!(f, Arg::Expr(_))).count() == 0 {
AssistKind::RefactorExtract
@ -171,7 +174,7 @@ fn add_macro_decl(s: &'static str) -> String {
#[test]
fn multiple_middle_arg() {
check_assist(
move_format_string_arg,
extract_expressions_from_format_string,
&add_macro_decl(
r#"
fn main() {
@ -192,7 +195,7 @@ fn main() {
#[test]
fn single_arg() {
check_assist(
move_format_string_arg,
extract_expressions_from_format_string,
&add_macro_decl(
r#"
fn main() {
@ -213,7 +216,7 @@ fn main() {
#[test]
fn multiple_middle_placeholders_arg() {
check_assist(
move_format_string_arg,
extract_expressions_from_format_string,
&add_macro_decl(
r#"
fn main() {
@ -234,7 +237,7 @@ fn main() {
#[test]
fn multiple_trailing_args() {
check_assist(
move_format_string_arg,
extract_expressions_from_format_string,
&add_macro_decl(
r#"
fn main() {
@ -255,7 +258,7 @@ fn main() {
#[test]
fn improper_commas() {
check_assist(
move_format_string_arg,
extract_expressions_from_format_string,
&add_macro_decl(
r#"
fn main() {
@ -276,7 +279,7 @@ fn main() {
#[test]
fn nested_tt() {
check_assist(
move_format_string_arg,
extract_expressions_from_format_string,
&add_macro_decl(
r#"
fn main() {

View File

@ -138,7 +138,7 @@ mod handlers {
mod flip_binexpr;
mod flip_comma;
mod flip_trait_bound;
mod move_format_string_arg;
mod extract_expressions_from_format_string;
mod generate_constant;
mod generate_default_from_enum_variant;
mod generate_default_from_new;
@ -230,6 +230,7 @@ pub(crate) fn all() -> &'static [Handler] {
convert_while_to_loop::convert_while_to_loop,
destructure_tuple_binding::destructure_tuple_binding,
expand_glob_import::expand_glob_import,
extract_expressions_from_format_string::extract_expressions_from_format_string,
extract_struct_from_enum_variant::extract_struct_from_enum_variant,
extract_type_alias::extract_type_alias,
fix_visibility::fix_visibility,
@ -264,7 +265,6 @@ pub(crate) fn all() -> &'static [Handler] {
merge_match_arms::merge_match_arms,
move_bounds::move_bounds_to_where_clause,
move_const_to_impl::move_const_to_impl,
move_format_string_arg::move_format_string_arg,
move_guard::move_arm_cond_to_match_guard,
move_guard::move_guard_to_arm_body,
move_module_to_file::move_module_to_file,

View File

@ -624,6 +624,37 @@ fn qux(bar: Bar, baz: Baz) {}
)
}
#[test]
fn doctest_extract_expressions_from_format_string() {
check_doc_test(
"extract_expressions_from_format_string",
r#####"
macro_rules! format_args {
($lit:literal $(tt:tt)*) => { 0 },
}
macro_rules! print {
($($arg:tt)*) => (std::io::_print(format_args!($($arg)*)));
}
fn main() {
print!("{x + 1}$0");
}
"#####,
r#####"
macro_rules! format_args {
($lit:literal $(tt:tt)*) => { 0 },
}
macro_rules! print {
($($arg:tt)*) => (std::io::_print(format_args!($($arg)*)));
}
fn main() {
print!("{}"$0, x + 1);
}
"#####,
)
}
#[test]
fn doctest_extract_function() {
check_doc_test(
@ -1703,37 +1734,6 @@ fn foo() -> usize {
)
}
#[test]
fn doctest_move_format_string_arg() {
check_doc_test(
"move_format_string_arg",
r#####"
macro_rules! format_args {
($lit:literal $(tt:tt)*) => { 0 },
}
macro_rules! print {
($($arg:tt)*) => (std::io::_print(format_args!($($arg)*)));
}
fn main() {
print!("{x + 1}$0");
}
"#####,
r#####"
macro_rules! format_args {
($lit:literal $(tt:tt)*) => { 0 },
}
macro_rules! print {
($($arg:tt)*) => (std::io::_print(format_args!($($arg)*)));
}
fn main() {
print!("{}"$0, x + 1);
}
"#####,
)
}
#[test]
fn doctest_move_from_mod_rs() {
check_doc_test(