diff --git a/crates/ty_completion_eval/completion-evaluation-tasks.csv b/crates/ty_completion_eval/completion-evaluation-tasks.csv index 4bea881bf6..92d1a3f03d 100644 --- a/crates/ty_completion_eval/completion-evaluation-tasks.csv +++ b/crates/ty_completion_eval/completion-evaluation-tasks.csv @@ -17,7 +17,7 @@ numpy-array,main.py,1,1 object-attr-instance-methods,main.py,0,1 object-attr-instance-methods,main.py,1,1 raise-uses-base-exception,main.py,0,2 -scope-existing-over-new-import,main.py,0,13 +scope-existing-over-new-import,main.py,0,1 scope-prioritize-closer,main.py,0,2 scope-simple-long-identifier,main.py,0,1 tstring-completions,main.py,0,1 diff --git a/crates/ty_ide/src/completion.rs b/crates/ty_ide/src/completion.rs index 5b84e199f0..ae856dd7e7 100644 --- a/crates/ty_ide/src/completion.rs +++ b/crates/ty_ide/src/completion.rs @@ -883,9 +883,10 @@ fn is_in_definition_place(db: &dyn Db, tokens: &[Token], file: File) -> bool { /// This has the effect of putting all dunder attributes after "normal" /// attributes, and all single-underscore attributes after dunder attributes. fn compare_suggestions(c1: &Completion, c2: &Completion) -> Ordering { - fn key<'a>(completion: &'a Completion) -> (bool, NameKind, bool, &'a Name) { + fn key<'a>(completion: &'a Completion) -> (bool, bool, NameKind, bool, &'a Name) { ( completion.module_name.is_some(), + completion.builtin, NameKind::classify(&completion.name), completion.is_type_check_only, &completion.name, @@ -4196,6 +4197,25 @@ type "); } + #[test] + fn favour_imported_over_builtin() { + let snapshot = + completion_test_builder("from typing import Protocol\nclass Foo(P: ...") + .filter(|c| c.name.starts_with('P')) + .build() + .snapshot(); + + // Here we favour `Protocol` over the other completions + // because `Protocol` has been imported, and the other completions are builtin. + assert_snapshot!(snapshot, @r" + Protocol + PendingDeprecationWarning + PermissionError + ProcessLookupError + PythonFinalizationError + "); + } + /// A way to create a simple single-file (named `main.py`) completion test /// builder. ///