From 9e9c4be48a2efdc1e209dbb531cabb9e81840516 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Tue, 24 Dec 2019 21:46:07 +0100 Subject: Hide completions for private struct fields --- crates/ra_ide/src/completion/complete_dot.rs | 57 +++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index 294964887..13546c143 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir::Type; +use hir::{HasVisibility, Type}; use crate::completion::completion_item::CompletionKind; use crate::{ @@ -38,9 +38,15 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) { for receiver in receiver.autoderef(ctx.db) { for (field, ty) in receiver.fields(ctx.db) { + if ctx.module.map_or(false, |m| !field.visible_from(ctx.db, m)) { + // Skip private field. FIXME: If the definition location of the + // field is editable, we should show the completion + continue; + } acc.add_field(ctx, field, &ty); } for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() { + // FIXME: Handle visibility acc.add_tuple_field(ctx, i, &ty); } } @@ -186,6 +192,55 @@ mod tests { ); } + #[test] + fn test_struct_field_visibility_private() { + assert_debug_snapshot!( + do_ref_completion( + r" + mod inner { + struct A { + private_field: u32, + pub pub_field: u32, + pub(crate) crate_field: u32, + pub(super) super_field: u32, + } + } + fn foo(a: inner::A) { + a.<|> + } + ", + ), + @r###" + [ + CompletionItem { + label: "crate_field", + source_range: [313; 313), + delete: [313; 313), + insert: "crate_field", + kind: Field, + detail: "u32", + }, + CompletionItem { + label: "pub_field", + source_range: [313; 313), + delete: [313; 313), + insert: "pub_field", + kind: Field, + detail: "u32", + }, + CompletionItem { + label: "super_field", + source_range: [313; 313), + delete: [313; 313), + insert: "super_field", + kind: Field, + detail: "u32", + }, + ] + "### + ); + } + #[test] fn test_method_completion() { assert_debug_snapshot!( -- cgit v1.2.3 From 9fd2c813ca355c3a1f10f54993c16e81778b867b Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Fri, 27 Dec 2019 11:24:31 +0100 Subject: visible_from -> is_visible_from --- crates/ra_ide/src/completion/complete_dot.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index 13546c143..210a685e4 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs @@ -38,7 +38,7 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) { for receiver in receiver.autoderef(ctx.db) { for (field, ty) in receiver.fields(ctx.db) { - if ctx.module.map_or(false, |m| !field.visible_from(ctx.db, m)) { + if ctx.module.map_or(false, |m| !field.is_visible_from(ctx.db, m)) { // Skip private field. FIXME: If the definition location of the // field is editable, we should show the completion continue; -- cgit v1.2.3