Skip to content

fix: filter unnecessary completions after colon #13611

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 5 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 3 additions & 19 deletions crates/ide-completion/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,9 @@ impl<'a> CompletionContext<'a> {
return None;
}

if !is_prev_token_valid_path_start_or_segment(&prev_token) {
// has 3 colon in a row
// special casing this as per discussion in https://github.com/rust-lang/rust-analyzer/pull/13611#discussion_r1031845205
if prev_token.prev_token().map(|t| t.kind() == T![:]).unwrap_or(false) {
return None;
}
}
Expand Down Expand Up @@ -637,24 +639,6 @@ impl<'a> CompletionContext<'a> {
}
}

fn is_prev_token_valid_path_start_or_segment(token: &SyntaxToken) -> bool {
if let Some(prev_token) = token.prev_token() {
// token before coloncolon is invalid
if !matches!(
prev_token.kind(),
// trival
WHITESPACE | COMMENT
// PathIdentSegment
| IDENT | T![super] | T![self] | T![Self] | T![crate]
// QualifiedPath
| T![>]
) {
return false;
}
}
true
}

const OP_TRAIT_LANG_NAMES: &[&str] = &[
"add_assign",
"add",
Expand Down
8 changes: 1 addition & 7 deletions crates/ide-completion/src/tests/special.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,16 +967,10 @@ fn foo { crate:$0 }
}

#[test]
fn no_completions_in_invalid_path() {
fn no_completions_in_after_tripple_colon() {
check(
r#"
fn foo { crate:::$0 }
"#,
expect![""],
);
check(
r#"
fn foo { crate::::$0 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah wait, we no longer catch this because the token here is a :: again right? Let's add that to the check as well as it is simple, I forgot that :) That is, t.kind() == T![:] || t.kind() == T![::] for the previous token check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done 1ca5cb7

"#,
expect![""],
);
Expand Down