mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-20 04:29:47 +00:00
[ty] Fix tests for definition completions (#21153)
This commit is contained in:
parent
8737a2d5f5
commit
4b758b3746
1 changed files with 28 additions and 23 deletions
|
|
@ -854,23 +854,25 @@ fn is_in_string(tokens: &[Token]) -> bool {
|
||||||
/// If the tokens end with `class` or `def`, we return false.
|
/// If the tokens end with `class` or `def`, we return false.
|
||||||
/// This is fine because we don't provide completions anyway.
|
/// 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| {
|
||||||
|
if matches!(
|
||||||
|
token.kind(),
|
||||||
|
TokenKind::Def | TokenKind::Class | TokenKind::Type
|
||||||
|
) {
|
||||||
|
true
|
||||||
|
} else if token.kind() == TokenKind::Name {
|
||||||
|
let source = source_text(db, file);
|
||||||
|
&source[token.range()] == "type"
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
tokens
|
tokens
|
||||||
.len()
|
.len()
|
||||||
.checked_sub(2)
|
.checked_sub(2)
|
||||||
.and_then(|i| tokens.get(i))
|
.and_then(|i| tokens.get(i))
|
||||||
.is_some_and(|t| {
|
.is_some_and(is_definition_keyword)
|
||||||
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
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Order completions according to the following rules:
|
/// Order completions according to the following rules:
|
||||||
|
|
@ -4090,18 +4092,19 @@ def f<CURSOR>
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
|
|
||||||
builder.auto_import().build().not_contains("fabs");
|
assert!(builder.auto_import().build().completions().is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_completions_in_function_def_empty_name() {
|
fn completions_in_function_def_empty_name() {
|
||||||
let builder = completion_test_builder(
|
let builder = completion_test_builder(
|
||||||
"\
|
"\
|
||||||
def <CURSOR>
|
def <CURSOR>
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
|
|
||||||
builder.auto_import().build().not_contains("fabs");
|
// 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -4112,18 +4115,19 @@ class f<CURSOR>
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
|
|
||||||
builder.auto_import().build().not_contains("fabs");
|
assert!(builder.auto_import().build().completions().is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_completions_in_class_def_empty_name() {
|
fn completions_in_class_def_empty_name() {
|
||||||
let builder = completion_test_builder(
|
let builder = completion_test_builder(
|
||||||
"\
|
"\
|
||||||
class <CURSOR>
|
class <CURSOR>
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
|
|
||||||
builder.auto_import().build().not_contains("fabs");
|
// 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -4134,7 +4138,7 @@ type f<CURSOR> = int
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
|
|
||||||
builder.auto_import().build().not_contains("fabs");
|
assert!(builder.auto_import().build().completions().is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -4145,18 +4149,19 @@ type f<CURSOR>
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
|
|
||||||
builder.auto_import().build().not_contains("fabs");
|
assert!(builder.auto_import().build().completions().is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_completions_in_type_def_empty_name() {
|
fn completions_in_type_def_empty_name() {
|
||||||
let builder = completion_test_builder(
|
let builder = completion_test_builder(
|
||||||
"\
|
"\
|
||||||
type <CURSOR>
|
type <CURSOR>
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
|
|
||||||
builder.auto_import().build().not_contains("fabs");
|
// 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue