Migrate assists to format args captures, part 3

This commit is contained in:
DropDemBits 2022-10-10 14:20:27 -04:00
parent d23537394e
commit b1909a80af
18 changed files with 48 additions and 57 deletions

View file

@ -190,10 +190,10 @@ pub(crate) fn inline_call(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
PathResolution::Def(hir::ModuleDef::Function(f)) => f,
_ => return None,
};
(function, format!("Inline `{}`", path))
(function, format!("Inline `{path}`"))
}
ast::CallableExpr::MethodCall(call) => {
(ctx.sema.resolve_method_call(call)?, format!("Inline `{}`", name_ref))
(ctx.sema.resolve_method_call(call)?, format!("Inline `{name_ref}`"))
}
};

View file

@ -113,7 +113,7 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext<'_>)
.collect::<Option<Vec<_>>>()?;
let init_str = initializer_expr.syntax().text().to_string();
let init_in_paren = format!("({})", &init_str);
let init_in_paren = format!("({init_str})");
let target = match target {
ast::NameOrNameRef::Name(it) => it.syntax().text_range(),
@ -132,7 +132,7 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext<'_>)
let replacement = if should_wrap { &init_in_paren } else { &init_str };
if ast::RecordExprField::for_field_name(&name).is_some() {
cov_mark::hit!(inline_field_shorthand);
builder.insert(range.end(), format!(": {}", replacement));
builder.insert(range.end(), format!(": {replacement}"));
} else {
builder.replace(range, replacement.clone())
}

View file

@ -127,7 +127,7 @@ fn generate_unique_lifetime_param_name(
Some(type_params) => {
let used_lifetime_params: FxHashSet<_> =
type_params.lifetime_params().map(|p| p.syntax().text().to_string()).collect();
('a'..='z').map(|it| format!("'{}", it)).find(|it| !used_lifetime_params.contains(it))
('a'..='z').map(|it| format!("'{it}")).find(|it| !used_lifetime_params.contains(it))
}
None => Some("'a".to_string()),
}

View file

@ -78,7 +78,7 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
.join(" | ")
};
let arm = format!("{} => {},", pats, current_expr.syntax().text());
let arm = format!("{pats} => {current_expr},");
if let [first, .., last] = &*arms_to_merge {
let start = first.syntax().text_range().start();

View file

@ -40,11 +40,11 @@ pub(crate) fn move_from_mod_rs(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
let target = source_file.syntax().text_range();
let module_name = module.name(ctx.db())?.to_string();
let path = format!("../{}.rs", module_name);
let path = format!("../{module_name}.rs");
let dst = AnchoredPathBuf { anchor: ctx.file_id(), path };
acc.add(
AssistId("move_from_mod_rs", AssistKind::Refactor),
format!("Convert {}/mod.rs to {}.rs", module_name, module_name),
format!("Convert {module_name}/mod.rs to {module_name}.rs"),
target,
|builder| {
builder.move_file(ctx.file_id(), dst);

View file

@ -133,16 +133,16 @@ pub(crate) fn move_arm_cond_to_match_guard(
};
let then_arm_end = match_arm.syntax().text_range().end();
let indent_level = match_arm.indent_level();
let spaces = " ".repeat(indent_level.0 as _);
let spaces = indent_level;
let mut first = true;
for (cond, block) in conds_blocks {
if !first {
edit.insert(then_arm_end, format!("\n{}", spaces));
edit.insert(then_arm_end, format!("\n{spaces}"));
} else {
first = false;
}
let guard = format!("{} if {} => ", match_pat, cond.syntax().text());
let guard = format!("{match_pat} if {cond} => ");
edit.insert(then_arm_end, guard);
let only_expr = block.statements().next().is_none();
match &block.tail_expr() {
@ -158,7 +158,7 @@ pub(crate) fn move_arm_cond_to_match_guard(
}
if let Some(e) = tail {
cov_mark::hit!(move_guard_ifelse_else_tail);
let guard = format!("\n{}{} => ", spaces, match_pat);
let guard = format!("\n{spaces}{match_pat} => ");
edit.insert(then_arm_end, guard);
let only_expr = e.statements().next().is_none();
match &e.tail_expr() {
@ -183,7 +183,7 @@ pub(crate) fn move_arm_cond_to_match_guard(
{
cov_mark::hit!(move_guard_ifelse_has_wildcard);
}
_ => edit.insert(then_arm_end, format!("\n{}{} => {{}}", spaces, match_pat)),
_ => edit.insert(then_arm_end, format!("\n{spaces}{match_pat} => {{}}")),
}
}
},

View file

@ -52,7 +52,7 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext<'_>) ->
let mut buf = String::from("./");
match parent_module.name(ctx.db()) {
Some(name) if !parent_module.is_mod_rs(ctx.db()) => {
format_to!(buf, "{}/", name)
format_to!(buf, "{name}/")
}
_ => (),
}
@ -82,7 +82,7 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext<'_>) ->
items
};
let buf = format!("mod {};", module_name);
let buf = format!("mod {module_name};");
let replacement_start = match module_ast.mod_token() {
Some(mod_token) => mod_token.text_range(),

View file

@ -40,11 +40,11 @@ pub(crate) fn move_to_mod_rs(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
let target = source_file.syntax().text_range();
let module_name = module.name(ctx.db())?.to_string();
let path = format!("./{}/mod.rs", module_name);
let path = format!("./{module_name}/mod.rs");
let dst = AnchoredPathBuf { anchor: ctx.file_id(), path };
acc.add(
AssistId("move_to_mod_rs", AssistKind::Refactor),
format!("Convert {}.rs to {}/mod.rs", module_name, module_name),
format!("Convert {module_name}.rs to {module_name}/mod.rs"),
target,
|builder| {
builder.move_file(ctx.file_id(), dst);

View file

@ -38,7 +38,7 @@ pub(crate) fn reformat_number_literal(acc: &mut Assists, ctx: &AssistContext<'_>
converted.push_str(suffix);
let group_id = GroupLabel("Reformat number literal".into());
let label = format!("Convert {} to {}", literal, converted);
let label = format!("Convert {literal} to {converted}");
let range = literal.syntax().text_range();
acc.add_group(
&group_id,

View file

@ -54,7 +54,7 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext<'_>) ->
acc.add(
AssistId("qualify_method_call", AssistKind::RefactorInline),
format!("Qualify `{}` method call", ident.text()),
format!("Qualify `{ident}` method call"),
range,
|builder| {
qualify_candidate.qualify(

View file

@ -118,14 +118,14 @@ impl QualifyCandidate<'_> {
match self {
QualifyCandidate::QualifierStart(segment, generics) => {
let generics = generics.as_ref().map_or_else(String::new, ToString::to_string);
replacer(format!("{}{}::{}", import, generics, segment));
replacer(format!("{import}{generics}::{segment}"));
}
QualifyCandidate::UnqualifiedName(generics) => {
let generics = generics.as_ref().map_or_else(String::new, ToString::to_string);
replacer(format!("{}{}", import, generics));
replacer(format!("{import}{generics}"));
}
QualifyCandidate::TraitAssocItem(qualifier, segment) => {
replacer(format!("<{} as {}>::{}", qualifier, import, segment));
replacer(format!("<{qualifier} as {import}>::{segment}"));
}
QualifyCandidate::TraitMethod(db, mcall_expr) => {
Self::qualify_trait_method(db, mcall_expr, replacer, import, item);
@ -155,16 +155,11 @@ impl QualifyCandidate<'_> {
hir::Access::Exclusive => make::expr_ref(receiver, true),
hir::Access::Owned => receiver,
};
replacer(format!(
"{}::{}{}{}",
import,
method_name,
generics,
match arg_list {
Some(args) => make::arg_list(iter::once(receiver).chain(args)),
None => make::arg_list(iter::once(receiver)),
}
));
let arg_list = match arg_list {
Some(args) => make::arg_list(iter::once(receiver).chain(args)),
None => make::arg_list(iter::once(receiver)),
};
replacer(format!("{import}::{method_name}{generics}{arg_list}"));
}
Some(())
}
@ -218,15 +213,17 @@ fn group_label(candidate: &ImportCandidate) -> GroupLabel {
}
}
.text();
GroupLabel(format!("Qualify {}", name))
GroupLabel(format!("Qualify {name}"))
}
fn label(candidate: &ImportCandidate, import: &LocatedImport) -> String {
let import_path = &import.import_path;
match candidate {
ImportCandidate::Path(candidate) if candidate.qualifier.is_none() => {
format!("Qualify as `{}`", import.import_path)
format!("Qualify as `{import_path}`")
}
_ => format!("Qualify with `{}`", import.import_path),
_ => format!("Qualify with `{import_path}`"),
}
}

View file

@ -34,13 +34,10 @@ pub(crate) fn make_raw_string(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
let hashes = "#".repeat(required_hashes(&value).max(1));
if matches!(value, Cow::Borrowed(_)) {
// Avoid replacing the whole string to better position the cursor.
edit.insert(token.syntax().text_range().start(), format!("r{}", hashes));
edit.insert(token.syntax().text_range().start(), format!("r{hashes}"));
edit.insert(token.syntax().text_range().end(), hashes);
} else {
edit.replace(
token.syntax().text_range(),
format!("r{}\"{}\"{}", hashes, value, hashes),
);
edit.replace(token.syntax().text_range(), format!("r{hashes}\"{value}\"{hashes}"));
}
},
)
@ -83,7 +80,7 @@ pub(crate) fn make_usual_string(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
}
}
edit.replace(token.syntax().text_range(), format!("\"{}\"", escaped));
edit.replace(token.syntax().text_range(), format!("\"{escaped}\""));
},
)
}

View file

@ -102,7 +102,7 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
};
(
macro_call.syntax().text_range(),
if wrap { format!("({})", expr) } else { expr.to_string() },
if wrap { format!("({expr})") } else { expr.to_string() },
)
}
// dbg!(expr0, expr1, ...)
@ -127,8 +127,8 @@ mod tests {
fn check(ra_fixture_before: &str, ra_fixture_after: &str) {
check_assist(
remove_dbg,
&format!("fn main() {{\n{}\n}}", ra_fixture_before),
&format!("fn main() {{\n{}\n}}", ra_fixture_after),
&format!("fn main() {{\n{ra_fixture_before}\n}}"),
&format!("fn main() {{\n{ra_fixture_after}\n}}"),
);
}

View file

@ -124,7 +124,7 @@ fn add_assist(
) -> Option<()> {
let target = attr.syntax().text_range();
let annotated_name = adt.name()?;
let label = format!("Convert to manual `impl {} for {}`", replace_trait_path, annotated_name);
let label = format!("Convert to manual `impl {replace_trait_path} for {annotated_name}`");
acc.add(
AssistId("replace_derive_with_manual_impl", AssistKind::Refactor),
@ -158,11 +158,8 @@ fn add_assist(
}
}
builder.insert_snippet(
cap,
insert_pos,
format!("\n\n{}", render_snippet(cap, impl_def.syntax(), cursor)),
)
let rendered = render_snippet(cap, impl_def.syntax(), cursor);
builder.insert_snippet(cap, insert_pos, format!("\n\n{rendered}"))
}
};
},

View file

@ -62,7 +62,7 @@ pub(crate) fn replace_or_with_or_else(acc: &mut Assists, ctx: &AssistContext<'_>
acc.add(
AssistId("replace_or_with_or_else", AssistKind::RefactorRewrite),
format!("Replace {} with {}", name.text(), replace),
format!("Replace {name} with {replace}"),
call.syntax().text_range(),
|builder| {
builder.replace(name.syntax().text_range(), replace);
@ -138,7 +138,7 @@ pub(crate) fn replace_or_else_with_or(acc: &mut Assists, ctx: &AssistContext<'_>
acc.add(
AssistId("replace_or_else_with_or", AssistKind::RefactorRewrite),
format!("Replace {} with {}", name.text(), replace),
format!("Replace {name} with {replace}"),
call.syntax().text_range(),
|builder| {
builder.replace(name.syntax().text_range(), replace);

View file

@ -79,7 +79,7 @@ pub(crate) fn replace_turbofish_with_explicit_type(
"Replace turbofish with explicit type",
TextRange::new(initializer_start, turbofish_range.end()),
|builder| {
builder.insert(ident_range.end(), format!(": {}", returned_type));
builder.insert(ident_range.end(), format!(": {returned_type}"));
builder.delete(turbofish_range);
},
);

View file

@ -69,13 +69,13 @@ pub(crate) fn unwrap_tuple(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
for (pat, ty, expr) in
itertools::izip!(tuple_pat.fields(), tys.fields(), tuple_init.fields())
{
zipped_decls.push_str(&format!("{}let {pat}: {ty} = {expr};\n", indents))
zipped_decls.push_str(&format!("{indents}let {pat}: {ty} = {expr};\n"))
}
edit.replace(parent.text_range(), zipped_decls.trim());
} else {
let mut zipped_decls = String::new();
for (pat, expr) in itertools::izip!(tuple_pat.fields(), tuple_init.fields()) {
zipped_decls.push_str(&format!("{}let {pat} = {expr};\n", indents));
zipped_decls.push_str(&format!("{indents}let {pat} = {expr};\n"));
}
edit.replace(parent.text_range(), zipped_decls.trim());
}

View file

@ -76,11 +76,11 @@ pub(crate) fn wrap_return_type_in_result(acc: &mut Assists, ctx: &AssistContext<
match ctx.config.snippet_cap {
Some(cap) => {
let snippet = format!("Result<{}, ${{0:_}}>", type_ref);
let snippet = format!("Result<{type_ref}, ${{0:_}}>");
builder.replace_snippet(cap, type_ref.syntax().text_range(), snippet)
}
None => builder
.replace(type_ref.syntax().text_range(), format!("Result<{}, _>", type_ref)),
.replace(type_ref.syntax().text_range(), format!("Result<{type_ref}, _>")),
}
},
)