Skip to content

Fix assoc type where clause position #20235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2025
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
53 changes: 48 additions & 5 deletions crates/ide-completion/src/completions/item_list/trait_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use ide_db::{
SymbolKind, documentation::HasDocs, path_transform::PathTransform,
syntax_helpers::prettify_macro_expansion, traits::get_missing_assoc_items,
};
use syntax::ast::HasGenericParams;
use syntax::{
AstNode, SmolStr, SyntaxElement, SyntaxKind, T, TextRange, ToSmolStr,
ast::{self, HasGenericArgs, HasTypeBounds, edit_in_place::AttrsOwnerEdit, make},
Expand Down Expand Up @@ -388,6 +389,12 @@ fn add_type_alias_impl(
{
end
} else if let Some(end) = transformed_ty.eq_token().map(|tok| tok.text_range().start())
{
end
} else if let Some(end) = transformed_ty
.where_clause()
.and_then(|wc| wc.where_token())
.map(|tok| tok.text_range().start())
{
end
} else if let Some(end) =
Expand All @@ -400,17 +407,29 @@ fn add_type_alias_impl(

let len = end - start;
let mut decl = transformed_ty.syntax().text().slice(..len).to_string();
if !decl.ends_with(' ') {
decl.push(' ');
}
decl.push_str("= ");
decl.truncate(decl.trim_end().len());
decl.push_str(" = ");

let wc = transformed_ty
.where_clause()
.map(|wc| {
let ws = wc
.where_token()
.and_then(|it| it.prev_token())
.filter(|token| token.kind() == SyntaxKind::WHITESPACE)
.map(|token| token.to_string())
.unwrap_or_else(|| " ".into());
format!("{ws}{wc}")
})
.unwrap_or_default();

match ctx.config.snippet_cap {
Some(cap) => {
let snippet = format!("{decl}$0;");
let snippet = format!("{decl}$0{wc};");
item.snippet_edit(cap, TextEdit::replace(replacement_range, snippet));
}
None => {
decl.push_str(&wc);
item.text_edit(TextEdit::replace(replacement_range, decl));
}
};
Expand Down Expand Up @@ -1437,6 +1456,30 @@ trait Tr<'b> {
impl<'b> Tr<'b> for () {
type Ty<'a: 'b, T: Copy, const C: usize> = $0;
}
"#,
);
}
#[test]
fn includes_where_clause() {
check_edit(
"type Ty",
r#"
trait Tr {
type Ty where Self: Copy;
}

impl Tr for () {
$0
}
"#,
r#"
trait Tr {
type Ty where Self: Copy;
}

impl Tr for () {
type Ty = $0 where Self: Copy;
}
"#,
);
}
Expand Down
27 changes: 27 additions & 0 deletions crates/ide-completion/src/tests/item_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,33 @@ type O = $0;
r"
struct A;
trait B {
type O<'a>
where
Self: 'a;
}
impl B for A {
$0
}
",
r#"
struct A;
trait B {
type O<'a>
where
Self: 'a;
}
impl B for A {
type O<'a> = $0
where
Self: 'a;
}
"#,
);
check_edit(
"type O",
r"
struct A;
trait B {
type O: ?Sized = u32;
}
impl B for A {
Expand Down
Loading