Clean up definition completions docs and tests (#21183)

<!--
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.
This commit is contained in:
Matthew Mckee 2025-11-02 13:01:06 +00:00 committed by GitHub
parent bff32a41dc
commit de1a6fb8ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -830,16 +830,14 @@ fn find_typed_text(
Some(source[last.range()].to_string()) Some(source[last.range()].to_string())
} }
/// Whether the given offset within the parsed module is within /// Whether the last token is within a comment or not.
/// a comment or not.
fn is_in_comment(tokens: &[Token]) -> bool { fn is_in_comment(tokens: &[Token]) -> bool {
tokens.last().is_some_and(|t| t.kind().is_comment()) tokens.last().is_some_and(|t| t.kind().is_comment())
} }
/// Returns true when the cursor at `offset` is positioned within /// Whether the last token is positioned within a string token (regular, f-string, t-string, etc).
/// a string token (regular, f-string, t-string, etc).
/// ///
/// Note that this will return `false` when positioned within an /// Note that this will return `false` when the last token is positioned within an
/// interpolation block in an f-string or a t-string. /// interpolation block in an f-string or a t-string.
fn is_in_string(tokens: &[Token]) -> bool { fn is_in_string(tokens: &[Token]) -> bool {
tokens.last().is_some_and(|t| { tokens.last().is_some_and(|t| {
@ -850,9 +848,7 @@ fn is_in_string(tokens: &[Token]) -> bool {
}) })
} }
/// If the tokens end with `class f` or `def f` we return true. /// Returns true when the tokens indicate that the definition of a new name is being introduced at the end.
/// If the tokens end with `class` or `def`, we return false.
/// This is fine because we don't provide completions anyway.
fn is_in_definition_place(db: &dyn Db, tokens: &[Token], file: File) -> bool { fn is_in_definition_place(db: &dyn Db, tokens: &[Token], file: File) -> bool {
let is_definition_keyword = |token: &Token| { let is_definition_keyword = |token: &Token| {
if matches!( if matches!(
@ -4088,11 +4084,13 @@ def f[T](x: T):
fn no_completions_in_function_def_name() { fn no_completions_in_function_def_name() {
let builder = completion_test_builder( let builder = completion_test_builder(
"\ "\
foo = 1
def f<CURSOR> def f<CURSOR>
", ",
); );
assert!(builder.auto_import().build().completions().is_empty()); assert!(builder.build().completions().is_empty());
} }
#[test] #[test]
@ -4104,18 +4102,20 @@ def <CURSOR>
); );
// This is okay because the ide will not request completions when the cursor is in this position. // This is okay because the ide will not request completions when the cursor is in this position.
assert!(!builder.auto_import().build().completions().is_empty()); assert!(!builder.build().completions().is_empty());
} }
#[test] #[test]
fn no_completions_in_class_def_name() { fn no_completions_in_class_def_name() {
let builder = completion_test_builder( let builder = completion_test_builder(
"\ "\
foo = 1
class f<CURSOR> class f<CURSOR>
", ",
); );
assert!(builder.auto_import().build().completions().is_empty()); assert!(builder.build().completions().is_empty());
} }
#[test] #[test]
@ -4127,29 +4127,33 @@ class <CURSOR>
); );
// This is okay because the ide will not request completions when the cursor is in this position. // This is okay because the ide will not request completions when the cursor is in this position.
assert!(!builder.auto_import().build().completions().is_empty()); assert!(!builder.build().completions().is_empty());
} }
#[test] #[test]
fn no_completions_in_type_def_name() { fn no_completions_in_type_def_name() {
let builder = completion_test_builder( let builder = completion_test_builder(
"\ "\
foo = 1
type f<CURSOR> = int type f<CURSOR> = int
", ",
); );
assert!(builder.auto_import().build().completions().is_empty()); assert!(builder.build().completions().is_empty());
} }
#[test] #[test]
fn no_completions_in_maybe_type_def_name() { fn no_completions_in_maybe_type_def_name() {
let builder = completion_test_builder( let builder = completion_test_builder(
"\ "\
foo = 1
type f<CURSOR> type f<CURSOR>
", ",
); );
assert!(builder.auto_import().build().completions().is_empty()); assert!(builder.build().completions().is_empty());
} }
#[test] #[test]
@ -4161,7 +4165,7 @@ type <CURSOR>
); );
// This is okay because the ide will not request completions when the cursor is in this position. // This is okay because the ide will not request completions when the cursor is in this position.
assert!(!builder.auto_import().build().completions().is_empty()); assert!(!builder.build().completions().is_empty());
} }
/// A way to create a simple single-file (named `main.py`) completion test /// A way to create a simple single-file (named `main.py`) completion test