-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Don't provide completions when in class or function definition #21146
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
[ty] Don't provide completions when in class or function definition #21146
Conversation
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
MichaReiser
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you
crates/ty_ide/src/completion.rs
Outdated
| if is_in_comment(&parsed, offset) | ||
| || is_in_string(&parsed, offset) | ||
| || is_in_definition_place(&parsed, offset) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we pull out the tokens.before()? It seems every one of those functions now searches the tokens before offset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea
crates/ty_ide/src/completion.rs
Outdated
| .len() | ||
| .checked_sub(2) | ||
| .and_then(|i| tokens.get(i)) | ||
| .is_some_and(|t| matches!(t.kind(), TokenKind::Def | TokenKind::Class)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to mention this in my PR but we should probably do this too in `type .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(unless i look directly at the string, but I'm not sure if this is right or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right, because it is a contextual keyword. I think inspecting the source would be fine here because I don't see any case where type <name> is valid.
crates/ty_ide/src/completion.rs
Outdated
| ) || file.read_to_string(db).is_ok_and(|source| { | ||
| source[t.range().start().to_usize()..t.range().end().to_usize()] == *"type" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not seem like the right approach, but i can't seem to find any other apis to use to do this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You want something like:
if matches!(t.kind(), TokenKind::Def | TokenKind::Class | TokenKind::Type) {
true
} else if t.kind() == TokenKind::Name {
let source = source_text(db, file);
&source[t.range()] == "type"
} else {
false
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool, thanks
MichaReiser
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sweet
BurntSushi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize this PR was already merged, but I wanted to leave feedback anyway.
I can address these in a follow-up PR if you don't want though. :-)
| /// a string token (regular, f-string, t-string, etc). | ||
| /// | ||
| /// Note that this will return `false` when positioned within an | ||
| /// interpolation block in an f-string or a t-string. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc comments for this function (and the one above) should be updated.
| ); | ||
|
|
||
| builder.auto_import().build().not_contains("fabs"); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think these tests need to be enabling auto_import(). That's a bit of a big hammer. You can instead add a local item in the source file and test completions on that.
|
|
||
| /// If the tokens end with `class f` or `def f` we return true. | ||
| /// If the tokens end with `class` or `def`, we return false. | ||
| /// This is fine because we don't provide completions anyway. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to not repeat the implementation in the doc comment where possible. e.g., Your doc comment doesn't include type. Instead, something like, "Returns true when the tokens indicate that the definition of a new name is being introduced at the end."
* origin/main: [ty] Fix generic inference for non-dataclass inheriting from generic dataclass (#21159) Update etcetera to 0.11.0 (#21160) Fix missing diagnostics for notebooks (#21156) [ty] Fix tests for definition completions (#21153) Bump v0.14.3 (#21152) [ty] Don't provide completions when in class or function definition (#21146) [ty] Use the top materialization of classes for narrowing in class-patterns for `match` statements (#21150)
|
Thanks for the feedback, ill address this in a PR |
<!-- Thank you for contributing to Ruff/ty! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? (Please prefix with `[ty]` for ty pull requests.) - Does this pull request include references to any relevant issues? --> ## Summary @BurntSushi provided some feedback in #21146 so i address it here.

Summary
Resolves astral-sh/ty#1457
We now check if we are in a situation where we are in the name of class or function definition like
def forclass f. We check if the second last token isdeforclass.Test Plan
Added test shown in that issue.
Also add test for situation where we have not started typing the name like
defandclass. My changes do not change any behavior there, but i think it is good to show we still dont provide completions there