Skip to content

fix(completion): remove bound insert of type in trait #13831

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
Dec 23, 2022
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
21 changes: 16 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,7 +37,7 @@ use ide_db::{
traits::get_missing_assoc_items, SymbolKind,
};
use syntax::{
ast::{self, edit_in_place::AttrsOwnerEdit},
ast::{self, edit_in_place::AttrsOwnerEdit, HasTypeBounds},
AstNode, SyntaxElement, SyntaxKind, TextRange, T,
};
use text_edit::TextEdit;
Expand Down Expand Up @@ -265,10 +265,21 @@ fn add_type_alias_impl(
};

let start = transformed_ty.syntax().text_range().start();
let Some(end) = transformed_ty
.eq_token()
.map(|tok| tok.text_range().start())
.or(transformed_ty.semicolon_token().map(|tok| tok.text_range().start())) else { return };

let end = if let Some(end) =
transformed_ty.colon_token().map(|tok| tok.text_range().start())
{
end
} else if let Some(end) = transformed_ty.eq_token().map(|tok| tok.text_range().start())
{
end
} else if let Some(end) =
transformed_ty.semicolon_token().map(|tok| tok.text_range().start())
{
end
} else {
return;
};

let len = end - start;
let mut decl = transformed_ty.syntax().text().slice(..len).to_string();
Expand Down
90 changes: 89 additions & 1 deletion crates/ide-completion/src/tests/item_list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Completion tests for item list position.
use expect_test::{expect, Expect};

use crate::tests::{completion_list, BASE_ITEMS_FIXTURE};
use crate::tests::{check_edit, completion_list, BASE_ITEMS_FIXTURE};

fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list(&format!("{}{}", BASE_ITEMS_FIXTURE, ra_fixture));
Expand Down Expand Up @@ -277,3 +277,91 @@ fn after_unit_struct() {
"#]],
);
}

#[test]
fn type_in_impl_trait() {
check_edit(
"type O",
r"
struct A;
trait B {
type O: ?Sized;
}
impl B for A {
$0
}
",
r#"
struct A;
trait B {
type O: ?Sized;
}
impl B for A {
type O = $0;
}
"#,
);
check_edit(
"type O",
r"
struct A;
trait B {
type O;
}
impl B for A {
$0
}
",
r#"
struct A;
trait B {
type O;
}
impl B for A {
type O = $0;
}
"#,
);
check_edit(
"type O",
r"
struct A;
trait B {
type O: ?Sized = u32;
}
impl B for A {
$0
}
",
r#"
struct A;
trait B {
type O: ?Sized = u32;
}
impl B for A {
type O = $0;
}
"#,
);
check_edit(
"type O",
r"
struct A;
trait B {
type O = u32;
}
impl B for A {
$0
}
",
r"
struct A;
trait B {
type O = u32;
}
impl B for A {
type O = $0;
}
",
)
}