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

Conversation

yue4u
Copy link
Contributor

@yue4u yue4u commented Nov 12, 2022

close #13597
related: #10173

This PR also happens to fix two extra issues:

  1. The test case in https://github.com/rust-lang/rust-analyzer/blob/master/crates/ide-completion/src/tests/attribute.rs#L778-L801 was never triggered in previous behavior.

after:

tt.mp4
2. completions were triggered even in invalid paths, like
fn main() {
    core:::::$0
}
#[:::::$0]
struct X;

only ::: and :::: is excluded as discussed in #13611 (comment)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 12, 2022
@@ -812,18 +812,6 @@ pub(crate) fn handle_completion(
let completion_trigger_character =
params.context.and_then(|ctx| ctx.trigger_character).and_then(|s| s.chars().next());

if Some(':') == completion_trigger_character {
Copy link
Contributor Author

@yue4u yue4u Nov 12, 2022

Choose a reason for hiding this comment

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

It's possible to keep the filtering here but

  1. it's not testable (here)
  2. we need to parse the file when trigger is Some(':') | None anyway and I hope this change has similar perf (filtered before context creation)
  3. actually the new behavior has nothing to do with the trigger char

Copy link
Member

Choose a reason for hiding this comment

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

the filtering definitely should not be happening here, so moving the handling into the ide-completion crate seems proper to me 👍

@@ -893,3 +902,89 @@ fn f() {
"#]],
);
}

Copy link
Contributor Author

@yue4u yue4u Nov 12, 2022

Choose a reason for hiding this comment

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

not sure if putting tests here (special.rs) is better than https://github.com/rust-lang/rust-analyzer/blob/45ec315e01dc8dd1146dfeb65f0ef6e5c2efed78/crates/ide-completion/src/tests.rs
I didn't find any expect_test usage there and not sure if there are any reasons for that

@yue4u yue4u marked this pull request as draft November 12, 2022 14:02
@yue4u yue4u marked this pull request as ready for review November 12, 2022 14:06
Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

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

Apologies, I thought I had already reviewed this last week

@@ -812,18 +812,6 @@ pub(crate) fn handle_completion(
let completion_trigger_character =
params.context.and_then(|ctx| ctx.trigger_character).and_then(|s| s.chars().next());

if Some(':') == completion_trigger_character {
Copy link
Member

Choose a reason for hiding this comment

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

the filtering definitely should not be happening here, so moving the handling into the ide-completion crate seems proper to me 👍

Comment on lines 575 to 587
T![:] => {
// return if no prev token before colon
let prev_token = original_token.prev_token()?;

// only has a single colon
if prev_token.kind() != T![:] {
return None;
}

if !is_prev_token_valid_path_start_or_segment(&prev_token) {
return None;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we can just return None if we find a T![:] without any additional checks? The previous token can't be another T![:] as the parser should produce a T![::] then I think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Parser will produce two continuous T![:] if we are inside token streams and that's the case I wanted to support as the video in the description.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

However that will produce too much noise if we are accidentally typing like :::. Every single : after will trigger the completion again. So I introduced is_prev_token_valid_path_start_or_segment to solve that. I know it's invalid input but felt this behavior pretty annoying during testing without the check.

Copy link
Member

@Veykril Veykril Nov 24, 2022

Choose a reason for hiding this comment

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

Oh I see, ye then we need something like that here. Though I think this is still too restrictive, so maybe let's instead check that we either have a ::, or exactly two : tokens in a row (but not three)? I think that's a decent trade off unless I am missing something else. We can't make completions in macros perfect unfortunately. In the future if we know about fragment types for inputs this can be potentially refined.

Copy link
Contributor Author

@yue4u yue4u Nov 26, 2022

Choose a reason for hiding this comment

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

updated in .b701b14ca5b195fd6e227b913a912a4fcc87efb0 I mean e1de04d

@Veykril Veykril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 19, 2022
@yue4u
Copy link
Contributor Author

yue4u commented Nov 19, 2022

I removed the check before :: in 7a568f7 but still not sure about #13611 (comment)

  1. if we don't bother completing inside token tree then it's ok to remove the checking (same as current behavior)
  2. if we want to completing inside token tree
    1. I think it's better to deal with the ::: case but it's not necessary
    2. more invalid completions might be triggered than before

@yue4u
Copy link
Contributor Author

yue4u commented Nov 19, 2022

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 19, 2022
@Veykril Veykril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 24, 2022
@yue4u yue4u force-pushed the fix/completion-after-colon branch from b701b14 to e1de04d Compare November 26, 2022 16:53
@yue4u
Copy link
Contributor Author

yue4u commented Nov 26, 2022

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 26, 2022
);
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

@Veykril
Copy link
Member

Veykril commented Nov 26, 2022

@bors delegate+

@bors
Copy link
Contributor

bors commented Nov 26, 2022

✌️ @yue4u can now approve this pull request

@yue4u
Copy link
Contributor Author

yue4u commented Nov 26, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Nov 26, 2022

📌 Commit 1ca5cb7 has been approved by yue4u

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Nov 26, 2022

⌛ Testing commit 1ca5cb7 with merge 34e2bc6...

@bors
Copy link
Contributor

bors commented Nov 26, 2022

☀️ Test successful - checks-actions
Approved by: yue4u
Pushing 34e2bc6 to master...

@bors bors merged commit 34e2bc6 into rust-lang:master Nov 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Completions on single colons
4 participants