Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 87 additions & 64 deletions crates/ty_ide/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ f<CURSOR>
",
);

assert_snapshot!(test.completions(), @r"
f
foo
");
assert_snapshot!(test.completions(), @"foo");
}

#[test]
Expand All @@ -143,10 +140,7 @@ g<CURSOR>
",
);

assert_snapshot!(test.completions(), @r"
foo
g
");
assert_snapshot!(test.completions(), @"foo");
}

#[test]
Expand Down Expand Up @@ -175,10 +169,7 @@ f<CURSOR>
",
);

assert_snapshot!(test.completions(), @r"
f
foo
");
assert_snapshot!(test.completions(), @"foo");
}

#[test]
Expand Down Expand Up @@ -208,7 +199,6 @@ def foo():
);

assert_snapshot!(test.completions(), @r"
f
foo
foofoo
");
Expand Down Expand Up @@ -259,7 +249,6 @@ def foo():
);

assert_snapshot!(test.completions(), @r"
f
foo
foofoo
");
Expand All @@ -276,7 +265,6 @@ def foo():
);

assert_snapshot!(test.completions(), @r"
f
foo
foofoo
");
Expand All @@ -295,7 +283,6 @@ def frob(): ...
);

assert_snapshot!(test.completions(), @r"
f
foo
foofoo
frob
Expand All @@ -315,7 +302,6 @@ def frob(): ...
);

assert_snapshot!(test.completions(), @r"
f
foo
frob
");
Expand All @@ -334,7 +320,6 @@ def frob(): ...
);

assert_snapshot!(test.completions(), @r"
f
foo
foofoo
foofoofoo
Expand Down Expand Up @@ -451,15 +436,10 @@ def frob(): ...
",
);

// It's not totally clear why `for` shows up in the
// symbol tables of the detected scopes here. My guess
// is that there's perhaps some sub-optimal behavior
// here because the list comprehension as written is not
// valid.
assert_snapshot!(test.completions(), @r"
bar
for
");
// TODO: it would be good if `bar` was included here, but
// the list comprehension is not yet valid and so we do not
// detect this as a definition of `bar`.
assert_snapshot!(test.completions(), @"<No completions found>");
}

#[test]
Expand All @@ -470,10 +450,7 @@ def frob(): ...
",
);

assert_snapshot!(test.completions(), @r"
f
foo
");
assert_snapshot!(test.completions(), @"foo");
}

#[test]
Expand All @@ -484,10 +461,7 @@ def frob(): ...
",
);

assert_snapshot!(test.completions(), @r"
f
foo
");
assert_snapshot!(test.completions(), @"foo");
}

#[test]
Expand All @@ -498,10 +472,7 @@ def frob(): ...
",
);

assert_snapshot!(test.completions(), @r"
f
foo
");
assert_snapshot!(test.completions(), @"foo");
}

#[test]
Expand All @@ -512,10 +483,7 @@ def frob(): ...
",
);

assert_snapshot!(test.completions(), @r"
f
foo
");
assert_snapshot!(test.completions(), @"foo");
}

#[test]
Expand All @@ -526,10 +494,7 @@ def frob(): ...
",
);

assert_snapshot!(test.completions(), @r"
f
foo
");
assert_snapshot!(test.completions(), @"foo");
}

#[test]
Expand Down Expand Up @@ -602,7 +567,6 @@ class Foo:

assert_snapshot!(test.completions(), @r"
Foo
b
bar
frob
quux
Expand All @@ -621,7 +585,6 @@ class Foo:

assert_snapshot!(test.completions(), @r"
Foo
b
bar
quux
");
Expand Down Expand Up @@ -750,7 +713,6 @@ bar(o<CURSOR>
assert_snapshot!(test.completions(), @r"
bar
foo
o
");
}

Expand Down Expand Up @@ -788,7 +750,6 @@ class C:
assert_snapshot!(test.completions(), @r"
C
bar
f
foo
self
");
Expand Down Expand Up @@ -825,7 +786,6 @@ class C:
assert_snapshot!(test.completions(), @r"
C
bar
f
foo
self
");
Expand Down Expand Up @@ -854,11 +814,55 @@ print(f\"{some<CURSOR>
",
);

assert_snapshot!(test.completions(), @r"
print
some
some_symbol
");
assert_snapshot!(test.completions(), @"some_symbol");
}

#[test]
fn statically_invisible_symbols() {
let test = cursor_test(
"\
if 1 + 2 != 3:
hidden_symbol = 1

hidden_<CURSOR>
",
);

assert_snapshot!(test.completions(), @"<No completions found>");
}

#[test]
fn completions_inside_unreachable_sections() {
let test = cursor_test(
"\
import sys

if sys.platform == \"not-my-current-platform\":
only_available_in_this_branch = 1

on<CURSOR>
",
);

// TODO: ideally, `only_available_in_this_branch` should be available here, but we
// currently make no effort to provide a good IDE experience within sections that
// are unreachable
assert_snapshot!(test.completions(), @"sys");
}

#[test]
fn star_import() {
let test = cursor_test(
"\
from typing import *

Re<CURSOR>
",
);

test.assert_completions_include("Reversible");
// `ReadableBuffer` is a symbol in `typing`, but it is not re-exported
test.assert_completions_do_not_include("ReadableBuffer");
}

// Ref: https://github.com/astral-sh/ty/issues/572
Expand Down Expand Up @@ -921,10 +925,7 @@ Fo<CURSOR> = float
",
);

assert_snapshot!(test.completions(), @r"
Fo
float
");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

float will be available again once we add all builtins as a source for completions.

assert_snapshot!(test.completions(), @"Fo");
}

// Ref: https://github.com/astral-sh/ty/issues/572
Expand Down Expand Up @@ -999,9 +1000,7 @@ except Type<CURSOR>:
",
);

assert_snapshot!(test.completions(), @r"
Type
");
assert_snapshot!(test.completions(), @"<No completions found>");
}

// Ref: https://github.com/astral-sh/ty/issues/572
Expand Down Expand Up @@ -1029,5 +1028,29 @@ def _():
.collect::<Vec<String>>()
.join("\n")
}

#[track_caller]
fn assert_completions_include(&self, expected: &str) {
let completions = completion(&self.db, self.file, self.cursor_offset);

assert!(
completions
.iter()
.any(|completion| completion.label == expected),
"Expected completions to include `{expected}`"
);
}

#[track_caller]
fn assert_completions_do_not_include(&self, unexpected: &str) {
let completions = completion(&self.db, self.file, self.cursor_offset);

assert!(
completions
.iter()
.all(|completion| completion.label != unexpected),
"Expected completions to not include `{unexpected}`",
);
}
}
}
8 changes: 5 additions & 3 deletions crates/ty_python_semantic/src/semantic_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::module_resolver::{Module, resolve_module};
use crate::semantic_index::ast_ids::HasScopedExpressionId;
use crate::semantic_index::semantic_index;
use crate::semantic_index::symbol::FileScopeId;
use crate::types::ide_support::all_declarations_and_bindings;
use crate::types::{Type, binding_type, infer_scope_types};

pub struct SemanticModel<'db> {
Expand Down Expand Up @@ -66,9 +67,10 @@ impl<'db> SemanticModel<'db> {
};
let mut symbols = vec![];
for (file_scope, _) in index.ancestor_scopes(file_scope) {
for symbol in index.symbol_table(file_scope).symbols() {
symbols.push(symbol.name().clone());
}
symbols.extend(all_declarations_and_bindings(
self.db,
file_scope.to_scope_id(self.db, self.file),
));
}
symbols
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ty_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ mod diagnostic;
mod display;
mod function;
mod generics;
mod ide_support;
pub(crate) mod ide_support;
mod infer;
mod instance;
mod mro;
Expand Down
Loading
Loading