Skip to content

Commit 20a62ab

Browse files
authored
Merge pull request #20025 from SoxPopuli/hide_private_imports_without_pe
Hide imported privates if private editable is disabled
2 parents 5f2cf7e + 1f24f02 commit 20a62ab

File tree

2 files changed

+98
-5
lines changed

2 files changed

+98
-5
lines changed

src/tools/rust-analyzer/crates/ide-completion/src/completions/expr.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,16 @@ pub(crate) fn complete_expr_path(
145145
});
146146
match resolution {
147147
hir::PathResolution::Def(hir::ModuleDef::Module(module)) => {
148-
// Set visible_from to None so private items are returned.
149-
// They will be possibly filtered out in add_path_resolution()
150-
// via def_is_visible().
151-
let module_scope = module.scope(ctx.db, None);
148+
let visible_from = if ctx.config.enable_private_editable {
149+
// Set visible_from to None so private items are returned.
150+
// They will be possibly filtered out in add_path_resolution()
151+
// via def_is_visible().
152+
None
153+
} else {
154+
Some(ctx.module)
155+
};
156+
157+
let module_scope = module.scope(ctx.db, visible_from);
152158
for (name, def) in module_scope {
153159
if scope_def_applicable(def) {
154160
acc.add_path_resolution(

src/tools/rust-analyzer/crates/ide-completion/src/tests/visibility.rs

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Completion tests for visibility modifiers.
22
use expect_test::expect;
33

4-
use crate::tests::{check, check_with_trigger_character};
4+
use crate::tests::{check, check_with_private_editable, check_with_trigger_character};
55

66
#[test]
77
fn empty_pub() {
@@ -78,3 +78,90 @@ mod bar {}
7878
"#]],
7979
);
8080
}
81+
82+
#[test]
83+
fn use_inner_public_function() {
84+
check(
85+
r#"
86+
//- /inner.rs crate:inner
87+
pub fn inner_public() {}
88+
fn inner_private() {}
89+
//- /foo.rs crate:foo deps:inner
90+
use inner::inner_public;
91+
pub fn outer_public() {}
92+
//- /lib.rs crate:lib deps:foo
93+
fn x() {
94+
foo::$0
95+
}
96+
"#,
97+
expect![[r#"
98+
fn outer_public() fn()
99+
"#]],
100+
);
101+
}
102+
103+
#[test]
104+
fn pub_use_inner_public_function() {
105+
check(
106+
r#"
107+
//- /inner.rs crate:inner
108+
pub fn inner_public() {}
109+
fn inner_private() {}
110+
//- /foo.rs crate:foo deps:inner
111+
pub use inner::inner_public;
112+
pub fn outer_public() {}
113+
//- /lib.rs crate:lib deps:foo
114+
fn x() {
115+
foo::$0
116+
}
117+
"#,
118+
expect![[r#"
119+
fn inner_public() fn()
120+
fn outer_public() fn()
121+
"#]],
122+
);
123+
}
124+
125+
#[test]
126+
fn use_inner_public_function_private_editable() {
127+
check_with_private_editable(
128+
r#"
129+
//- /inner.rs crate:inner
130+
pub fn inner_public() {}
131+
fn inner_private() {}
132+
//- /foo.rs crate:foo deps:inner
133+
use inner::inner_public;
134+
pub fn outer_public() {}
135+
//- /lib.rs crate:lib deps:foo
136+
fn x() {
137+
foo::$0
138+
}
139+
"#,
140+
expect![[r#"
141+
fn inner_public() fn()
142+
fn outer_public() fn()
143+
"#]],
144+
);
145+
}
146+
147+
#[test]
148+
fn pub_use_inner_public_function_private_editable() {
149+
check_with_private_editable(
150+
r#"
151+
//- /inner.rs crate:inner
152+
pub fn inner_public() {}
153+
fn inner_private() {}
154+
//- /foo.rs crate:foo deps:inner
155+
pub use inner::inner_public;
156+
pub fn outer_public() {}
157+
//- /lib.rs crate:lib deps:foo
158+
fn x() {
159+
foo::$0
160+
}
161+
"#,
162+
expect![[r#"
163+
fn inner_public() fn()
164+
fn outer_public() fn()
165+
"#]],
166+
);
167+
}

0 commit comments

Comments
 (0)