mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-01 07:48:45 +00:00
Auto merge of #15434 - alibektas:deunwrap/generate_derive, r=lnicola
minor : Deunwrap generate_derive #15398 subtask 1. Since the editing closure has arms, I did something *experimental* ( in this case just a clever term for bad code ) to bypass creating an `Option` but I am ready to change this.
This commit is contained in:
commit
c0f6ece3c4
1 changed files with 25 additions and 12 deletions
|
@ -27,13 +27,19 @@ pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
|
||||||
let cap = ctx.config.snippet_cap?;
|
let cap = ctx.config.snippet_cap?;
|
||||||
let nominal = ctx.find_node_at_offset::<ast::Adt>()?;
|
let nominal = ctx.find_node_at_offset::<ast::Adt>()?;
|
||||||
let target = nominal.syntax().text_range();
|
let target = nominal.syntax().text_range();
|
||||||
acc.add(AssistId("generate_derive", AssistKind::Generate), "Add `#[derive]`", target, |edit| {
|
|
||||||
let derive_attr = nominal
|
let derive_attr = nominal
|
||||||
.attrs()
|
.attrs()
|
||||||
.filter_map(|x| x.as_simple_call())
|
.filter_map(|x| x.as_simple_call())
|
||||||
.filter(|(name, _arg)| name == "derive")
|
.filter(|(name, _arg)| name == "derive")
|
||||||
.map(|(_name, arg)| arg)
|
.map(|(_name, arg)| arg)
|
||||||
.next();
|
.next();
|
||||||
|
|
||||||
|
let delimiter = match &derive_attr {
|
||||||
|
None => None,
|
||||||
|
Some(tt) => Some(tt.right_delimiter_token()?),
|
||||||
|
};
|
||||||
|
|
||||||
|
acc.add(AssistId("generate_derive", AssistKind::Generate), "Add `#[derive]`", target, |edit| {
|
||||||
match derive_attr {
|
match derive_attr {
|
||||||
None => {
|
None => {
|
||||||
let derive = make::attr_outer(make::meta_token_tree(
|
let derive = make::attr_outer(make::meta_token_tree(
|
||||||
|
@ -45,16 +51,23 @@ pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
|
||||||
let nominal = edit.make_mut(nominal);
|
let nominal = edit.make_mut(nominal);
|
||||||
nominal.add_attr(derive.clone());
|
nominal.add_attr(derive.clone());
|
||||||
|
|
||||||
|
let delimiter = derive
|
||||||
|
.meta()
|
||||||
|
.expect("make::attr_outer was expected to have Meta")
|
||||||
|
.token_tree()
|
||||||
|
.expect("failed to get token tree out of Meta")
|
||||||
|
.r_paren_token()
|
||||||
|
.expect("make::attr_outer was expected to have a R_PAREN");
|
||||||
|
|
||||||
|
edit.add_tabstop_before_token(cap, delimiter);
|
||||||
|
}
|
||||||
|
Some(_) => {
|
||||||
|
// Just move the cursor.
|
||||||
edit.add_tabstop_before_token(
|
edit.add_tabstop_before_token(
|
||||||
cap,
|
cap,
|
||||||
derive.meta().unwrap().token_tree().unwrap().r_paren_token().unwrap(),
|
delimiter.expect("Right delim token could not be found."),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Some(tt) => {
|
|
||||||
// Just move the cursor.
|
|
||||||
let tt = edit.make_mut(tt);
|
|
||||||
edit.add_tabstop_before_token(cap, tt.right_delimiter_token().unwrap());
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue