Skip to content

Commit c0f6ece

Browse files
committed
Auto merge of rust-lang#15434 - alibektas:deunwrap/generate_derive, r=lnicola
minor : Deunwrap generate_derive rust-lang#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.
2 parents d895e9e + 2a78173 commit c0f6ece

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

crates/ide-assists/src/handlers/generate_derive.rs

+25-12
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
2727
let cap = ctx.config.snippet_cap?;
2828
let nominal = ctx.find_node_at_offset::<ast::Adt>()?;
2929
let target = nominal.syntax().text_range();
30+
let derive_attr = nominal
31+
.attrs()
32+
.filter_map(|x| x.as_simple_call())
33+
.filter(|(name, _arg)| name == "derive")
34+
.map(|(_name, arg)| arg)
35+
.next();
36+
37+
let delimiter = match &derive_attr {
38+
None => None,
39+
Some(tt) => Some(tt.right_delimiter_token()?),
40+
};
41+
3042
acc.add(AssistId("generate_derive", AssistKind::Generate), "Add `#[derive]`", target, |edit| {
31-
let derive_attr = nominal
32-
.attrs()
33-
.filter_map(|x| x.as_simple_call())
34-
.filter(|(name, _arg)| name == "derive")
35-
.map(|(_name, arg)| arg)
36-
.next();
3743
match derive_attr {
3844
None => {
3945
let derive = make::attr_outer(make::meta_token_tree(
@@ -45,16 +51,23 @@ pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opt
4551
let nominal = edit.make_mut(nominal);
4652
nominal.add_attr(derive.clone());
4753

54+
let delimiter = derive
55+
.meta()
56+
.expect("make::attr_outer was expected to have Meta")
57+
.token_tree()
58+
.expect("failed to get token tree out of Meta")
59+
.r_paren_token()
60+
.expect("make::attr_outer was expected to have a R_PAREN");
61+
62+
edit.add_tabstop_before_token(cap, delimiter);
63+
}
64+
Some(_) => {
65+
// Just move the cursor.
4866
edit.add_tabstop_before_token(
4967
cap,
50-
derive.meta().unwrap().token_tree().unwrap().r_paren_token().unwrap(),
68+
delimiter.expect("Right delim token could not be found."),
5169
);
5270
}
53-
Some(tt) => {
54-
// Just move the cursor.
55-
let tt = edit.make_mut(tt);
56-
edit.add_tabstop_before_token(cap, tt.right_delimiter_token().unwrap());
57-
}
5871
};
5972
})
6073
}

0 commit comments

Comments
 (0)