Skip to content

Commit

Permalink
feat: don't suggest private struct fields in LSP
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Oct 9, 2024
1 parent fc1c7ab commit 644ef53
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
34 changes: 22 additions & 12 deletions tooling/lsp/src/requests/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ use noirc_frontend::{
UseTreeKind, Visitor,
},
graph::{CrateId, Dependency},
hir::def_map::{CrateDefMap, LocalModuleId, ModuleDefId, ModuleId},
hir::{
def_map::{CrateDefMap, LocalModuleId, ModuleDefId, ModuleId},
resolution::visibility::struct_field_is_visible,
},
hir_def::traits::Trait,
node_interner::NodeInterner,
node_interner::ReferenceId,
node_interner::{NodeInterner, ReferenceId},
parser::{Item, ItemKind, ParsedSubModule},
token::{CustomAttribute, Token, Tokens},
Kind, ParsedModule, StructType, Type, TypeBinding,
Expand Down Expand Up @@ -691,16 +693,24 @@ impl<'a> NodeFinder<'a> {
prefix: &str,
self_prefix: bool,
) {
for (field_index, (name, typ)) in struct_type.get_fields(generics).iter().enumerate() {
if name_matches(name, prefix) {
self.completion_items.push(self.struct_field_completion_item(
name,
typ,
struct_type.id,
field_index,
self_prefix,
));
for (field_index, (name, visibility, typ)) in
struct_type.get_fields_with_visibility(generics).iter().enumerate()
{
if !struct_field_is_visible(struct_type, *visibility, self.module_id, self.def_maps) {
continue;
}

if !name_matches(name, prefix) {
continue;
}

self.completion_items.push(self.struct_field_completion_item(
name,
typ,
struct_type.id,
field_index,
self_prefix,
));
}
}

Expand Down
16 changes: 16 additions & 0 deletions tooling/lsp/src/requests/completion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,22 @@ mod completion_tests {
assert_completion(src, vec![field_completion_item("bar", "i32")]).await;
}

#[test]
async fn test_does_not_suggest_private_struct_field() {
let src = r#"
mod moo {
pub struct Some {
property: i32,
}
}
fn foo(s: moo::Some) {
s.>|<
}
"#;
assert_completion(src, vec![]).await;
}

#[test]
async fn test_suggests_struct_impl_method() {
let src = r#"
Expand Down

0 comments on commit 644ef53

Please sign in to comment.