[ty] Sort keyword completions above everything else

It looks like VS Code does this forcefully. As in, I don't think we can
override it. It also seems like a plausibly good idea. But by us doing
it too, it makes our completion evaluation framework match real world
conditions. (To the extent that "VS Code" and "real world conditions"
are the same. Which... they aren't. But it's close, since VS Code is so
popular.)
This commit is contained in:
Andrew Gallant 2025-11-11 10:47:34 -05:00 committed by Andrew Gallant
parent 1bbe4f0d5e
commit 164c2a6cc6
2 changed files with 12 additions and 3 deletions

View file

@ -12,12 +12,12 @@ import-deprioritizes-type_check_only,main.py,3,2
import-deprioritizes-type_check_only,main.py,4,3
import-keyword-completion,main.py,0,1
internal-typeshed-hidden,main.py,0,5
none-completion,main.py,0,11
none-completion,main.py,0,2
numpy-array,main.py,0,
numpy-array,main.py,1,1
object-attr-instance-methods,main.py,0,1
object-attr-instance-methods,main.py,1,1
pass-keyword-completion,main.py,0,7
pass-keyword-completion,main.py,0,1
raise-uses-base-exception,main.py,0,2
scope-existing-over-new-import,main.py,0,1
scope-prioritize-closer,main.py,0,2

1 name file index rank
12 import-deprioritizes-type_check_only main.py 4 3
13 import-keyword-completion main.py 0 1
14 internal-typeshed-hidden main.py 0 5
15 none-completion main.py 0 11 2
16 numpy-array main.py 0
17 numpy-array main.py 1 1
18 object-attr-instance-methods main.py 0 1
19 object-attr-instance-methods main.py 1 1
20 pass-keyword-completion main.py 0 7 1
21 raise-uses-base-exception main.py 0 2
22 scope-existing-over-new-import main.py 0 1
23 scope-prioritize-closer main.py 0 2

View file

@ -1004,9 +1004,18 @@ 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, bool, NameKind, bool, &'a Name) {
fn key<'a>(completion: &'a Completion) -> (bool, bool, bool, NameKind, bool, &'a Name) {
(
completion.module_name.is_some(),
// At time of writing (2025-11-11), keyword completions
// are classified as builtins, which makes them sort after
// everything else. But we probably want keyword completions
// to sort *before* anything else since they are so common.
// Moreover, it seems VS Code forcefully does this sorting.
// By doing it ourselves, we make our natural sorting match
// VS Code's, and thus our completion evaluation framework
// should be more representative of real world conditions.
completion.kind != Some(CompletionKind::Keyword),
completion.builtin,
NameKind::classify(&completion.name),
completion.is_type_check_only,