Skip to content

Commit

Permalink
feat: don't suggest private struct fields in LSP (#6256)
Browse files Browse the repository at this point in the history
# Description

## Problem

Struct fields now have visibility, but LSP still suggests all fields
regardless of their visibility.

## Summary

Hides non-accessible struct fields using the same logic the compiler
applies to check if a struct field is visible.

## Additional Context

None.

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
asterite authored Oct 9, 2024
1 parent fc1c7ab commit 2a727b3
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 2a727b3

Please sign in to comment.