mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-25 13:24:22 +00:00
fix: arbitary noop of assist and same file double writes
This commit is contained in:
parent
32b95ea310
commit
227490c069
@ -73,63 +73,69 @@ pub(crate) fn extract_module(acc: &mut Assists, ctx: &AssistContext) -> Option<(
|
|||||||
|
|
||||||
let old_item_indent = module.body_items[0].indent_level();
|
let old_item_indent = module.body_items[0].indent_level();
|
||||||
|
|
||||||
|
//This takes place in three steps:
|
||||||
|
//
|
||||||
|
//- Firstly, we will update the references(usages) e.g. converting a
|
||||||
|
// function call bar() to modname::bar(), and similarly for other items
|
||||||
|
//
|
||||||
|
//- Secondly, changing the visibility of each item inside the newly selected module
|
||||||
|
// i.e. making a fn a() {} to pub(crate) fn a() {}
|
||||||
|
//
|
||||||
|
//- Thirdly, resolving all the imports this includes removing paths from imports
|
||||||
|
// outside the module, shifting/cloning them inside new module, or shifting the imports, or making
|
||||||
|
// new import statemnts
|
||||||
|
|
||||||
|
//We are getting item usages and record_fields together, record_fields
|
||||||
|
//for change_visibility and usages for first point mentioned above in the process
|
||||||
|
let (usages_to_be_processed, record_fields) = module.get_usages_and_record_fields(ctx);
|
||||||
|
|
||||||
|
let import_paths_to_be_removed = module.resolve_imports(curr_parent_module, &ctx);
|
||||||
|
module.body_items = module.change_visibility(record_fields)?;
|
||||||
|
if module.body_items.len() == 0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("extract_module", AssistKind::RefactorExtract),
|
AssistId("extract_module", AssistKind::RefactorExtract),
|
||||||
"Extract Module",
|
"Extract Module",
|
||||||
module.text_range,
|
module.text_range,
|
||||||
|builder| {
|
|builder| {
|
||||||
//This takes place in three steps:
|
let mut body_items = Vec::new();
|
||||||
//
|
let new_item_indent = old_item_indent + 1;
|
||||||
//- Firstly, we will update the references(usages) e.g. converting a
|
for item in module.body_items {
|
||||||
// function call bar() to modname::bar(), and similarly for other items
|
let item = item.indent(IndentLevel(1));
|
||||||
//
|
let mut indented_item = String::new();
|
||||||
//- Secondly, changing the visibility of each item inside the newly selected module
|
format_to!(indented_item, "{}{}", new_item_indent, item.to_string());
|
||||||
// i.e. making a fn a() {} to pub(crate) fn a() {}
|
body_items.push(indented_item);
|
||||||
//
|
|
||||||
//- Thirdly, resolving all the imports this includes removing paths from imports
|
|
||||||
// outside the module, shifting/cloning them inside new module, or shifting the imports, or making
|
|
||||||
// new import statemnts
|
|
||||||
|
|
||||||
//We are getting item usages and record_fields together, record_fields
|
|
||||||
//for change_visibility and usages for first point mentioned above in the process
|
|
||||||
let (usages_to_be_processed, record_fields) = module.get_usages_and_record_fields(ctx);
|
|
||||||
|
|
||||||
let import_paths_to_be_removed = module.resolve_imports(curr_parent_module, &ctx);
|
|
||||||
|
|
||||||
if let Some(block_items) = module.change_visibility(record_fields) {
|
|
||||||
module.body_items = block_items;
|
|
||||||
if module.body_items.len() == 0 {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut body_items = Vec::new();
|
|
||||||
let new_item_indent = old_item_indent + 1;
|
|
||||||
for item in module.body_items {
|
|
||||||
let item = item.indent(IndentLevel(1));
|
|
||||||
let mut indented_item = String::new();
|
|
||||||
format_to!(indented_item, "{}{}", new_item_indent, item.to_string());
|
|
||||||
body_items.push(indented_item);
|
|
||||||
}
|
|
||||||
|
|
||||||
let body = body_items.join("\n\n");
|
|
||||||
|
|
||||||
let mut module_def = String::new();
|
|
||||||
|
|
||||||
format_to!(module_def, "mod {} {{\n{}\n{}}}", module.name, body, old_item_indent);
|
|
||||||
|
|
||||||
for usages_to_be_updated_for_file in usages_to_be_processed {
|
|
||||||
builder.edit_file(usages_to_be_updated_for_file.0);
|
|
||||||
for usage_to_be_processed in usages_to_be_updated_for_file.1 {
|
|
||||||
builder.replace(usage_to_be_processed.0, usage_to_be_processed.1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.edit_file(ctx.frange.file_id);
|
|
||||||
for import_path_text_range in import_paths_to_be_removed {
|
|
||||||
builder.delete(import_path_text_range);
|
|
||||||
}
|
|
||||||
builder.replace(module.text_range, module_def)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let body = body_items.join("\n\n");
|
||||||
|
|
||||||
|
let mut module_def = String::new();
|
||||||
|
|
||||||
|
format_to!(module_def, "mod {} {{\n{}\n{}}}", module.name, body, old_item_indent);
|
||||||
|
|
||||||
|
let mut usages_to_be_updated_for_curr_file = vec![];
|
||||||
|
for usages_to_be_updated_for_file in usages_to_be_processed {
|
||||||
|
if usages_to_be_updated_for_file.0 == ctx.frange.file_id {
|
||||||
|
usages_to_be_updated_for_curr_file = usages_to_be_updated_for_file.1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
builder.edit_file(usages_to_be_updated_for_file.0);
|
||||||
|
for usage_to_be_processed in usages_to_be_updated_for_file.1 {
|
||||||
|
builder.replace(usage_to_be_processed.0, usage_to_be_processed.1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.edit_file(ctx.frange.file_id);
|
||||||
|
for usage_to_be_processed in usages_to_be_updated_for_curr_file {
|
||||||
|
builder.replace(usage_to_be_processed.0, usage_to_be_processed.1)
|
||||||
|
}
|
||||||
|
|
||||||
|
for import_path_text_range in import_paths_to_be_removed {
|
||||||
|
builder.delete(import_path_text_range);
|
||||||
|
}
|
||||||
|
builder.replace(module.text_range, module_def)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user