Skip to content

Commit de1a6fb

Browse files
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.
1 parent bff32a4 commit de1a6fb

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

crates/ty_ide/src/completion.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -830,16 +830,14 @@ fn find_typed_text(
830830
Some(source[last.range()].to_string())
831831
}
832832

833-
/// Whether the given offset within the parsed module is within
834-
/// a comment or not.
833+
/// Whether the last token is within a comment or not.
835834
fn is_in_comment(tokens: &[Token]) -> bool {
836835
tokens.last().is_some_and(|t| t.kind().is_comment())
837836
}
838837

839-
/// Returns true when the cursor at `offset` is positioned within
840-
/// a string token (regular, f-string, t-string, etc).
838+
/// Whether the last token is positioned within a string token (regular, f-string, t-string, etc).
841839
///
842-
/// Note that this will return `false` when positioned within an
840+
/// Note that this will return `false` when the last token is positioned within an
843841
/// interpolation block in an f-string or a t-string.
844842
fn is_in_string(tokens: &[Token]) -> bool {
845843
tokens.last().is_some_and(|t| {
@@ -850,9 +848,7 @@ fn is_in_string(tokens: &[Token]) -> bool {
850848
})
851849
}
852850

853-
/// If the tokens end with `class f` or `def f` we return true.
854-
/// If the tokens end with `class` or `def`, we return false.
855-
/// This is fine because we don't provide completions anyway.
851+
/// Returns true when the tokens indicate that the definition of a new name is being introduced at the end.
856852
fn is_in_definition_place(db: &dyn Db, tokens: &[Token], file: File) -> bool {
857853
let is_definition_keyword = |token: &Token| {
858854
if matches!(
@@ -4088,11 +4084,13 @@ def f[T](x: T):
40884084
fn no_completions_in_function_def_name() {
40894085
let builder = completion_test_builder(
40904086
"\
4087+
foo = 1
4088+
40914089
def f<CURSOR>
40924090
",
40934091
);
40944092

4095-
assert!(builder.auto_import().build().completions().is_empty());
4093+
assert!(builder.build().completions().is_empty());
40964094
}
40974095

40984096
#[test]
@@ -4104,18 +4102,20 @@ def <CURSOR>
41044102
);
41054103

41064104
// This is okay because the ide will not request completions when the cursor is in this position.
4107-
assert!(!builder.auto_import().build().completions().is_empty());
4105+
assert!(!builder.build().completions().is_empty());
41084106
}
41094107

41104108
#[test]
41114109
fn no_completions_in_class_def_name() {
41124110
let builder = completion_test_builder(
41134111
"\
4112+
foo = 1
4113+
41144114
class f<CURSOR>
41154115
",
41164116
);
41174117

4118-
assert!(builder.auto_import().build().completions().is_empty());
4118+
assert!(builder.build().completions().is_empty());
41194119
}
41204120

41214121
#[test]
@@ -4127,29 +4127,33 @@ class <CURSOR>
41274127
);
41284128

41294129
// This is okay because the ide will not request completions when the cursor is in this position.
4130-
assert!(!builder.auto_import().build().completions().is_empty());
4130+
assert!(!builder.build().completions().is_empty());
41314131
}
41324132

41334133
#[test]
41344134
fn no_completions_in_type_def_name() {
41354135
let builder = completion_test_builder(
41364136
"\
4137+
foo = 1
4138+
41374139
type f<CURSOR> = int
41384140
",
41394141
);
41404142

4141-
assert!(builder.auto_import().build().completions().is_empty());
4143+
assert!(builder.build().completions().is_empty());
41424144
}
41434145

41444146
#[test]
41454147
fn no_completions_in_maybe_type_def_name() {
41464148
let builder = completion_test_builder(
41474149
"\
4150+
foo = 1
4151+
41484152
type f<CURSOR>
41494153
",
41504154
);
41514155

4152-
assert!(builder.auto_import().build().completions().is_empty());
4156+
assert!(builder.build().completions().is_empty());
41534157
}
41544158

41554159
#[test]
@@ -4161,7 +4165,7 @@ type <CURSOR>
41614165
);
41624166

41634167
// This is okay because the ide will not request completions when the cursor is in this position.
4164-
assert!(!builder.auto_import().build().completions().is_empty());
4168+
assert!(!builder.build().completions().is_empty());
41654169
}
41664170

41674171
/// A way to create a simple single-file (named `main.py`) completion test

0 commit comments

Comments
 (0)