Skip to content

Commit ef51d05

Browse files
committed
fix: Don't show qualified path completions for private items
1 parent 6b823b0 commit ef51d05

File tree

6 files changed

+92
-32
lines changed

6 files changed

+92
-32
lines changed

crates/hir-def/src/nameres.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl DefMap {
262262
// NB: we use `None` as block here, which would be wrong for implicit
263263
// modules declared by blocks with items. At the moment, we don't use
264264
// this visibility for anything outside IDE, so that's probably OK.
265-
let visibility = Visibility::Module(ModuleId { krate, local_id, block: None });
265+
let visibility = Visibility::Public;
266266
let root = modules.alloc(ModuleData::new(root_module_origin, visibility));
267267
assert_eq!(local_id, root);
268268

crates/hir/src/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ impl HirDisplay for Module {
505505
// FIXME: Module doesn't have visibility saved in data.
506506
match self.name(f.db) {
507507
Some(name) => write!(f, "mod {}", name),
508-
None if self.is_crate_root(f.db) => match self.krate().display_name(f.db) {
508+
None if self.is_crate_root(f.db) => match self.krate(f.db).display_name(f.db) {
509509
Some(name) => write!(f, "extern crate {}", name),
510510
None => f.write_str("extern crate {unknown}"),
511511
},

crates/hir/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3573,3 +3573,27 @@ impl HasCrate for Macro {
35733573
self.module(db).krate()
35743574
}
35753575
}
3576+
3577+
impl HasCrate for Trait {
3578+
fn krate(&self, db: &dyn HirDatabase) -> Crate {
3579+
self.module(db).krate()
3580+
}
3581+
}
3582+
3583+
impl HasCrate for Static {
3584+
fn krate(&self, db: &dyn HirDatabase) -> Crate {
3585+
self.module(db).krate()
3586+
}
3587+
}
3588+
3589+
impl HasCrate for Adt {
3590+
fn krate(&self, db: &dyn HirDatabase) -> Crate {
3591+
self.module(db).krate()
3592+
}
3593+
}
3594+
3595+
impl HasCrate for Module {
3596+
fn krate(&self, _: &dyn HirDatabase) -> Crate {
3597+
Module::krate(*self)
3598+
}
3599+
}

crates/ide-completion/src/completions.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,19 @@ impl Completions {
174174
local_name: hir::Name,
175175
resolution: hir::ScopeDef,
176176
) {
177-
if ctx.is_scope_def_hidden(resolution) {
178-
cov_mark::hit!(qualified_path_doc_hidden);
179-
return;
180-
}
177+
let is_private_editable = match ctx.def_is_visible(&resolution) {
178+
Visible::Yes => false,
179+
Visible::Editable => true,
180+
Visible::No => return,
181+
};
181182
self.add(
182-
render_path_resolution(RenderContext::new(ctx), path_ctx, local_name, resolution)
183-
.build(),
183+
render_path_resolution(
184+
RenderContext::new(ctx).private_editable(is_private_editable),
185+
path_ctx,
186+
local_name,
187+
resolution,
188+
)
189+
.build(),
184190
);
185191
}
186192

@@ -191,13 +197,19 @@ impl Completions {
191197
local_name: hir::Name,
192198
resolution: hir::ScopeDef,
193199
) {
194-
if ctx.is_scope_def_hidden(resolution) {
195-
cov_mark::hit!(qualified_path_doc_hidden);
196-
return;
197-
}
200+
let is_private_editable = match ctx.def_is_visible(&resolution) {
201+
Visible::Yes => false,
202+
Visible::Editable => true,
203+
Visible::No => return,
204+
};
198205
self.add(
199-
render_pattern_resolution(RenderContext::new(ctx), pattern_ctx, local_name, resolution)
200-
.build(),
206+
render_pattern_resolution(
207+
RenderContext::new(ctx).private_editable(is_private_editable),
208+
pattern_ctx,
209+
local_name,
210+
resolution,
211+
)
212+
.build(),
201213
);
202214
}
203215

crates/ide-completion/src/context.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub(crate) enum PatternRefutability {
3333
Irrefutable,
3434
}
3535

36+
#[derive(Debug)]
3637
pub(crate) enum Visible {
3738
Yes,
3839
Editable,
@@ -383,6 +384,30 @@ impl<'a> CompletionContext<'a> {
383384
FamousDefs(&self.sema, self.krate)
384385
}
385386

387+
/// Checks if an item is visible and not `doc(hidden)` at the completion site.
388+
pub(crate) fn def_is_visible(&self, item: &ScopeDef) -> Visible {
389+
match item {
390+
ScopeDef::ModuleDef(def) => match def {
391+
hir::ModuleDef::Module(it) => self.is_visible(it),
392+
hir::ModuleDef::Function(it) => self.is_visible(it),
393+
hir::ModuleDef::Adt(it) => self.is_visible(it),
394+
hir::ModuleDef::Variant(it) => self.is_visible(it),
395+
hir::ModuleDef::Const(it) => self.is_visible(it),
396+
hir::ModuleDef::Static(it) => self.is_visible(it),
397+
hir::ModuleDef::Trait(it) => self.is_visible(it),
398+
hir::ModuleDef::TypeAlias(it) => self.is_visible(it),
399+
hir::ModuleDef::Macro(it) => self.is_visible(it),
400+
hir::ModuleDef::BuiltinType(_) => Visible::Yes,
401+
},
402+
ScopeDef::GenericParam(_)
403+
| ScopeDef::ImplSelfType(_)
404+
| ScopeDef::AdtSelfType(_)
405+
| ScopeDef::Local(_)
406+
| ScopeDef::Label(_)
407+
| ScopeDef::Unknown => Visible::Yes,
408+
}
409+
}
410+
386411
/// Checks if an item is visible and not `doc(hidden)` at the completion site.
387412
pub(crate) fn is_visible<I>(&self, item: &I) -> Visible
388413
where
@@ -393,14 +418,6 @@ impl<'a> CompletionContext<'a> {
393418
self.is_visible_impl(&vis, &attrs, item.krate(self.db))
394419
}
395420

396-
pub(crate) fn is_scope_def_hidden(&self, scope_def: ScopeDef) -> bool {
397-
if let (Some(attrs), Some(krate)) = (scope_def.attrs(self.db), scope_def.krate(self.db)) {
398-
return self.is_doc_hidden(&attrs, krate);
399-
}
400-
401-
false
402-
}
403-
404421
/// Check if an item is `#[doc(hidden)]`.
405422
pub(crate) fn is_item_hidden(&self, item: &hir::ItemInNs) -> bool {
406423
let attrs = item.attrs(self.db);
@@ -468,6 +485,14 @@ impl<'a> CompletionContext<'a> {
468485
self.scope.process_all_names(&mut |name, def| f(name, def));
469486
}
470487

488+
fn is_scope_def_hidden(&self, scope_def: ScopeDef) -> bool {
489+
if let (Some(attrs), Some(krate)) = (scope_def.attrs(self.db), scope_def.krate(self.db)) {
490+
return self.is_doc_hidden(&attrs, krate);
491+
}
492+
493+
false
494+
}
495+
471496
fn is_visible_impl(
472497
&self,
473498
vis: &hir::Visibility,

crates/ide-completion/src/tests/special.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,9 @@ mod m {
419419
pub use super::p::WrongType as RightType;
420420
}
421421
mod p {
422-
fn wrong_fn() {}
423-
const WRONG_CONST: u32 = 1;
424-
struct WrongType {};
422+
pub fn wrong_fn() {}
423+
pub const WRONG_CONST: u32 = 1;
424+
pub struct WrongType {};
425425
}
426426
"#,
427427
expect![[r#"
@@ -442,9 +442,9 @@ mod m {
442442
pub use super::p::WrongType as RightType;
443443
}
444444
mod p {
445-
fn wrong_fn() {}
446-
const WRONG_CONST: u32 = 1;
447-
struct WrongType {};
445+
pub fn wrong_fn() {}
446+
pub const WRONG_CONST: u32 = 1;
447+
pub struct WrongType {};
448448
}
449449
"#,
450450
r#"
@@ -456,9 +456,9 @@ mod m {
456456
pub use super::p::WrongType as RightType;
457457
}
458458
mod p {
459-
fn wrong_fn() {}
460-
const WRONG_CONST: u32 = 1;
461-
struct WrongType {};
459+
pub fn wrong_fn() {}
460+
pub const WRONG_CONST: u32 = 1;
461+
pub struct WrongType {};
462462
}
463463
"#,
464464
);
@@ -627,7 +627,6 @@ fn main() {
627627

628628
#[test]
629629
fn respects_doc_hidden2() {
630-
cov_mark::check!(qualified_path_doc_hidden);
631630
check(
632631
r#"
633632
//- /lib.rs crate:lib deps:dep

0 commit comments

Comments
 (0)