mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-04 10:50:15 +00:00
Migrate promote_local_to_const
Assist to SyntaxEditor
Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
This commit is contained in:
parent
ce2c0c968a
commit
a423b7c7cc
1 changed files with 13 additions and 9 deletions
|
@ -3,8 +3,7 @@ use ide_db::{assists::AssistId, defs::Definition};
|
|||
use stdx::to_upper_snake_case;
|
||||
use syntax::{
|
||||
AstNode,
|
||||
ast::{self, HasName, make},
|
||||
ted,
|
||||
ast::{self, HasName, syntax_factory::SyntaxFactory},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
@ -69,15 +68,18 @@ pub(crate) fn promote_local_to_const(acc: &mut Assists, ctx: &AssistContext<'_>)
|
|||
"Promote local to constant",
|
||||
let_stmt.syntax().text_range(),
|
||||
|edit| {
|
||||
let make = SyntaxFactory::with_mappings();
|
||||
let mut editor = edit.make_editor(let_stmt.syntax());
|
||||
let name = to_upper_snake_case(&name.to_string());
|
||||
let usages = Definition::Local(local).usages(&ctx.sema).all();
|
||||
if let Some(usages) = usages.references.get(&ctx.file_id()) {
|
||||
let name_ref = make::name_ref(&name);
|
||||
let name_ref = make.name_ref(&name);
|
||||
|
||||
for usage in usages {
|
||||
let Some(usage_name) = usage.name.as_name_ref().cloned() else { continue };
|
||||
if let Some(record_field) = ast::RecordExprField::for_name_ref(&usage_name) {
|
||||
let name_expr = make::expr_path(make::path_from_text(&name));
|
||||
let path = make.ident_path(&name);
|
||||
let name_expr = make.expr_path(path);
|
||||
utils::replace_record_field_expr(ctx, edit, record_field, name_expr);
|
||||
} else {
|
||||
let usage_range = usage.range;
|
||||
|
@ -86,15 +88,17 @@ pub(crate) fn promote_local_to_const(acc: &mut Assists, ctx: &AssistContext<'_>)
|
|||
}
|
||||
}
|
||||
|
||||
let item = make::item_const(None, make::name(&name), make::ty(&ty), initializer)
|
||||
.clone_for_update();
|
||||
let let_stmt = edit.make_mut(let_stmt);
|
||||
let item = make.item_const(None, make.name(&name), make.ty(&ty), initializer);
|
||||
|
||||
if let Some((cap, name)) = ctx.config.snippet_cap.zip(item.name()) {
|
||||
edit.add_tabstop_before(cap, name);
|
||||
let tabstop = edit.make_tabstop_before(cap);
|
||||
editor.add_annotation(name.syntax().clone(), tabstop);
|
||||
}
|
||||
|
||||
ted::replace(let_stmt.syntax(), item.syntax());
|
||||
editor.replace(let_stmt.syntax(), item.syntax());
|
||||
|
||||
editor.add_mappings(make.finish_with_mappings());
|
||||
edit.add_file_edits(ctx.vfs_file_id(), editor);
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue