Use format_to! to make logic a bit nicer

This commit is contained in:
Matt Hall 2021-02-20 17:11:04 +00:00
parent b963893349
commit 8497b5bd75

View file

@ -1,5 +1,6 @@
use ast::{edit::IndentLevel, VisibilityOwner}; use ast::{edit::IndentLevel, VisibilityOwner};
use ide_db::base_db::AnchoredPathBuf; use ide_db::base_db::AnchoredPathBuf;
use stdx::format_to;
use syntax::{ use syntax::{
ast::{self, edit::AstNodeEdit, NameOwner}, ast::{self, edit::AstNodeEdit, NameOwner},
AstNode, TextRange, AstNode, TextRange,
@ -36,8 +37,6 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt
let module_def = ctx.sema.to_def(&module_ast)?; let module_def = ctx.sema.to_def(&module_ast)?;
let parent_module = module_def.parent(ctx.db())?; let parent_module = module_def.parent(ctx.db())?;
let vis_str =
if let Some(v) = module_ast.visibility() { v.to_string() + " " } else { "".to_string() };
acc.add( acc.add(
AssistId("move_module_to_file", AssistKind::RefactorExtract), AssistId("move_module_to_file", AssistKind::RefactorExtract),
@ -61,10 +60,13 @@ pub(crate) fn move_module_to_file(acc: &mut Assists, ctx: &AssistContext) -> Opt
items items
}; };
builder.replace( let mut buf = String::new();
module_ast.syntax().text_range(), if let Some(v) = module_ast.visibility() {
format!("{}mod {};", vis_str, module_name), format_to!(buf, "{} ", v);
); }
format_to!(buf, "mod {};", module_name);
builder.replace(module_ast.syntax().text_range(), buf);
let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path }; let dst = AnchoredPathBuf { anchor: ctx.frange.file_id, path };
builder.create_file(dst, contents); builder.create_file(dst, contents);