Skip to content

Commit

Permalink
LS: Limit module import completion to visible members (#6566)
Browse files Browse the repository at this point in the history
  • Loading branch information
integraledelebesgue authored Nov 14, 2024
1 parent 89a00fe commit a952721
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,24 @@ pub fn colon_colon_completions(
.resolve_concrete_path(&mut diagnostics, segments, NotFoundItemType::Identifier)
.ok()?;

let current_module_id = module_file_id.0;

Some(match item {
ResolvedConcreteItem::Module(module_id) => db
.module_items(module_id)
.ok()?
.iter()
.map(|item| CompletionItem {
label: item.name(db.upcast()).to_string(),
kind: ResolvedGenericItem::from_module_item(db, *item)
.ok()
.map(resolved_generic_item_completion_kind),
..CompletionItem::default()
.filter_map(|item| {
let resolved_item = ResolvedGenericItem::from_module_item(db, *item).ok()?;
let item_info = db.module_item_info_by_name(module_id, item.name(db)).ok()??;

peek_visible_in(db, item_info.visibility, module_id, current_module_id).then(|| {
CompletionItem {
label: item.name(db.upcast()).to_string(),
kind: Some(resolved_generic_item_completion_kind(resolved_item)),
..CompletionItem::default()
}
})
})
.collect(),
ResolvedConcreteItem::Trait(item) => db
Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-language-server/tests/e2e/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cairo_lang_test_utils::test_file_test!(
{
methods_text_edits: "methods_text_edits.txt",
structs: "structs.txt",
module_items: "module_items.txt",
},
test_completions_text_edits

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! > Test completing public members of a module.

//! > test_runner_name
test_completions_text_edits

//! > cairo_project.toml
[crate_roots]
hello = "src"

[config.global]
edition = "2024_07"

//! > cairo_code
mod helper_module {
pub trait Trait1<T> {
fn some_method(self: @T);
}

pub const CONST: felt252 = 0x0;

fn foo() {}
pub fn bar() {}
}

mod not_exporting_module {
const CONST: u32 = 0;
fn foo() {}
fn bar() {}
}

mod nested_module {
pub mod inner {}
}

use helper_module::<caret>;
use non_exporting_module::<caret>;
use nested_module::<caret>;
use non_existent_module::<caret>;

//! > Completions #0
use helper_module::<caret>;
--------------------------
Completion: Trait1
--------------------------
Completion: CONST
--------------------------
Completion: bar

//! > Completions #1
use non_exporting_module::<caret>;

//! > Completions #2
use nested_module::<caret>;
--------------------------
Completion: inner

//! > Completions #3
use non_existent_module::<caret>;

0 comments on commit a952721

Please sign in to comment.