Skip to content
Open
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
101 changes: 98 additions & 3 deletions crates/ide-assists/src/handlers/generate_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext<'_, '_>) -> O
let name = nominal.name()?;
let target = nominal.syntax().text_range();

if ctx.find_node_at_offset::<ast::RecordFieldList>().is_some() {
if !can_trigger_assist(ctx, &nominal) {
return None;
}

Expand Down Expand Up @@ -98,7 +98,7 @@ pub(crate) fn generate_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_, '_>
let name = nominal.name()?;
let target = nominal.syntax().text_range();

if ctx.find_node_at_offset::<ast::RecordFieldList>().is_some() {
if !can_trigger_assist(ctx, &nominal) {
return None;
}

Expand Down Expand Up @@ -243,6 +243,21 @@ pub(crate) fn generate_impl_trait(acc: &mut Assists, ctx: &AssistContext<'_, '_>
)
}

fn can_trigger_assist(ctx: &AssistContext<'_, '_>, node: &impl AstNode) -> bool {
let body = node
.syntax()
.children()
.find(|it| it.first_child_or_token().is_some_and(|it| it.kind().is_l_delimiter()));

let Some(body) = body else { return true };
let l_delim = body.first_child_or_token().filter(|it| it.kind().is_l_delimiter());
let r_delim = body.last_child_or_token().filter(|it| it.kind().is_r_delimiter());

l_delim.is_some_and(|it| it.text_range().contains_inclusive(ctx.offset()))
|| r_delim.is_some_and(|it| it.text_range().contains_inclusive(ctx.offset()))
|| !body.text_range().contains(ctx.offset())
}

fn extract_expr(item: ast::AssocItem) -> Option<ast::Expr> {
let ast::AssocItem::Fn(f) = item else {
return None;
Expand All @@ -252,7 +267,7 @@ fn extract_expr(item: ast::AssocItem) -> Option<ast::Expr> {

#[cfg(test)]
mod tests {
use crate::tests::{check_assist, check_assist_target};
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};

use super::*;

Expand Down Expand Up @@ -395,6 +410,86 @@ mod tests {
);
}

#[test]
fn test_add_impl_applicable() {
check_assist(
generate_impl,
r#"
struct Foo$0 {
x: i32
}
"#,
r#"
struct Foo {
x: i32
}

impl Foo {$0}
"#,
);
check_assist(
generate_impl,
r#"
struct Foo {$0
x: i32
}
"#,
r#"
struct Foo {
x: i32
}

impl Foo {$0}
"#,
);
check_assist(
generate_impl,
r#"
struct Foo {
x: i32
}$0
"#,
r#"
struct Foo {
x: i32
}

impl Foo {$0}
"#,
);
check_assist_not_applicable(
generate_impl,
r#"
struct Foo {
$0x: i32
}
"#,
);
check_assist(
generate_trait_impl,
r#"
struct Foo {
x: i32
}$0
"#,
r#"
struct Foo {
x: i32
}

impl ${1:_} for Foo {$0}
"#,
);
check_assist_not_applicable(
generate_trait_impl,
r#"
struct Foo {
$0x: i32
}
"#,
);
}

#[test]
fn add_impl_target() {
check_assist_target(
Expand Down
15 changes: 15 additions & 0 deletions crates/parser/src/syntax_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,19 @@ impl SyntaxKind {
// Assuming no edition removed keywords...
self == SyntaxKind::IDENT || self.is_keyword(Edition::LATEST)
}

#[inline]
pub fn is_l_delimiter(self) -> bool {
matches!(self, SyntaxKind::L_CURLY | Self::L_PAREN | Self::L_BRACK)
}

#[inline]
pub fn is_r_delimiter(self) -> bool {
matches!(self, SyntaxKind::R_CURLY | Self::R_PAREN | Self::R_BRACK)
}

#[inline]
pub fn is_delimiter(self) -> bool {
self.is_l_delimiter() || self.is_r_delimiter()
}
}
Loading