mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-08-04 18:58:41 +00:00
Migrate toggle_macro_delimiter
assist to SyntaxEditor
Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
This commit is contained in:
parent
cb52f4c3c9
commit
3af0f4f6be
1 changed files with 14 additions and 12 deletions
|
@ -1,8 +1,7 @@
|
|||
use ide_db::assists::AssistId;
|
||||
use syntax::{
|
||||
AstNode, T,
|
||||
ast::{self, make},
|
||||
ted,
|
||||
ast::{self, syntax_factory::SyntaxFactory},
|
||||
};
|
||||
|
||||
use crate::{AssistContext, Assists};
|
||||
|
@ -37,8 +36,7 @@ pub(crate) fn toggle_macro_delimiter(acc: &mut Assists, ctx: &AssistContext<'_>)
|
|||
RCur,
|
||||
}
|
||||
|
||||
let makro = ctx.find_node_at_offset::<ast::MacroCall>()?.clone_for_update();
|
||||
let makro_text_range = makro.syntax().text_range();
|
||||
let makro = ctx.find_node_at_offset::<ast::MacroCall>()?;
|
||||
|
||||
let cursor_offset = ctx.offset();
|
||||
let semicolon = makro.semicolon_token();
|
||||
|
@ -71,24 +69,28 @@ pub(crate) fn toggle_macro_delimiter(acc: &mut Assists, ctx: &AssistContext<'_>)
|
|||
},
|
||||
token_tree.syntax().text_range(),
|
||||
|builder| {
|
||||
let make = SyntaxFactory::with_mappings();
|
||||
let mut editor = builder.make_editor(makro.syntax());
|
||||
|
||||
match token {
|
||||
MacroDelims::LPar | MacroDelims::RPar => {
|
||||
ted::replace(ltoken, make::token(T!['{']));
|
||||
ted::replace(rtoken, make::token(T!['}']));
|
||||
editor.replace(ltoken, make.token(T!['{']));
|
||||
editor.replace(rtoken, make.token(T!['}']));
|
||||
if let Some(sc) = semicolon {
|
||||
ted::remove(sc);
|
||||
editor.delete(sc);
|
||||
}
|
||||
}
|
||||
MacroDelims::LBra | MacroDelims::RBra => {
|
||||
ted::replace(ltoken, make::token(T!['(']));
|
||||
ted::replace(rtoken, make::token(T![')']));
|
||||
editor.replace(ltoken, make.token(T!['(']));
|
||||
editor.replace(rtoken, make.token(T![')']));
|
||||
}
|
||||
MacroDelims::LCur | MacroDelims::RCur => {
|
||||
ted::replace(ltoken, make::token(T!['[']));
|
||||
ted::replace(rtoken, make::token(T![']']));
|
||||
editor.replace(ltoken, make.token(T!['[']));
|
||||
editor.replace(rtoken, make.token(T![']']));
|
||||
}
|
||||
}
|
||||
builder.replace(makro_text_range, makro.syntax().text());
|
||||
editor.add_mappings(make.finish_with_mappings());
|
||||
builder.add_file_edits(ctx.vfs_file_id(), editor);
|
||||
},
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue