Skip to content
Draft
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
2 changes: 1 addition & 1 deletion pyrefly/lib/lsp/wasm/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ impl Transaction<'_> {
});
}

for module_name in self.search_modules_fuzzy(identifier_text) {
for module_name in self.search_modules_fuzzy(handle, identifier_text) {
if module_name == handle.module() {
continue;
}
Expand Down
10 changes: 7 additions & 3 deletions pyrefly/lib/state/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2917,11 +2917,15 @@ impl<'a> Transaction<'a> {
.map(|item| TextRangeWithModule::new(item.module, item.definition_range))
}

pub(crate) fn search_modules_fuzzy(&self, pattern: &str) -> Vec<ModuleName> {
pub(crate) fn search_modules_fuzzy(&self, handle: &Handle, pattern: &str) -> Vec<ModuleName> {
let matcher = SkimMatcherV2::default().smart_case();
let mut results = Vec::new();
// `self.modules()` only contains modules that have already been loaded. Include modules
// discoverable from the active file's import paths so auto-import works on the first try.
let mut module_names: HashSet<_> = self.modules().into_iter().collect();
module_names.extend(self.import_prefixes(handle, ModuleName::from_str(pattern)));

for module_name in self.modules() {
for module_name in module_names {
let module_name_str = module_name.as_str();

// Skip builtins module
Expand Down Expand Up @@ -3010,7 +3014,7 @@ impl<'a> Transaction<'a> {
&mut import_actions,
unknown_name,
);
for module_name in self.search_modules_fuzzy(unknown_name) {
for module_name in self.search_modules_fuzzy(handle, unknown_name) {
if module_name == handle.module() {
continue;
}
Expand Down
35 changes: 35 additions & 0 deletions pyrefly/lib/test/lsp/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2398,6 +2398,41 @@ jso
);
}

#[test]
fn autoimport_suggests_unloaded_stdlib_module() {
let code = r#"
jso
# ^
"#;
let mut env = TestEnv::new().with_default_require_level(Require::Exports);
env.add("main", code);
let (state, handle_for) = env.to_state();
let handle = handle_for("main");
let position = extract_cursors_for_test(code)[0];

let completions =
state
.transaction()
.completion(&handle, position, ImportFormat::Absolute, true, None);
let json = completions
.iter()
.find(|item| item.label == "json")
.expect("expected an auto-import completion for the unloaded json module");
assert!(
json.detail
.as_ref()
.is_some_and(|detail| detail.contains("import json")),
"expected json completion to add an import, got {:?}",
json.detail
);
assert!(
json.additional_text_edits
.as_ref()
.is_some_and(|edits| !edits.is_empty()),
"expected json completion to include an import edit"
);
}

#[test]
fn autoimport_does_not_duplicate_existing_module_import_after_another_import() {
let code = r#"
Expand Down
Loading