Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ra_syntax::{

use crate::{AssistContext, AssistId, AssistKind, Assists};

// Assist: add_derive
// Assist: generate_derive
//
// Adds a new `#[derive()]` clause to a struct or enum.
//
Expand All @@ -24,32 +24,37 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
// y: u32,
// }
// ```
pub(crate) fn add_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
pub(crate) fn generate_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let cap = ctx.config.snippet_cap?;
let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
let node_start = derive_insertion_offset(&nominal)?;
let target = nominal.syntax().text_range();
acc.add(AssistId("add_derive", AssistKind::None), "Add `#[derive]`", target, |builder| {
let derive_attr = nominal
.attrs()
.filter_map(|x| x.as_simple_call())
.filter(|(name, _arg)| name == "derive")
.map(|(_name, arg)| arg)
.next();
match derive_attr {
None => {
builder.insert_snippet(cap, node_start, "#[derive($0)]\n");
}
Some(tt) => {
// Just move the cursor.
builder.insert_snippet(
cap,
tt.syntax().text_range().end() - TextSize::of(')'),
"$0",
)
}
};
})
acc.add(
AssistId("generate_derive", AssistKind::Generate),
"Add `#[derive]`",
target,
|builder| {
let derive_attr = nominal
.attrs()
.filter_map(|x| x.as_simple_call())
.filter(|(name, _arg)| name == "derive")
.map(|(_name, arg)| arg)
.next();
match derive_attr {
None => {
builder.insert_snippet(cap, node_start, "#[derive($0)]\n");
}
Some(tt) => {
// Just move the cursor.
builder.insert_snippet(
cap,
tt.syntax().text_range().end() - TextSize::of(')'),
"$0",
)
}
};
},
)
}

// Insert `derive` after doc comments.
Expand All @@ -70,12 +75,12 @@ mod tests {
#[test]
fn add_derive_new() {
check_assist(
add_derive,
generate_derive,
"struct Foo { a: i32, <|>}",
"#[derive($0)]\nstruct Foo { a: i32, }",
);
check_assist(
add_derive,
generate_derive,
"struct Foo { <|> a: i32, }",
"#[derive($0)]\nstruct Foo { a: i32, }",
);
Expand All @@ -84,7 +89,7 @@ mod tests {
#[test]
fn add_derive_existing() {
check_assist(
add_derive,
generate_derive,
"#[derive(Clone)]\nstruct Foo { a: i32<|>, }",
"#[derive(Clone$0)]\nstruct Foo { a: i32, }",
);
Expand All @@ -93,7 +98,7 @@ mod tests {
#[test]
fn add_derive_new_with_doc_comment() {
check_assist(
add_derive,
generate_derive,
"
/// `Foo` is a pretty important struct.
/// It does stuff.
Expand All @@ -111,7 +116,7 @@ struct Foo { a: i32, }
#[test]
fn add_derive_target() {
check_assist_target(
add_derive,
generate_derive,
"
struct SomeThingIrrelevant;
/// `Foo` is a pretty important struct.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use test_utils::mark;

use crate::{utils::FamousDefs, AssistContext, AssistId, AssistKind, Assists};

// Assist: add_from_impl_for_enum
// Assist: generate_from_impl_for_enum
//
// Adds a From impl for an enum variant with one tuple field.
//
Expand All @@ -21,7 +21,7 @@ use crate::{utils::FamousDefs, AssistContext, AssistId, AssistKind, Assists};
// }
// }
// ```
pub(crate) fn add_from_impl_for_enum(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
pub(crate) fn generate_from_impl_for_enum(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let variant = ctx.find_node_at_offset::<ast::EnumVariant>()?;
let variant_name = variant.name()?;
let enum_name = variant.parent_enum().name()?;
Expand All @@ -45,8 +45,8 @@ pub(crate) fn add_from_impl_for_enum(acc: &mut Assists, ctx: &AssistContext) ->

let target = variant.syntax().text_range();
acc.add(
AssistId("add_from_impl_for_enum", AssistKind::Refactor),
"Add From impl for this enum variant",
AssistId("generate_from_impl_for_enum", AssistKind::Generate),
"Generate `From` impl for this enum variant",
target,
|edit| {
let start_offset = variant.parent_enum().syntax().text_range().end();
Expand Down Expand Up @@ -97,9 +97,9 @@ mod tests {
use super::*;

#[test]
fn test_add_from_impl_for_enum() {
fn test_generate_from_impl_for_enum() {
check_assist(
add_from_impl_for_enum,
generate_from_impl_for_enum,
"enum A { <|>One(u32) }",
r#"enum A { One(u32) }

Expand All @@ -112,9 +112,9 @@ impl From<u32> for A {
}

#[test]
fn test_add_from_impl_for_enum_complicated_path() {
fn test_generate_from_impl_for_enum_complicated_path() {
check_assist(
add_from_impl_for_enum,
generate_from_impl_for_enum,
r#"enum A { <|>One(foo::bar::baz::Boo) }"#,
r#"enum A { One(foo::bar::baz::Boo) }

Expand All @@ -129,7 +129,7 @@ impl From<foo::bar::baz::Boo> for A {
fn check_not_applicable(ra_fixture: &str) {
let fixture =
format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE);
check_assist_not_applicable(add_from_impl_for_enum, &fixture)
check_assist_not_applicable(generate_from_impl_for_enum, &fixture)
}

#[test]
Expand Down Expand Up @@ -166,7 +166,7 @@ impl From<u32> for A {
#[test]
fn test_add_from_impl_different_variant_impl_exists() {
check_assist(
add_from_impl_for_enum,
generate_from_impl_for_enum,
r#"enum A { <|>One(u32), Two(String), }

impl From<String> for A {
Expand Down
Loading