Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: don't suggest private struct fields in LSP #6256

Merged
merged 1 commit into from
Oct 9, 2024
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
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 @@
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 All @@ -42,7 +44,7 @@
use super::process_request;

mod auto_import;
mod builtins;

Check warning on line 47 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (builtins)
mod completion_items;
mod kinds;
mod sort_text;
Expand Down Expand Up @@ -236,7 +238,7 @@
let mut idents: Vec<Ident> = Vec::new();

// Find in which ident we are in, and in which part of it
// (it could be that we are completting in the middle of an ident)

Check warning on line 241 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (completting)
for segment in &path.segments {
let ident = &segment.ident;

Expand Down Expand Up @@ -691,16 +693,24 @@
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 Expand Up @@ -1699,8 +1709,8 @@
///
/// For example:
///
/// // "merk" and "ro" match "merkle" and "root" and are in order

Check warning on line 1712 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (merk)
/// name_matches("compute_merkle_root", "merk_ro") == true

Check warning on line 1713 in tooling/lsp/src/requests/completion.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (merk)
///
/// // "ro" matches "root", but "merkle" comes before it, so no match
/// name_matches("compute_merkle_root", "ro_mer") == false
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 @@ -133,14 +133,14 @@
#[test]
async fn test_use_first_segment() {
let src = r#"
mod foobaz {}

Check warning on line 136 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foobaz)
mod foobar {}
use foob>|<

Check warning on line 138 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
"#;

assert_completion(
src,
vec![module_completion_item("foobaz"), module_completion_item("foobar")],

Check warning on line 143 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foobaz)
)
.await;
}
Expand Down Expand Up @@ -303,7 +303,7 @@
mod bar {
mod something {}

use super::foob>|<

Check warning on line 306 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (foob)
}
"#;

Expand Down Expand Up @@ -1071,6 +1071,22 @@
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 Expand Up @@ -1575,7 +1591,7 @@
async fn test_auto_import_suggests_modules_too() {
let src = r#"
mod foo {
pub mod barbaz {

Check warning on line 1594 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (barbaz)
fn hello_world() {}
}
}
Expand All @@ -1588,7 +1604,7 @@
assert_eq!(items.len(), 1);

let item = &items[0];
assert_eq!(item.label, "barbaz");

Check warning on line 1607 in tooling/lsp/src/requests/completion/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (barbaz)
assert_eq!(
item.label_details,
Some(CompletionItemLabelDetails {
Expand Down
Loading